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( ...
随机推荐
- IDirect3DDevice9::Clear
在绘制每一帧图形前都要先清空视区,即清空渲染目标表面上的视区矩形的内容:颜色缓冲区.深度缓冲区或者模板缓冲区. HRESULT Clear( [in] DWORD Count, ...
- web移动开发的小细节(持续添加)
1.触屏设备上如何去掉数字转化为拨号链接的方法 <meta name=”format-detection” content=”telephone=no” /> telephone=no就禁 ...
- ECSTORE关于后端FILTER条件的表现形式以及含义。
cstore关于后端filter条件的表现形式以及含义如下: $FILTERARRAY= ARRAY( 'THAN'=>' > '.$VAR, 'LTHAN'=>' < '.$ ...
- QQ在线客服
css代码: .float0831 { POSITION: fixed; TOP: 180px; RIGHT: 1px; _position: absolute } .float0831 A { CO ...
- cygwin编译SDL1.2
1.下载了一个SDL-1.2.14.tar.gz 2.下载一个cygwin64对SDL-1.2.14.tar.gz解压 tar -zxvf SDL-1.2.14.tar.gz 在网上找的大概是需要需要 ...
- 我和小美的撸码日记(2)之第一个基于MVC+Jqgrid的列表页面
一.前言 “尼玛哥,上周你教我改了下OA系统UI,黄总看了很满意呀.” “不错不错,看来小美进步很大,可以提前结束试用期,到时候加工资别忘了请我吃饭呀!” “尼玛哥,你有女朋友了吗?” “ ...
- STARTUP.A51详解
STARTUP.A51 这个文件有什么用,有必要添加到工程吗? 如果不添加"startup.a51"文件,编译器就会自动加入一段初始化内存以及堆栈等的代码,这时的内存初始化部分你就 ...
- 转:linux shell 数组建立及使用技巧
linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...
- 深入理解linux网络技术内幕读书笔记(二)--关键数据结构
Table of Contents 1 套接字缓冲区: sk_buff结构 1.1 网络选项及内核结构 1.2 结构说明及操作函数 2 net_device结构 2.1 MTU 2.2 结构说明及操作 ...
- Spring事务管理器分类
Spring并不直接管理事务,事实上,它是提供事务的多方选择.你能委托事务的职责给一个特定的平台实现,比如用JTA或者是别的持久机制.Spring的事务管理器可以用下表表示: 事务管理器的实例 目标 ...