二维树状数组

来源:互联网 发布:windows账户登录不了 编辑:程序博客网 时间:2024/05/16 07:51
#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <math.h>#define LL long longusing namespace std;const int MAXN = 1e3+10;int dp[MAXN][MAXN];int lowbit(int k){return k&-k;}void updata(int n,int x,int y,int value){    for(int i = x; i <= n; i+=lowbit(i)){        for(int j = y; j <= n; j+=lowbit(j)){            dp[i][j] += value;        }    }}int query(int x,int y){    int sum = 0;    for(int i = x; i > 0; i-=lowbit(i)){        for(int j = y; j > 0; j-=lowbit(j))            sum += dp[i][j];    }    return sum;}int main(){    int n,a;    cin>>n;    memset(dp,0,sizeof(dp));    int m;    cin>>m;    int x,y,z,t;    while(m--){        cin>>t;        if(t==1){            cin>>x>>y;            cout<<query(x,y)<<endl;        }        else{            cin>>x>>y>>z;            updata(n,x,y,z);        }    }}

0 0
原创粉丝点击