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 }

原文地址:https://www.cnblogs.com/fzl194/p/8955103.html

时间: 2024-08-28 03:41:32

hdu-1892 See you~---二维树状数组运用的相关文章

HDU 1892 See you~ (二维树状数组)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 See you~ Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say so

hdu 5517 Triple(二维树状数组)

HDU 5517 题目:给出一种有三个元素的顺序,这个数据定义一种偏序,当且仅当一个三元数的所有元素都大于等于另一个时这个数比另一个大.求这个一个集合中不比任何数小的三元数有多少个. 思路:首先排序去重,此时第一维有序,不用考虑,然后只有不存在比当前数的另外两维都大的数时当前数是可选的,用二位树装数组维护这个信息即可. /* * @author: Cwind */ #include <bits/stdc++.h> using namespace std; #define IOS std::io

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

hdu 5517 Triple(二维树状数组)

题目链接:hdu 5517 Triple 题意: 有n个两元组A,有m个三元组B,然后set C有一个计算方式. 现在让你找set TOP的size. 题解: 理解题意后,显然对于每个b的分组,只有最大的a才有贡献, 然后就可以发现set B中每个元素按照e分组后,只会对应一个a,所以最多有1e5个三元组可能有贡献. 然后将这个三元组排一下序,用二维树状数组搞搞就行了. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i&l

hdu 4456 Crowd(二维树状数组)

题目链接:hdu 4456 Crowd 题目大意:给定N,然后M次操作 1 x y z:在x,y的位置加z 2 x y z:询问与x,y曼哈顿距离小于z的点值和. 解题思路:将矩阵旋转45度,然后询问就等于是询问一个矩形,可以用容斥定理搞,维护用二维树状数组,但是空间开 不下,直接用离散化,将有用到的点处理出来. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

hdoj 1892(二维树状数组)

Problem H Time Limit : 5000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Now I am leaving hust acm. In

HDU_4456_二维树状数组

http://acm.hdu.edu.cn/showproblem.php?pid=4456 第一道二维树状数组就这么麻烦,题目要计算的是一个菱形范围内的和,于是可以把原来的坐标系旋转45度,就是求一个正方形范围内的和,这里还涉及到坐标系平移和放大,由于题目数据较大,用了离散化来保存需要处理的点,放在h数组内,对应的二维树状数组存在tree数组内. #include<iostream> #include<cstring> #include<cstdio> #includ

hdu6078 Wavel Sequence dp+二维树状数组

//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:给定a序列和b序列.从a中取一个子序列x(数的位置顺序保持不变),子序列每个数满足a1<a2>a3<a4>a5<a6... 波浪形 从b中取相同长度的子序列y,也满足波浪形. 如果x

HDU1559 最大子矩阵 (二维树状数组)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2901    Accepted Submission(s): 1454 Problem Description 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使