Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1628    Accepted Submission(s):
683

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
 
初学树状数组,看了一些文章,算是有了初步的认识。
自己的理解:用二进制表示的方法来实现二分。一个二进制数c[k]表示的是从a[k]开始往前的lowbit(k)个数的和(lowbit(k)为k的最低位,也即将k的除最低位外的所有为置为0)。
更新:
void add(int k,int x)
{
for(int i=k;i<MAXN;i+=lowbit(i))
c[i]+=x;
}

lowbit():

int lowbit(int x)   //取最低位
{
return x&(-x);
}

查询:

int get_sum(int k)
{
int res=;
for(int i=k;i>;i-=lowbit(i))
res+=c[i];
return res;
}

这道题是一道二维树状数组,原理其实也就是这样。

注意:题目中坐标从0开始,可能对一颗star做两次同样的操作。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stdlib.h>
#include<stack>
#include<vector>
using namespace std; const int MAXN=;
int a[MAXN][MAXN];
bool b[MAXN][MAXN]; int lowbit(int x)
{
return x&(-x);
} void modify(int x,int y,int data)
{
for(int i=x; i<MAXN; i+=lowbit(i))
for(int j=y; j<MAXN; j+=lowbit(j))
a[i][j]+=data;
} int getsum(int x,int y)
{
int res=;
for(int i=x; i>; i-=lowbit(i))
for(int j=y; j>; j-=lowbit(j))
res+=a[i][j];
return res;
} int main()
{
int n,x,y,x1,y1;
char str[];
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(str[]=='B')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]) continue;
modify(x,y,);
b[x][y]=;
}
else if(str[]=='D')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]==) continue;
modify(x,y,-);
b[x][y]=;
}
else
{
scanf("%d%d%d%d",&x,&x1,&y,&y1);
x++;x1++;y++;y1++;
if(x>x1) swap(x,x1);
if(y>y1) swap(y,y1);
int ans=getsum(x1,y1)-getsum(x-,y1)-getsum(x1,y-)+getsum(x-,y-);
printf("%d\n",ans);
}
}
return ;
}

最新文章

  1. 关于jQuery外部框架
  2. java中文乱码分析整理
  3. 【OPENGL】第二篇 HELLO OPENGL(续)
  4. 大毕设-MATLAB-FFT实现
  5. 05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能
  6. 使用perl实现scp批量分发
  7. Android activity跳转方式
  8. jquery uploadify 使用
  9. PHP Document 注释标记及规范 &amp;&amp; PHP命名规范
  10. C#学习笔记(四):委托和事件
  11. JavaBean 反射机制实现自动配置数据
  12. 201521123093 java 第九周学习总结
  13. qsort函数应用大全
  14. 跟我学ASP.NET MVC之四:使用Razor
  15. nginx加密,访问接口认证
  16. ZH奶酪:Ubuntu 14.04安装LAMP(Linux,Apache,MySQL,PHP)
  17. C语言博客作业02——循环结构
  18. Python的闭包和装饰器
  19. 关于spring boot 的事务类型配置留存
  20. P2860 [USACO06JAN]冗余路径Redundant Paths

热门文章

  1. 【[Offer收割]编程练习赛13 D】骑士游历(矩阵模板,乘法,加法,乘方)
  2. BNUOJ 3958 MAX Average Problem
  3. SCU Right turn
  4. 【学QT】2 - QT/E环境的建立
  5. oracle导入少量数据(少于10M)
  6. N - Corporate Identity
  7. window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法
  8. mongodb之存储引擎
  9. iOS中UITextView的操作技巧
  10. springboot-quartz 实现动态添加,修改,删除,暂停,恢复等功能