• cogs 577 . 蝗灾 cdq分治


    题目链接

    两种操作, 一种将x, y这个格子+w, 另一种给出左下和右上坐标, 问你这个矩形中值的和。

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <complex>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef complex <double> cmx;
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 2e5+5;
    struct node
    {
        int x1, y1, x2, y2, w, id, sign;
        bool operator < (node a) const
        {
            return id < a.id;
        }
    }a[maxn], c[maxn*2];
    int sum[500005], ans[maxn], n, m;
    int lowbit(int x)
    {
        return x & (-x);
    }
    void update(int x, int val)
    {
        while(x <= m) {
            sum[x] += val;
            x += lowbit(x);
        }
    }
    int query(int x)
    {
        int ret = 0;
        while(x) {
            ret += sum[x];
            x -= lowbit(x);
        }
        return ret;
    }
    bool cmp(const node& a, const node& b)
    {
        if(a.x1 != b.x1)
            return a.x1 < b.x1;
        return a.sign < b.sign;
    }
    void cdq(int l, int r)
    {
        if(l == r)
            return ;
        int m = l+r>>1;
        cdq(l, m);
        cdq(m+1, r);
        int num = 0;
        for(int i = l; i <= m; i++) {
            if(a[i].sign == 1) {
                c[num++] = a[i];
            }
        }
        for(int i = m + 1; i <= r; i++) {
            if(a[i].sign == 2) {
                c[num++] = a[i];
                c[num-1].x1--;
                c[num++] = a[i];
                c[num-1].x1 = c[num-1].x2;
                c[num-1].sign = 3;
            }
        }
        sort(c, c+num, cmp);
        for(int i = 0; i < num; i++) {
            if(c[i].sign == 1) {
                update(c[i].y1, c[i].w);
            } else if(c[i].sign == 2) {
                ans[c[i].id] -= (query(c[i].y2) - query(c[i].y1 - 1));
            } else {
                ans[c[i].id] += (query(c[i].y2) - query(c[i].y1 - 1));
            }
        }
        for(int i = l; i <= m; i++) {
            if(a[i].sign == 1) {
                update(a[i].y1, -a[i].w);
            }
        }
    }
    int main()
    {
        freopen ( "locust.in" , "r" , stdin ) ;
        freopen ( "locust.out" , "w" , stdout ) ;
        while(cin>>m>>n) {
            for(int i = 1; i <= n; i++) {
                scanf("%d", &a[i].sign);
                if(a[i].sign == 1) {
                    scanf("%d%d%d", &a[i].x1, &a[i].y1, &a[i].w);
                } else {
                    scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2);
                }
                a[i].id = i;
            }
            mem(sum);
            mem(ans);
            cdq(1, n);
            for(int i = 1; i <= n; i++) {
                if(a[i].sign == 2) {
                    printf("%d
    ", ans[i]);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    iOS 在Host App 与 App Extension 之间发送通知
    将博客搬至CSDN
    ios7 UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示
    UITableViewCell 分割线如何满屏
    CocoaPods管理的项目移植到别人电脑后找不到头文件
    iOS Touch ID 简易开发教程
    iOS内存泄露统计
    iOS开发之Objective-c的AES128加密和解密算法的实现
    iOS中使用RNCryptor对资源文件加密
    iOS持续集成:命令行创建工程
  • 原文地址:https://www.cnblogs.com/yohaha/p/5703150.html
Copyright © 2020-2023  润新知