【题目描述】:
坦佩雷地区的第四代移动电话基站工作如下。这个地区是一个正方形。正方形被分成S×S格,其中行和列编号从0到S-1。每个方块都包含一个基站。由于手机从一格移动到另一格或电话接通或断开,广场内活动手机的数量会发生变化。每个基站会不时向主基站报告活动电话的数量。
编写一个程序,它接收这些报告并回答关于任何矩形形状区域中当前活动电话总数的查询。
报告次数M次形式如下,第一个数是报告类型,后面跟着报告的数据:
0 S:表示坦佩雷地区被分成全0的S×S个方块。该报告只会是第一条。
1 X Y A:表示第X行Y列的基站增加A个电话(可能是负数)。
2 L B R T:表示询问左上(L,B)、右下(R,T)矩形区间在用电话的数量。
3:终止程序,将是最后一条报告。
备注:每个基站不会出现小于0个或多于32767个移动电话。
【输入描述】:
输入多行每行一个报告;
【输出描述】:
输出多行,每行对于每个2类型的报告予以答复。
【样例输入】:
0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3
【样例输出】:
3
4
【时间限制、数据范围及描述】:
时间:1s 空间:128M
对于 30%的数据:m<=50;
对于 70%的数据:m<=4000;
对于 100%的数据:1<=S<=1024; 3<=M<=60002;
对于所有的数据:-32768<=A<=32767;全地区电话数量不超过2^30;
对于所有的数据:L<=R; B<=T;
【解题思路】:
二维树状数组裸题
【AC代码】:
#include<bits/stdc++.h>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define Mod 100003
using namespace std;
inline void read(int &x){
char ch=getchar(),c=ch;
x=0;
while(ch<'0' || ch>'9'){
c=ch;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
if(c=='-')x=-x;
}
int c[1025][1025];
int n,i,j;
int lowbit(int x)
{
return x & -x;
}
void update(int x, int y, int delta){
for(int i=x;i<=n; ){
for(int j=y;j<=n;){
c[i][j]+=delta;
j=j+lowbit(j);
}
i=i+lowbit(i);
}
}
int sum(int x, int y){
int anss=0;
for (int i=x;i>0;){
for (int j=y;j>0;){
anss+=c[i][j];
j=j-lowbit(j);
}
i=i-lowbit(i);
}
return anss;
}
int main(){
int ins;
while (1){
read(ins);
if (ins==3)break;
if (ins==0){
read(n);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
c[i][j]=0;
continue;
}
if (ins == 1){
int x, y, A;
read(x),read(y),read(A);
update(x+1,y+1,A);
}
else if (ins == 2){
int x1, y1, x2, y2;
read(x1),read(y1),read(x2),read(y2);
int result;
result=sum(x2+1,y2+1)+sum(x1, y1)-sum(x1,y2+1)-sum(x2+1,y1);
cout<<result<<endl;
}
}
return 0;
}