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 sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanced to the World Finals last year.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.
Input
In the first line of the input file there is an Integer T(1<=T<=10), which means the number of test cases in the input file. Then N test cases are followed.
For each test case, in the first line there is an Integer Q(1<Q<=100,000), means the queries of the case. Then followed by Q queries.
There are 4 kind of queries, sum, add, delete and move.
For example:
S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including the two points.
A x1 y1 n1 means I put n1 books on the position (x1,y1)
D x1 y1 n1 means I move away n1 books on the position (x1,y1), if less than n1 books at that position, move away all of them.
M x1 y1 x2 y2 n1 means you move n1 books from (x1,y1) to (x2,y2), if less than n1 books at that position, move away all of them.
Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
Output
At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries.
For each "S" query, just print out the total number of books in that area.
Sample Input
2
3
S 1 1 1 1
A 1 1 2
S 1 1 1 1
3
S 1 1 1 1
A 1 1 2
S 1 1 1 2
Sample Output
Case 1:
1
3
Case 2:
1
4
/*AC代码*/
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; const int size = ;
int a[size+][size+]; int lowbit(int i)
{
return i & (-i);
} void add(int x, int y, int num)
{
for(int i = x; i <= size; i += lowbit(i))
for(int j = y; j <= size; j += lowbit(j))
a[i][j] += num;
} int getSum(int x, int y)
{
int tot = ;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j -= lowbit(j))
tot += a[i][j];
return tot;
} void init()
{
memset(a, , sizeof(a));
for(int i = ; i < size; ++i)
for(int j = ; j < size; ++j)
add(i, j, );
} int main()
{
int T, n, x1, x2, y1, y2, n1;
char cmd[];
scanf("%d", &T);
for(int cnt = ; cnt <= T; ++cnt)
{
init();
scanf("%d", &n);
printf("Case %d:\n", cnt);
while(n--)
{
scanf("%s", cmd);
if(cmd[] == 'A')
{
scanf("%d%d%d", &x1, &y1, &n1);
add(x1+, y1+, n1);
}
else if(cmd[] == 'S')
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
++x1;
++x2;
++y1;
++y2;
if(x1 > x2)
swap(x1, x2);
if(y1 > y2)
swap(y1, y2);
int temp;
temp=getSum(x2,y2)-getSum(x1-,y2)-getSum(x2,y1-)+getSum(x1-,y1-);
printf("%d\n",temp);
}
else if(cmd[] == 'D')
{
scanf("%d%d%d",&x1,&y1,&n1);
++x1;
++y1;
int temp;
temp=getSum(x1,y1)-getSum(x1-,y1)-getSum(x1,y1-)+getSum(x1-,y1-);
if(n1 > temp)
n1 = temp;
add(x1, y1, -n1);
}
else if(cmd[] == 'M')
{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n1);
++x1;
++x2;
++y1;
++y2;
int temp;
temp=getSum(x1,y1)-getSum(x1-,y1)-getSum(x1,y1-)+getSum(x1-,y1-);
if(n1 > temp)
n1 = temp;
add(x1, y1, -n1);
add(x2, y2, n1);
}
}
}
return ;
}
HDU 1892 See you~ (二维树状数组)的更多相关文章
- HDU 1892(书架统计 二维树状数组)
题意是在二维平面上在一些位置上进行数据的增删改查操作,使用树状数组(了解树状数组点这里) 原来的树状数组在求区间和时是 sum( x, y ) = getsum( y ) - getsum( x - ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- hdu 2642 Stars 【二维树状数组】
题目 题目大意:Yifenfei是一个浪漫的人,他喜欢数天上的星星.为了使问题变得更容易,我们假设天空是一个二维平面,上面的星星有时会亮,有时会发暗.最开始,没有明亮的星星在天空中,然后将给出一些信息 ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- HDU 5517 【二维树状数组///三维偏序问题】
题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...
- HDU 5465 Clarke and puzzle Nim游戏+二维树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 26 ...
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
随机推荐
- JAVA对MySQL数据库的操作
一.导包: 使用JDBC连接MySQL数据库时,首先需要导入一个第三方的JAR包(点击下载),下载解压得到一个JAR包,并导入到JAVA项目中,如下图: 二.DBHelper类: 代码如下: impo ...
- Java牛人
Java领域有很多著名的人物,他们为Java社区编写框架.产品.工具或撰写书籍改变了Java编程的方式.本文是<最受欢迎的8位Java牛人>的2.0版本. PS:排名不分先后.本文的信息整 ...
- java.lang.NoSuchFieldError 异常原因
一般都是因为 class 或 jar 包重复 导致的 , 也有可能是编译器的问题. 我碰到的问题是,在项目api 接口jar包里定义了一个Config.java,然后在业务层service 项目 的相 ...
- java后端制作MD5加密
由于一次业务的需要,我制作了一次密码的修改子业务. 当用户忘记密码的情况下,我需要动态的发给他一个6位的随机密码,通过即时通,短信,微信等.并同时修改数据库中的原密码为这6位的随机密码.让用户再去修改 ...
- jquery版小型婚礼(可动态添加祝福语)
前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久……一直没找到满意的……无意间看到祝福语和一些卡通婚礼图片.最 ...
- Fedora中允许mysql远程访问的几种方式
Fedora中允许mysql远程访问,可以使用以下两种方式:a.改表. mysql>use mysql; mysql>update user set host = '%' where us ...
- Python复习之下划线的含义
__xx__ 系统定义名字 __xx 双下划线的表示的是私有类型的变量.只能是允许这个类本身进行访问了.连子类也不可以 _xx 单下划线 不能用'from moduleimport *'导入 即保护类 ...
- destoon去掉会员注册email验证
修改文件: /module/member/member.class.php 删除61行: //if(!is_email($email)) return $this->_($L['member_e ...
- VIM下的跳转练习
在vim下可以使用常用的箭头键 但是 还有其它键可以让你更快的达到目标 hjkl 这是代替箭头键功能的 H M L 跳到屏幕的顶上 中间 下方 w 跳到下一个单词的开始e 跳到单词的结束b 向后跳 g ...
- Windows10+Ubuntu双系统安装 (转)
1.Windows10+Ubuntu双系统安装: http://www.jianshu.com/p/2eebd6ad284d 2.UEFI启动模式安装ubuntu指南 : http://col ...