标签:
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4768 Accepted Submission(s): 1521
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<queue> 6 #include<string.h> 7 #include<map> 8 #include<vector> 9 #include<queue> 10 using namespace std; 11 typedef long long LL; 12 int ma[1005][1005]; 13 int bit[1005][1005]; 14 int cit[1005][1005]; 15 int lowbit(int x); 16 void add(int x,int y,int c,int v); 17 int ask(int x,int y); 18 int main(void) 19 { 20 int n; 21 scanf("%d",&n); 22 int __ca = 0; 23 int i,j; 24 for(i = 1; i <= 1001; i++) 25 { 26 for(j = 1; j <= 1001; j++) 27 { 28 ma[i][j] = 1; 29 add(i,j,1,0); 30 } 31 } 32 while(n--) 33 { 34 memset(bit,0,sizeof(bit)); 35 int m; 36 for(i = 0; i <= 1001; i++) 37 { 38 for(j = 0; j <= 1001; j++) 39 { 40 bit[i][j] = cit[i][j]; 41 } 42 } 43 for(i = 0; i <= 1001; i++) 44 { 45 for(j = 0; j <= 1001; j++) 46 { 47 ma[i][j] = 1; 48 } 49 } 50 char ans[10]; 51 scanf("%d",&m); 52 printf("Case %d:\n",++__ca); 53 while(m--) 54 { 55 scanf("%s",ans); 56 int x,y,x1,y1; 57 if(ans[0]==‘S‘) 58 { 59 scanf("%d %d %d %d",&x,&y,&x1,&y1); 60 if(x > x1) 61 swap(x,x1),swap(y,y1); 62 if(y > y1) 63 { 64 swap(y,y1); 65 } 66 x++; 67 y++; 68 x1++; 69 y1++; 70 int sum = ask(x1,y1); 71 sum -= ask(x-1,y1); 72 sum -= ask(x1,y-1); 73 sum += ask(x-1,y-1); 74 printf("%d\n",sum); 75 } 76 else if(ans[0] == ‘A‘) 77 { 78 int c; 79 scanf("%d %d %d",&x,&y,&c); 80 x++; 81 y++; 82 ma[x][y] += c; 83 add(x,y,c,1); 84 } 85 else if(ans[0] == ‘M‘) 86 { 87 int c; 88 scanf("%d %d %d %d %d",&x,&y,&x1,&y1,&c); 89 x++; 90 y++; 91 x1++; 92 y1++; 93 if(ma[x][y] < c) 94 c = ma[x][y]; 95 ma[x][y] -= c; 96 ma[x1][y1]+=c; 97 add(x,y,-c,1); 98 add(x1,y1,c,1); 99 } 100 else 101 { 102 int c; 103 scanf("%d %d %d",&x,&y,&c); 104 x++; 105 y++; 106 if(ma[x][y] < c) 107 c = ma[x][y]; 108 ma[x][y] -= c; 109 add(x,y,-c,1); 110 } 111 } 112 } 113 return 0; 114 } 115 int lowbit(int x) 116 { 117 return x&(-x); 118 } 119 void add(int x,int y,int c,int v) 120 { 121 int i,j; 122 for(i = x; i <= 1001; i += lowbit(i)) 123 { 124 for(j = y; j <= 1001; j += lowbit(j)) 125 { 126 if(v) 127 bit[i][j] += c; 128 else cit[i][j]+=c; 129 } 130 } 131 } 132 int ask(int x,int y) 133 { 134 int i,j; 135 int sum = 0; 136 for(i = x; i > 0; i -= lowbit(i)) 137 { 138 for(j = y; j > 0; j -= lowbit(j)) 139 { 140 sum += bit[i][j]; 141 } 142 } 143 return sum; 144 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5903520.html