poj1195二维树状数组+单点修改区间查询

http://poj.org/problem?id=1195

最后减的时候注意一下 还要加上被多减的部分

#include
using namespace std;
typedef long long ll;
const int N = 1e3+100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
ll a[N][N],n;
int lowbit(int k){
    return k&-k;
}
void add(int x,int y,int v){
    for(int i = x;i <= n;i += lowbit(i)){
        for(int j = y;j <= n;j += lowbit(j)){
            a[i][j] += v;
        }
    }
}
ll query(int x,int y){
    ll ans = 0;
    for(int i = x;i;i -= lowbit(i)){
        for(int j = y;j;j -= lowbit(j)){
            ans += a[i][j];
        }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int c;
    while(cin >> c && c < 3){
        if(c == 1){
            int x,y,v;
            cin >> x >> y >> v;
            add(++x,++y,v);
        }else if(c == 2){
            int l1,l2,r1,r2;
            cin >> l1 >> r1 >> l2 >> r2;
            ++l1,++r1,++l2,++r2;
            cout << query(l2,r2) - query(l2,r1-1) - query(l1-1,r2) + query(l1-1,r1-1) << '\n';
        }else cin >> n;
    }
    return 0;
}

 

你可能感兴趣的:(POJ,数据结构-树状数组)