See you~

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4729    Accepted Submission(s): 1515

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
 
Author
Sempr|CrazyBird|hust07p43
 题意:
二维坐标平面内,在x,y>=0的点中,每一个点刚开始都有一本书,有4种操作,S求x1,y1,x2,y2,两个点的横纵坐标围成的矩形内有多少书。
代码:
 /*由于x,y从0开始,所以把每一个x,y加一,否则会陷入死循环*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t,q;
int A[][];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int c)
{
for(int i=x;i<=;i+=lowbit(i))
{
for(int j=y;j<=;j+=lowbit(j))
A[i][j]+=c;
}
}
int sum(int x,int y)
{
int s=;
for(int i=x;i>;i-=lowbit(i))
{
for(int j=y;j>;j-=lowbit(j))
s+=A[i][j];
}
return s;
}
int ans(int x1,int y1,int x2,int y2)
{
return (sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-));
}
int main()
{
int x1,y1,x2,y2,n1;
char ch;
scanf("%d",&t);
for(int k=;k<=t;k++)
{
printf("Case %d:\n",k);
memset(A,,sizeof(A));
scanf("%d",&q);
for(int i=;i<;i++)
for(int j=;j<;j++)
add(i,j,);
while(q--)
{
cin>>ch;
if(ch=='S')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2) //交换,求的是一个矩形区域所以交换后结果不会变
{
x1=x1^x2;
x2=x1^x2;
x1=x1^x2;
}
if(y1>y2)
{
y1=y1^y2;
y2=y1^y2;
y1=y1^y2;
}
printf("%d\n",ans(x1+,y1+,x2+,y2+));
}
else if(ch=='A')
{
scanf("%d%d%d",&x1,&y1,&n1);
add(x1+,y1+,n1);
}
else if(ch=='D')
{
scanf("%d%d%d",&x1,&y1,&n1);
int num=ans(x1+,y1+,x1+,y1+);
add(x1+,y1+,-(num>n1?n1:num));
}
else
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
int num=ans(x1+,y1+,x1+,y1+);
add(x1+,y1+,-(num>n1?n1:num));
add(x2+,y2+,num>n1?n1:num);
}
}
}
return ;
}

HDU1892二维树状数组的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  4. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  5. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  8. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

随机推荐

  1. loj 1167(二分+最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...

  2. Eclipse导出可执行Java工程/可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  3. POJ 2418 字典树

    题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...

  4. Color the ball(线段树)

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  5. 【转】Python yield 使用浅析

    转载地址: www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ Python yield 使用浅析 初学 Python 的开发者经 ...

  6. JavaScript 笔记 ( Prototype )

    这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...

  7. spring mvc 返回json

    服务器端返回的是文本,客户端得到文本后将文本转换成json就可以了,服务器端将对象转换成json 的文本形式,并且需要设定文本的类型为text/plain,charset=UTF-8 所以在 appl ...

  8. uva748 - Exponentiation

    import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class Exponenti ...

  9. Intellij IDEA常用快捷键——Mac版

    http://blog.csdn.net/longshen747/article/details/17204699 http://totohust.iteye.com/blog/1035550 设置自 ...

  10. Windows CMD命令大全

    http://greatverve.cnblogs.com/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机 ...