hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 785 Accepted Submission(s): 335
To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where 'B' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region correspond X1,X2,Y1,Y2.
There is only one case.
each line start with a operational character.
if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.
B 581 145
B 581 145
Q 0 600 0 200
D 581 145
Q 0 600 0 200
0
#include<stdio.h>
#include<string.h>
const int N=1003;
int c[N+5][N+5],flag[N+5][N+5],n,m;
int mmax(int a,int b)
{
return a>b?a:b;
}
int lowbit(int x)
{
return x&(-x);
} void update(int x,int y,int delta )
{
int i, j;
for(i=x;i<=N;i+=lowbit(i))
{
for(j=y; j<=N; j+=lowbit(j))
{
c[i][j] += delta;
}
}
} int sum( int x, int y )
{
int res=0,i,j;
for(i=x;i>0;i-=lowbit(i))
{
for(j=y; j>0; j-=lowbit(j))
{
res += c[i][j];
}
}
return res;
} int main()
{
int x1,x2,y1,y2; while(scanf("%d",&m)!=EOF)
{
memset(c,0,sizeof(c));
memset(flag,0,sizeof(flag));
while(m--)
{
char s[2];
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
x1++;y1++;x2++;y2++;
if(x1>x2) {int temp=x1;x1=x2;x2=temp;}
if(y1>y2) {int temp=y1;y1=y2;y2=temp;}
int ans=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1);
printf("%d\n",ans);
}
else if(s[0]=='B')
{
scanf("%d %d",&x1,&y1);
x1++;y1++;
if(flag[x1][y1]==1) continue;
flag[x1][y1]=1;
update(x1,y1,1);
}
else
{
scanf("%d %d",&x1,&y1);
x1++;y1++;
if(flag[x1][y1]==0) continue;
flag[x1][y1]=0;
update(x1,y1,-1);
}
}
}
return 0;
}
hdu 2642 二维树状数组 单点更新区间查询 模板水题的更多相关文章
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- TZOJ 2725 See you~(二维树状数组单点更新区间查询)
描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...
- hdu2642二维树状数组单点更新+区间查询
http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意:一个星空,二维的.上面有1000*1000的格点,每个格点上有星星在闪烁.一开始时星星全部暗淡着 ...
- 【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)
试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡 ...
- SPOJ - MATSUM 二维树状数组单点更新
忘记了单点更新时要在树状数组中减去原值..wa了一发 /* 矩形求和,单点更改 */ #include<iostream> #include<cstring> #include ...
- hdu2642二维树状数组单点更新
碰到这种题一定要注意坐标是不是有序的,也要注意坐标是不是有0的,有的话需要+1处理 #include<bits/stdc++.h> using namespace std; #define ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
随机推荐
- C# LDAP认证登录
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...
- 一种根据URL参数条件动态生成URL的方法
最近做了一个产品列表页类似于搜索列表页, 功能比较简单,比搜索页复杂的逻辑在于,生成各个查询条件的URL.我们的链接如下: http://xxx.xxx.xxx/product/list.html?s ...
- C++基础之二:常量指针和指针常量
1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; 注:可以将一个常量的地址赋值给一个对应类型的常 ...
- mysql 父子结构排序
项目中常常会遇到父子结构显示的问题,不同的数据库有不同的写的方式,比方SqlServer中用with union 实现.而Mysql则没有这么方便的语句. 例如以下category表.食品有pizaa ...
- Java总结之容器
[容器的概念] 容器:Java API所提供的一系列的实例,用于在程序中存放对象. [容器 API] J2SDK所提供的容器API位于java.util包内. {Collection[Set(Hash ...
- 可以根据柜子内表取出所有的柜子信息的BAPI函数
DATA: gt_hunumbers TYPE STANDARD TABLE OF bapihunumber, gt_huitem TYPE STANDARD TABLE OF bapih ...
- delphi 网页提交按钮执行点击事件
遍历即可实现,下列代码仅供参考: var i: integer; T: OleVariant; begin T := WebBrowser1.Document; do begin if T.all.i ...
- Jquery发送ajax请求以及datatype参数为text/JSON方式
Jquery发送ajax请求以及datatype参数为text/JSON方式 1.方式一:datatype:'text' 2.方式二:datatype:'JSON' 3.使用gson-1.5.jar包 ...
- OCA读书笔记(7) - 管理数据库存储结构
7.Managing Database Storage Structures 逻辑结构 数据库的存储结构有物理结构和逻辑结构组成的 物理结构:物理上,oracle是由一些操作系统文件组成的 SQL&g ...
- 排列-条件求和(Code)
static void Main(string[] args) { // Generate data int arraySize; int[] data; Random rnd; arraySize ...