hdu 2642 Stars
Problem Description
Yifenfei is a romantic guy and he likes to count the stars in the sky.
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.
Input
The first line contain a M(M <= 100000), then M line followed.
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.
Output
For each query,output the number of bright stars in one line.
Sample Input
5
B 581 145
B 581 145
Q 0 600 0 200
D 581 145
Q 0 600 0 200
Sample Output
1
0
二维树状数组
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[1005][1005],c[1005][1005];
int lowbit(int x)
{
return x&(-x);
}
int Sum(int i,int j)
{ int sum=0,k,p;
for(k=i;k>0;k=k-lowbit(k))
for(p=j;p>0;p=p-lowbit(p))
sum+=c[k][p];
return sum;
}
void change(int i,int j,int x)
{ int k,p;
for(k=i;k<=1004;k=k+lowbit(k))
for(p=j;p<=1004;p=p+lowbit(p))
c[k][p]+=x;
}
int main()
{
int i,j,k,n,m,x,y;
char ch;
while(cin>>n)
{
m=n;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
while(n--)
{
cin>>ch;
if(ch=='B'||ch=='D')
{
cin>>x>>y;
x++;y++;
if(ch=='B'&&a[x][y]==0)
{
change(x,y,1);
a[x][y]=1;
} if(ch=='D'&&a[x][y]==1)
{
change(x,y,-1);
a[x][y]=0;
}
}
else
{ int x1,x2,y1,y2;
cin>>x1>>x2>>y1>>y2;
if(x1>x2)
swap(x1,x2); if(y1>y2)
swap(y1,y2);
cout<<Sum(x2+1,y2+1)+Sum(x1,y1)-Sum(x1,y2+1)-Sum(x2+1,y1)<<endl;
} }
}
return 0;
}
hdu 2642 Stars的更多相关文章
- hdu 2642 Stars 【二维树状数组】
题目 题目大意:Yifenfei是一个浪漫的人,他喜欢数天上的星星.为了使问题变得更容易,我们假设天空是一个二维平面,上面的星星有时会亮,有时会发暗.最开始,没有明亮的星星在天空中,然后将给出一些信息 ...
- hdu 1541 Stars
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...
- POJ 2352 Stars(HDU 1541 Stars)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41521 Accepted: 18100 Descripti ...
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- HDU 1541 Stars (树状数组)
Problem Description Astronomers often examine star maps where stars are represented by points on a p ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- hdu 5126 stars (四维偏序,离线,CDQ套CDQ套树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5126 思路:支持离线,那么我们可以用两次CDQ分治使四维降为二维,降成二维后排个序用树状数组维护下就好 ...
- HDU 1541 Stars (线段树)
Problem Description Astronomers often examine star maps where stars are represented by points on ...
- HDU 1589 Stars Couple(计算几何求二维平面的最近点对和最远点对)
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
随机推荐
- 棋盘上的距离 - Grids1657
棋盘上的距离 问题描述: 写一个程序,给定起始位置和目标位置,计算王.后.车.象从起始位置走到目标位置所需的最少步数. 王:横.直.斜都可以走,但每步限走一格. 后:横.直.斜都可以走,每步格数不受限 ...
- WF学习
1.添加argument 类的argument必须先在表达式里面实例化 argument和variable 没有区别啊??????? http://msdn.microsoft.com/en-us/l ...
- mac下 配置tomcat
第一步: 1.打开你的终端:然后输入 pico .bash_profile 回车 第二步: 2. 然后添加你tomcat放入的路径的path 编辑完后,control+x (保存) 继 ...
- Spring ioc 原理
java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用他的合作对象时,自己均要使用像new object() 这样的语法来完成合作对象的申请工作.你 ...
- vi编辑器经典技巧 -备
a)vi编辑器 (visual Interface简称) Linux常用,输出,删除,查找,替换,块操作,定制 b) vim编辑器 (vi IMproved简写)是vi增强版本,在vi上增加了很多功能 ...
- 【转】const的用法,特别是用在函数前面与后面的区别!
在普通的非 const成员函数中,this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const成员函数中,this的类型是一个指向 ...
- FJ省队集训DAY5 T1
思路:考试的时候打了LCT,自以为能过,没想到只能过80.. 考完一想:lct的做法点数是100W,就算是nlogn也会T. 讲一下lct的做法把:首先如果一条边连接的两个点都在同一个联通块内,那么这 ...
- 【转】 boot.img的解包与打包
原文网址:http://blog.csdn.net/wh_19910525/article/details/8200372 Android 产品中,内核格式是Linux标准的zImage,根文件系统采 ...
- tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式
tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式 https://github.com/zhangzheyuk/CaseConvert
- Maven .m2 setting.xml配置
settings.xml <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="h ...