您的位置:首页 > 其它

POJ 1195-Mobile phones-二维树状数组

2016-03-15 22:18 459 查看
二维树状数组裸题

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
const int maxn =1024+10;
int c[maxn][maxn];
int N;

int lowbit(int x)
{
return x&(-x);
}

void update(int x,int y,int d)
{
for(int i=x;i<=N;i+=lowbit(i))
for(int j=y;j<=N;j+=lowbit(j))
c[i][j] += d;
}

int getsum(int x,int y)
{
int res = 0;
for(int i=x;i>0;i -= lowbit(i))
for(int j=y;j>0;j -= lowbit(j))
res += c[i][j];
return res;
}

int query(int l,int r,int u,int d)
{
return getsum(r,u) - getsum(r,d-1) - getsum(l-1,u) + getsum(l-1,d-1);
}

int main()
{
int op;
int x,y,l,r,u,d;
while(~scanf("%d",&op))
{
if(op == 0)
{
scanf("%d",&N);
memset(c,0,sizeof c);
}
else if(op == 1)
{
scanf("%d%d%d",&x,&y,&u);
update(x+1,y+1,u);
}
else if(op == 2)
{
scanf("%d%d%d%d",&l,&d,&r,&u);
printf("%d\n",query(l+1,r+1,u+1,d+1));
}
else if(op == 3)
{
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: