1452: [JSOI2009]Count
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2083 Solved: 1228
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
Sample Output
1
2
2
HINT
思路:二维树状数组;
将数组开三维的然后,每一维维护一个值,然后直接二维树状数组维护即可;
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<queue> 6 #include<string.h> 7 using namespace std; 8 int bit[105][305][305]; 9 int ans[305][305]; 10 int lowbit(int x) 11 { 12 return x&(-x); 13 } 14 void add(int x,int y,int val,int op) 15 { 16 int i,j; 17 for(i = x; i < 305; i += lowbit(i)) 18 { 19 for(j = y; j < 305; j += lowbit(j)) 20 { 21 if(op) 22 bit[val][i][j]++; 23 else bit[val][i][j]--; 24 } 25 } 26 } 27 int ask(int x,int y,int val) 28 { 29 int i,j; 30 int sum = 0; 31 for(i = x; i > 0; i -= lowbit(i)) 32 { 33 for(j = y; j > 0; j -= lowbit(j)) 34 { 35 sum += bit[val][i][j]; 36 } 37 } 38 return sum; 39 } 40 int main(void) 41 { 42 int n,m; 43 scanf("%d %d",&n,&m); 44 int i,j; 45 for(i = 1; i <= n; i++) 46 { 47 for(j = 1; j <= m; j++) 48 { 49 int x; 50 scanf("%d",&x); 51 ans[i][j] = x; 52 add(i,j,x,1); 53 } 54 } 55 int T; 56 scanf("%d",&T); 57 while(T--) 58 { 59 int val; 60 scanf("%d",&val); 61 if(val == 1) 62 { 63 int xx,yy,c; 64 scanf("%d %d %d",&xx,&yy,&c); 65 add(xx,yy,ans[xx][yy],0); 66 ans[xx][yy] = c; 67 add(xx,yy,ans[xx][yy],1); 68 } 69 else 70 { 71 int x,y,x1,y1,c; 72 scanf("%d %d %d %d %d",&x,&x1,&y,&y1,&c); 73 int sum = ask(x1,y1,c); 74 sum -= ask(x-1,y1,c); 75 sum -= ask(x1,y-1,c); 76 sum += ask(x-1,y-1,c); 77 printf("%d ",sum); 78 } 79 } 80 return 0; 81 }