标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1628 Accepted Submission(s):
683
void add(int k,int x) { for(int i=k;i<MAXN;i+=lowbit(i)) c[i]+=x; }
lowbit():
int lowbit(int x) //取最低位 { return x&(-x); }
查询:
int get_sum(int k) { int res=0; for(int i=k;i>0;i-=lowbit(i)) res+=c[i]; return res; }
这道题是一道二维树状数组,原理其实也就是这样。
注意:题目中坐标从0开始,可能对一颗star做两次同样的操作。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<stdlib.h> #include<stack> #include<vector> using namespace std; const int MAXN=1010; int a[MAXN][MAXN]; bool b[MAXN][MAXN]; int lowbit(int x) { return x&(-x); } void modify(int x,int y,int data) { for(int i=x; i<MAXN; i+=lowbit(i)) for(int j=y; j<MAXN; j+=lowbit(j)) a[i][j]+=data; } 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+=a[i][j]; return res; } int main() { int n,x,y,x1,y1; char str[2]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); scanf("%d",&n); while(n--) { scanf("%s",str); if(str[0]==‘B‘) { scanf("%d%d",&x,&y); x++; y++; if(b[x][y]) continue; modify(x,y,1); b[x][y]=1; } else if(str[0]==‘D‘) { scanf("%d%d",&x,&y); x++; y++; if(b[x][y]==0) continue; modify(x,y,-1); b[x][y]=0; } else { scanf("%d%d%d%d",&x,&x1,&y,&y1); x++;x1++;y++;y1++; if(x>x1) swap(x,x1); if(y>y1) swap(y,y1); int ans=getsum(x1,y1)-getsum(x-1,y1)-getsum(x1,y-1)+getsum(x-1,y-1); printf("%d\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5736166.html