HDU1892二维树状数组

时间:2021-11-22 17:16:11

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 &lbrack;JSOI2009&rsqb;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&colon;Mobile phones(二维树状数组,矩阵求和)

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

  5. Codeforces Round &num;198 &lpar;Div&period; 1&rpar; D&period; Iahub and Xors 二维树状数组&ast;

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

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

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

  7. &lbrack;poj2155&rsqb;Matrix&lpar;二维树状数组&rpar;

    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. &lbrack;POJ2155&rsqb;Matrix(二维树状数组)

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

随机推荐

  1. 学完了js的知识,一起分享总结知识点

    又一个知识点学完了,到了总结学习效果和知识总结的时间了.js这个编程语言相对于html和css的逻辑性要强一些,也比较不容易上手.概念性的知识点不难理解,就是实际的操作并不容易,需要通过学习和借鉴案列 ...

  2. 关于easyui的窗口和tab页面不执行js说明

    一直以来群里里面很多人反应,在用tab加载界面的时候,界面里面的js不会执行.今天GodSon在此说明一下原因. 不管是window,dailog还是tab其实质最终都是继承了panel.panel有 ...

  3. c&num; 语法5&period;0 新特性 转自网络

    本专题概要: 引言 同步代码存在的问题 传统的异步编程改善程序的响应 C# 5.0 提供的async和await使异步编程更简单  async和await关键字剖析 小结 一.引言 在之前的C#基础知 ...

  4. Log4j各级别日志重复打印的问题

    今天在配置Log4j日志的时候,发现日志重复打印的问题.网上查了很多资料,发现介绍Log4j配置的文章数量不少,但提到这个问题的文章却寥寥,解决了自己的问题以后,赶紧记录一下. 原文地址:http:/ ...

  5. pdf&period;js的使用

    下载地址:  files.cnblogs.com/zycjwdss/mypdf.zip 把下载后的zip解压,放到web服务器根目录下,打开pdf.html,把这一句取消注释: //window.pd ...

  6. answerOpenCV轮廓类问题解析

    contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大.以contour为关键字,在answerOpenCV中能够发现很多有趣的东西. 1.无法解决的问题 http://answe ...

  7. jQuery链式语法演示

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Thunder团队--Beta发布用户使用报告

    Thunder爱阅app Beta 发布用户使用报告 用户数量:14人 以下为用户评论:(注:为了保护用户的姓名权,以下用户名以昵称形式给出.) 序号 昵称 个人信息 获得软件途径 使用次数 用户评论 ...

  9. 从零开始的全栈工程师——js篇(作用域 this 原型笔试题练习)

    作用域 // 1. fn() function fn () { console.log(12) } var as = function () { console.log(45) } // 2. var ...

  10. MATLAB循环结构:break&plus;continue&plus;嵌套

    break语句:终止当前循环,继续执行循环语句的下一语句: continue语句:跳过循环体的后面语句,开始下一个循环: 例:求[100,200]之间第一个能被21整除的整数 :200 %循环语句 ) ...