博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2642 二维树状数组 单点更新区间查询 模板水题
阅读量:6513 次
发布时间:2019-06-24

本文共 2936 字,大约阅读时间需要 9 分钟。

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)

Total Submission(s): 785    Accepted Submission(s): 335

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
 

 

Author
teddy
 

 

Source
 

 

Recommend
teddy
 
 
 
 
题意:  有一个地图   每个点都是一个灯    输入B 之后输入坐标 表示对应的点的灯变亮    输入D  输入坐标 表示变暗  输入Q  输入一个矩形的左下角和右上角 问矩形内的亮着的灯的个数
 
思路:
很明显的 二维树状数组  注意 灯亮过了就不能再亮了 而且灯关了就不能再关了 所以可以用一个flag数组标记是否可以进行关灯开灯操作   
 
注意下标要从1开始  题目从0开始  所以要加1
 
#include
#include
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;}

 

转载地址:http://neifo.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
网站的开发流程介绍(转)
查看>>
浅析:Android--Fragment的懒加载
查看>>
Linux操作系统目录和Linux常用的文件和目录管理命令
查看>>
DIY:自己动手做一个迷你 Linux 系统(二)
查看>>
猫猫学IOS(三十)UI之Quartz2D画图片画文字
查看>>
windows 指定的网络名不可用__被我解决了!
查看>>
09值类型、引用类型、字符串
查看>>
ethereumjs/merkle-patricia-tree-2-API
查看>>
go标准库的学习-runtime
查看>>
pytorch Debug —交互式调试工具Pdb (ipdb是增强版的pdb)-1-使用说明
查看>>
NodeJS学习之文件操作
查看>>
导入excel
查看>>
AJAX的get和post请求原生编写方法
查看>>
WebSocket 是什么原理?为什么可以实现持久连接
查看>>
Python自学笔记-logging模块详解
查看>>
IE6下实现min-height
查看>>
Head First--设计模式
查看>>
iOS之CAGradientLayer属性简介和使用
查看>>
微信小程序UI组件、开发框架、实用库
查看>>