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( ...
随机推荐
- HTML&CSS基础学习笔记1.4-定义文档类型
Web 世界中存在许多不同的文档.只有了解文档的类型,浏览器才能正确地显示文档. HTML 也有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 HTML 页面 ...
- 解决ie6显示透明图的问题
在我们设置png透明图片时,其他浏览器都显示很正常,唯独只有ie6看着不是透明的状态. 第一种办法是:单独设置ie6的样式.例: _background: none; _filter:progid:D ...
- Caffe : Layer Catalogue(1)
原文:http://caffe.berkeleyvision.org/tutorial/layers.html 参考:http://blog.csdn.net/u011762313/article/d ...
- chord原理的解读
chord: A Scalable Peer-to-peer Lookup Service for Internet Application 在 P2P 系统中,有效地定位分布在网络中不同节点的数据资 ...
- github进行修改
1)git status:可以让我们时刻掌握仓库当前的状态. 2)git diff [文件名]:查看改变的详细信息,显示的结果是Unix通用的diff格式. 步骤: 1.修改文件 2.通过git ad ...
- 关于DDMS查看Data文件夹
真机是无法查看的,只有通过模拟器查看
- Why Memory Barrier?
引言:xchg做了什么? 首先,xchg eax, ecx并不会比mov edx, eax + mov eax, ecx + mov ecx, edx这三条指令加一起快,原因是xchg有副作用. Mi ...
- 响应头location 页面跳转
登陆:http://192.168.32.161/DEVOPS/index.php/Index/do_login Cache-Control no-store, no-cache, must-reva ...
- 浅谈C++中指针和引用的区别者之间的区别和用法(转)
指针和引用在C++中很常用,但是对于它们之间的区别很多初学者都不是太熟悉,下面来谈谈他们2者之间的区别和用法. 1.指针和引用的定义和性质区别: (1)指针:指针是一个变量,只不过这个变量存储的是一个 ...
- <PHP>字符串处理代码
字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串 ...