1. 程式人生 > >hdu-1892 See you~---二維樹狀數組運用

hdu-1892 See you~---二維樹狀數組運用

sstream int d+ string \n pri scan HA else

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=1892

題目大意:

題目大意:有很多方格,每個方格對應的坐標為(I,J),剛開始時每個格子裏有1本書,然後讓你統計一片區域有多少本書,還可以增加書和減少,移動書。

解題思路:

直接二維數組數組模擬

註意:

每個下標+1,從(1, 1)開始

求區域和的時候給出的x1 y1 和x2 y2不是標準的正對角線,需要轉化

  1  #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5
#include<string> 6 #include<cmath> 7 #include<set> 8 #include<queue> 9 #include<map> 10 #include<stack> 11 #include<vector> 12 #include<list> 13 #include<deque> 14 #include<sstream> 15 #include<cctype> 16 #define REP(i, n) for(int i = 0; i < (n); i++) 17
#define FOR(i, s, t) for(int i = (s); i < (t); i++) 18 #define MEM(a, x) memset(a, x, sizeof(a)); 19 using namespace std; 20 typedef long long ll; 21 typedef unsigned long long ull; 22 const int maxn = 1010; 23 const double eps = 1e-10; 24 const int INF = 1 << 30; 25 const int dir[4][2] = {1
,0,0,1,0,-1,-1,0}; 26 const double pi = 3.1415926535898; 27 int T, n, m, cases; 28 int tree[maxn][maxn], a[maxn][maxn]; 29 int lowbit(int x) 30 { 31 return x&(-x); 32 } 33 int sum(int x, int y) 34 { 35 int ans = 0; 36 for(int i = x; i > 0; i -= lowbit(i)) 37 for(int j = y; j > 0; j -= lowbit(j)) 38 ans += tree[i][j]; 39 return ans; 40 } 41 void add(int x, int y, int d) 42 { 43 for(int i = x; i < maxn; i += lowbit(i)) 44 { 45 for(int j = y; j < maxn; j += lowbit(j)) 46 { 47 tree[i][j] += d; 48 } 49 } 50 } 51 int main() 52 { 53 scanf("%d", &T); 54 char s[3]; 55 int x, y, x1, y1, x2, y2, d; 56 while(T--) 57 { 58 scanf("%d", &n); 59 MEM(tree, 0); 60 for(int i = 1; i < maxn; i++) 61 { 62 for(int j = 1; j < maxn; j++) 63 { 64 add(i, j, 1); 65 a[i][j] = 1; 66 } 67 } 68 printf("Case %d:\n", ++cases); 69 while(n--) 70 { 71 scanf("%s", s); 72 if(s[0] == S) 73 { 74 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 75 ///不是標準的x1<x2, y1<y2,要對其進行轉化 76 int a, b, c, d; 77 a = min(x1, x2);a++; 78 b = min(y1, y2);b++; 79 c = max(x1, x2);c++; 80 d = max(y1, y2);d++; 81 printf("%d\n", sum(c, d) + sum(a - 1, b - 1) - sum(a - 1, d) - sum(c, b - 1)); 82 } 83 else if(s[0] == A) 84 { 85 scanf("%d%d%d", &x, &y, &d); 86 x++, y++; 87 add(x, y, d); 88 a[x][y] += d; 89 } 90 else if(s[0] == D) 91 { 92 scanf("%d%d%d", &x, &y, &d); 93 x++, y++; 94 if(d > a[x][y])d = a[x][y]; 95 add(x, y, -d); 96 a[x][y] -= d; 97 } 98 else 99 { 100 scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &d); 101 x1++, y1++, x2++, y2++; 102 if(d > a[x1][y1])d = a[x1][y1]; 103 add(x1, y1, -d); 104 add(x2, y2, d); 105 a[x1][y1] -= d; 106 a[x2][y2] += d; 107 } 108 } 109 } 110 return 0; 111 }

hdu-1892 See you~---二維樹狀數組運用