二维数组,每个位置上原来都有一本书,有如下操作:
S x1 y1 x2 y2
A x1 y1 n1D x1 y1 n1
M x1 y1 x2 y2 n1
具体实现看那代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define clr(A,k) memset(A,k,sizeof(A))
using namespace std;
const int maxn=1005;
int C[maxn][maxn],A[maxn][maxn];
int lowbit(int x){
return x&(-x);
}
void update(int x1,int y1,int n1){
for(int i=x1;i<maxn-1;i+=lowbit(i)){
for(int j=y1;j<maxn-1;j+=lowbit(j)){
C[i][j]+=n1;
}
}
}
int query(int x,int y){
int ret=0;
for(int i=x;i>0;i-=lowbit(i)){
for(int j=y;j>0;j-=lowbit(j)){
ret+=C[i][j];
}
}
return ret;
}
int main(){
int T,Q,x1,y1,x2,y2,n1;
char op[5];
cin>>T;
for(int Cas=1;Cas<=T;Cas++){
printf("Case %d:\n",Cas);
clr(C,0);
for(int i=1;i<maxn;i++)
for(int j=1;j<maxn;j++){
A[i][j]=1;
update(i,j,1);
}
cin>>Q;
while(Q--){
scanf("%s",op);
int sum=0;
if(op[0]=='S') {
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++;y1++;x2++;y2++;
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
//printf("%d %d %d %d\n",query(x1-1,y1-1),)
sum=query(x1-1,y1-1)-query(x2,y1-1)-query(x1-1,y2)+query(x2,y2);
cout<<sum<<endl;
}
if(op[0]=='A'){
scanf("%d%d%d",&x1,&y1,&n1);
update(++x1,++y1,n1);
A[x1][y1]+=n1;
}
if(op[0]=='D'){
scanf("%d%d%d",&x1,&y1,&n1);
if(A[++x1][++y1]<n1) n1=A[x1][y1];
A[x1][y1]-=n1;
update(x1,y1,-n1);
}
if(op[0]=='M'){
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
if(A[++x1][++y1]<n1) n1=A[x1][y1];
A[x1][y1]-=n1;
A[++x2][++y2]+=n1;
update(x1,y1,-n1);
update(x2,y2,n1);
}
}
}
return 0;
}