See you~(二维树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1892
See you~
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4178 Accepted Submission(s): 1317
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.
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.
For each "S" query, just print out the total number of books in that area.
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
1
3
Case 2:
1
4
#include<cstdio>
#include <iostream>
#include<cstring>
#include<algorithm> using namespace std;
#define N 1010
inline int lowbit(int i)
{
return i&(-i);
} int mp[N][N];
inline int add(int x , int y , int a)
{
for(int i = x ; i < N ;i+=lowbit(i))
for(int j = y ; j < N ;j+=lowbit(j))
mp[i][j]+=a;
}
inline int sum (int x , int y)
{
int ans = ;
for(int i = x ; i> ; i-=lowbit(i))
for(int j = y ; j> ; j-=lowbit(j))
ans+=mp[i][j];
return ans;
}
inline int Sum(int x1,int y1,int x2,int y2)
{
int ans = sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-);
return ans;
}//写成内敛函数会更快一点
inline int in_int()//输入外挂读入一个数字
{
char ch;
int res = ;
while(ch = getchar()) if(ch >= '' && ch <= '') break;
while(ch >= '' && ch <= '')
{
res = res*+ch-'';
ch = getchar();
}
return res;
} inline char in_ch()//输入外挂读入一个字符
{
char res = getchar();
while(res < 'A' || res > 'Z') res = getchar();
return res;
} void out(int x)//递归的输出外挂其实并不会快多少
{
if(x > ) out(x/);
putchar(x%+'');
}
int iniAry[N][N]; int main()
{
for(int i = ; i < N; i++)
for(int j = ; j < N; j++)
iniAry[i][j] = lowbit(i)*lowbit(j); //因为每次的初始化都是一样的,所以可以只算一次,然后memcpy()给mp;
int T;
T = in_int();
for(int cas = ; cas <= T; cas++)
{
printf("Case %d:\n", cas);
memcpy(mp, iniAry, sizeof(iniAry));
int n;
n = in_int();
char ml;
for(int i = ;i < n; i++)
{
ml = in_ch();
if(ml=='S')
{
int x1, x2,y1,y2;
x1 = in_int();
y1 = in_int();
x2 = in_int();
y2 = in_int();
x1++, y1++, x2++, y2++;
if(x1 > x2) swap(x1, x2);
if(y1 > y2) swap(y1, y2);
int sum = Sum(x1,y1,x2,y2);
out(sum);
puts("");
}
else if(ml == 'A')
{
int x , y, a;
x = in_int();
y = in_int();
x++, y++;
a = *in_int();
add(x,y,a);
}
else if(ml == 'D')
{
int x, y, m;
x = in_int();
y = in_int();
x++, y++;
m = in_int();
int tm = Sum(x, y, x, y);
if(tm < m) m = tm;
if(m > ) add(x, y, -m);
}
else if(ml =='M')
{
int x1 , x2, y1, y2, m;
x1 = in_int();
y1 = in_int();
x2 = in_int();
y2 = in_int();
x1++, y1++, x2++, y2++;
m = in_int();
int tm = Sum(x1, y1, x1, y1);
if(tm < m) m = tm;
if(m > )
{
add(x1,y1,-m);
add(x2,y2,m);
}
}
}
}
return ;
}
See you~(二维树状数组)的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- 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 ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- MooFest_二维树状数组
Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...
随机推荐
- Python学习日记:day9--------函数
初识函数 1,自定义函数 s ='内容' #自定义函数 def my_len():#自定义函数没有参数 i =0 for k in s: i+=1 print(i) return i #返回值 my_ ...
- ArcGIS API for Javascript 加载天地图(经纬度投影)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- angularjs 怎么获取鼠标焦点 鼠标移入显示浮动的div提示框
首先,我们要清楚几个基础的知识,angular的两个鼠标移入移出的指令------ng-mouseover(鼠标移入)ng-mouseleave(鼠标移出)--------还有就是window.eve ...
- Head First设计模式之策略模式
这是学习的第一个设计模式,而书中写的实例相对比较复杂,参考了网上的文章进行总结 一.定义 策略模式(strategy pattern): 定义了算法族, 分别封闭起来, 让它们之间可以互相替换, 此模 ...
- javascript进制转换
其他进制转十进制 原理 parseInt 或者 Number.parseInt 语法 parseInt(string, radix); string 必需.要被解析的字符串. radix 可选.表示要 ...
- 【Java框架型项目从入门到装逼】第八节 - 用EasyUI绘制主界面
1.引入资源包 在上一节中,我们把基本的框架都搭好了,用了Spring,SPringMVC.这一节,我们先来画页面,前端框架采用EasyUI来实现. easyui是一种基于jQuery的用户界面插件集 ...
- [Spark内核] 第33课:Spark Executor内幕彻底解密:Executor工作原理图、ExecutorBackend注册源码解密、Executor实例化内幕、Executor具体工作内幕
本課主題 Spark Executor 工作原理图 ExecutorBackend 注册源码鉴赏和 Executor 实例化内幕 Executor 具体是如何工作的 [引言部份:你希望读者看完这篇博客 ...
- FFmpeg AVPacket
AVPacket注解 AVPacket 是一个结构体,存储压缩数据.可作为编码器的输出,解码器的输入. 对于 Video 一般包含一个压缩帧,对于 Audio 可能包含多个压缩帧. 编码器允许输出空 ...
- 对《cookie之困》的一些总结与思考
0x00 前言 早些时候在gayhub翻安全大会的paper,翻到了kcon的一些paper. https://github.com/knownsec/KCon 从前辈们的paper中学到了不少,也产 ...
- [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
1:出现此种错误应该是jar版本包冲突了,启动hive的时候,由于hive依赖hadoop,启动hive,会将hadoop的配置以及jar包等等导入到hive中,导致jar包版本冲突,下面贴一下错误, ...