Stars--升级版 hdu2642
来源:互联网 发布:红色警戒3mac版下载 编辑:程序博客网 时间:2024/05/27 21:48
Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1504 Accepted Submission(s): 616
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
题意:有一个地图 每个点都是一个灯 输入B 之后输入坐标 表示对应的点的灯变亮 输入D 输入坐标 表示变暗 输入Q 输入一个矩形的左下角和右上角 问矩形内的亮着的灯的个数思路:很明显的 二维树状数组 注意 灯亮过了就不能再亮了 而且灯关了就不能再关了 所以可以用一个flag数组标记是否可以进行关灯开灯操作注意下标要从1开始 题目从0开始 所以要加1# include <iostream># include <cstdio># include <cstring>#define N 1000+3using namespace std;int c[N][N];int f[N][N];int bitlow(int x){return x&(-x);}void modify(int x,int y,int d){for(int i=x;i<=N;i+=bitlow(i)){for(int j=y;j<=N;j+=bitlow(j)){c[i][j]+=d;}}}int sum(int x,int y){int ans = 0;for(int i=x;i>0;i-=bitlow(i)){for(int j=y;j>0;j-=bitlow(j)){ans+=c[i][j];}}return ans;}/* 找出这两段代码的区别 这个是错的 int sum(int x,int y){int ans = 0;while(x>0){while(y>0){ans+=c[x][y];y-=bitlow(y);}x-=bitlow(x);}return ans;}*/int main(){int n;while(scanf("%d",&n)!=EOF){memset(c,0,sizeof(c));memset(f,0,sizeof(f));for(int i=0;i<n;i++){int x,y;char st[2];scanf("%s",st);if(st[0]=='B'){scanf("%d%d",&x,&y);++x;++y;if(f[x][y]==1) continue; //如果这个点的坐标已经点量就不用操作的 modify(x,y,1);f[x][y] = 1;//没有点量进行点亮 }else if(st[0]=='D'){scanf("%d%d",&x,&y);++x,++y;if(f[x][y]==0) continue;modify(x,y,-1);f[x][y] = 0;}else if(st[0]=='Q'){int x1,y1,x2,y2;scanf("%d%d%d%d",&x1,&x2,&y1,&y2);++x1,++x2,++y1,++y2;int t;if(x1>x2) t=x1,x1=x2,x2=t;if(y1>y2) t=y1,y1=y2,y2=t;int cnt = 0;cnt = sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1) + sum(x1-1,y1-1);printf("%d\n",cnt);}}}return 0;}
0 0
- Stars--升级版 hdu2642
- HDU2642 Stars 二维树状数组
- HDU2642 Stars【二维树状数组】
- HDU2642 Stars 二维树状数组
- hdu2642 Stars 二维树状数组
- hdu2642
- 树状数组(二维)例题 HDU2642——Stars
- stars
- Stars
- Stars
- Stars
- Stars
- Stars
- Stars
- Stars
- stars
- Stars
- Stars
- win7下使用easyBCD安装ubuntu双系统
- 互斥锁--pthread_mutex_t
- FastJson源码解析:序列化(1.1.45.android)
- Linux基础——VIM编辑器、磁盘管理、用户管理
- SCP指令错误解决方法
- Stars--升级版 hdu2642
- 推荐系统冷启动
- PHP Swift Mailer库使用
- 第35讲项目4——贪财的富翁
- android简单图片上传
- QT线程编程
- 蓝桥杯_算法训练_数字三角形
- 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
- ffmpeg ios代码加字幕相关