BZOJ 1452: [JSOI2009]Count 二维树状数组
1452: [JSOI2009]Count
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=1452Description
Input
Output
Sample Input
![](http://www.lydsy.com/JudgeOnline/images/1452_4.jpg)
Sample Output
1
2
HINT
题意
题解:
裸的二维树状数组
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 301 #define mod 1001 #define eps 1e-9 #define pi 3.1415926 int Num; //const int inf=0x7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* int S[101][maxn][maxn]; int col[maxn][maxn]; int n,m; int lowbit(int x){return x&(-x);} void updata(int c[maxn][maxn],int x,int y,int w) { for(int i=x;i<=n;i+=lowbit(i)) for(int j=y;j<=m;j+=lowbit(j)) c[i][j]+=w; } int sum(int c[maxn][maxn],int x,int y) { int ans=0; for(int i=x;i;i-=lowbit(i)) for(int j=y;j;j-=lowbit(j)) ans+=c[i][j]; return ans; } int solve(int c[maxn][maxn],int x1,int y1,int x2,int y2) { return sum(c,x2,y2)-sum(c,x2,y1-1)-sum(c,x1-1,y2)+sum(c,x1-1,y1-1); } int main() { n=read(),m=read(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { int x=read(); col[i][j]=x; updata(S[x],i,j,1); } } int q=read(); while(q--) { int op=read(); if(op==1) { int x=read(),y=read(),c=read(); updata(S[col[x][y]],x,y,-1); col[x][y]=c; updata(S[col[x][y]],x,y,1); } else { int x1=read(),x2=read(),y1=read(),y2=read(),c=read(); printf("%d\n",solve(S[c],x1,y1,x2,y2)); } } }