hdu-1892 See you~---二维树状数组运用
Posted 努力努力再努力x
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-1892 See you~---二维树状数组运用相关的知识,希望对你有一定的参考价值。
题目链接:
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~---二维树状数组运用的主要内容,如果未能解决你的问题,请参考以下文章