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<stdio.h>
#include<string.h>
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;
}

hdu 2642 二维树状数组 单点更新区间查询 模板水题的更多相关文章

  1. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  2. TZOJ 2725 See you~(二维树状数组单点更新区间查询)

    描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...

  3. hdu2642二维树状数组单点更新+区间查询

    http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意:一个星空,二维的.上面有1000*1000的格点,每个格点上有星星在闪烁.一开始时星星全部暗淡着 ...

  4. 【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)

    试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡 ...

  5. SPOJ - MATSUM 二维树状数组单点更新

    忘记了单点更新时要在树状数组中减去原值..wa了一发 /* 矩形求和,单点更改 */ #include<iostream> #include<cstring> #include ...

  6. hdu2642二维树状数组单点更新

    碰到这种题一定要注意坐标是不是有序的,也要注意坐标是不是有0的,有的话需要+1处理 #include<bits/stdc++.h> using namespace std; #define ...

  7. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  8. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

  9. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

随机推荐

  1. C#利用Lambda和Expression实现数据的动态绑定

    在程序开发过程中,有时为了让数据能够实时更新,我们会采用数据绑定来实现. 一般我们数据绑定时我们是这样写的 public class Helper : INotifyPropertyChanged { ...

  2. CSDN高校俱乐部2013年秋季北京地区第一站“编程语言的应用及其发展”—北京联合大学

    2013年12月11日晚17:00.CSDN高校俱乐2013年秋季北京地区第一站“编程语言的应用及其发展”在北京联合大学进行. 首先,CSDN总部人员介绍CSDN俱乐部的改版以及线上编程挑战赛.CSD ...

  3. Silverlight技术调查(3)——国际化

    原文 Silverlight技术调查(3)——国际化 网上有很多关于Silverlight国际化的说明,包括MSDN的示例,都没有强调一点,下面以红色标示,基础国际化知识请先参考MSDN相关章节,关键 ...

  4. poj 3778

    这就是个超级水题……!!!!写一写来纪念一下自己的错误…… 如果某个学生的的成绩是其他俩个或三个学生成绩的和则给予奖励 直接暴力,所以一开始直接用数组标记两个人或三个人的和,但是忽略了这种情况 20( ...

  5. html-图片button,抓包---Shinepans

    askLike.html <html> <meta http-equiv="content-type" content="text/html;chars ...

  6. 一个关于native sql的程序

    *&---------------------------------------------------------------------* *& Report ZHR_BPM11 ...

  7. java.util.Queue用法

    队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...

  8. Appium 出现 > error: com.test/.activity1 never started. Current: com.test/.activity2

    在apium 运行的时候,会出现提示 activity 没有启动,当前是 activity 出现原因: 出现这种情况是因为 launch activity 再启动 app 之后 无法找到,变成了app ...

  9. Bootstrap,Foundation和TypeScript

    http://www.oschina.net/question/12_128155 http://www.oschina.net/news/72330/typescript-2-0 给自己提个醒,随时 ...

  10. vc 加载bmp位图并显示的方法

    方法一.显示位图文件 HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(“xxx.bmp”),Image_Bitmap,0,0,Lr_CreateDibSectio ...