• fafu 1100 线段树


    题目链接

    单点更新, 区间查询。 这题空间好小....

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    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, a, n) for(int i = a; i<n; i++)
    #define ull unsigned long long
    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 = 1050;
    int sum[maxn*3][maxn*3], n;
    void pushUp(int pos, int rt) {
        sum[pos][rt] = sum[pos][rt<<1]+sum[pos][rt<<1|1];
    }
    void sub_update(int sign, int pos, int p, int l, int r, int rt, int val) {
        if(l == r) {
            if(!sign) {
                sum[pos][rt] += val;
            } else {
                sum[pos][rt] = sum[pos<<1][rt] + sum[pos<<1|1][rt];
            }
            return ;
        }
        int m = l+r>>1;
        if(p<=m)
            sub_update(sign, pos, p, lson, val);
        else
            sub_update(sign, pos, p, rson, val);
        pushUp(pos, rt);
    }
    void update(int x, int y, int l, int r, int rt, int val) {
        if(l == r) {
            sub_update(0, rt, y, 1, n, 1, val);
            return ;
        }
        int m = l+r>>1;
        if(x<=m)
            update(x, y, lson, val);
        else
            update(x, y, rson, val);
        sub_update(1, rt, y, 1, n, 1, val);
    }
    int sub_query(int pos, int L, int R, int l, int r, int rt) {
        if(L<=l&&R>=r) {
            return sum[pos][rt];
        }
        int m = l+r>>1, ret = 0;
        if(L<=m)
            ret += sub_query(pos, L, R, lson);
        if(R>m)
            ret += sub_query(pos, L, R, rson);
        return ret;
    }
    int query(int lx, int rx, int ly, int ry, int l, int r, int rt) {
        if(lx<=l&&rx>=r) {
            return sub_query(rt, ly, ry, 1, n, 1);
        }
        int m = l+r>>1, ret = 0;
        if(lx<=m)
            ret += query(lx, rx, ly, ry, lson);
        if(rx>m)
            ret += query(lx, rx, ly, ry, rson);
        return ret;
    }
    int main()
    {
        int sign, lx, ly, val, rx, ry, tmp;
        while(~scanf("%*d%d", &n)) {
            mem(sum);
            while(scanf("%d", &sign)) {
                if(sign == 3)
                    break;
                if(sign == 1) {
                    scanf("%d%d%d", &lx, &ly, &val);
                    lx++, ly++;
                    update(lx, ly, 1, n, 1, val);
                } else {
                    scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
                    lx++, ly++, rx++, ry++;
                    int ans = query(lx, rx, ly, ry, 1, n, 1);
                    cout<<ans<<endl;
                }
            }
        }
    }
  • 相关阅读:
    代腾飞(为自己名字作诗)
    愈到老愈受用:人生成功生涯规划100诀
    漫步白沫江畔有感
    成功自有成功的方法和道理(一堂价值百万的课)
    你的终点在哪里?决定成功与否的十种智力
    为你痴狂
    苏竹青(帮别人名字作诗)
    何韵仪(帮别人名字作诗)
    腹有诗书又如何
    漫步茶马古道有感
  • 原文地址:https://www.cnblogs.com/yohaha/p/5027100.html
Copyright © 2020-2023  润新知