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. hive regex insert join group cli

    1.insert Insert时,from子句既能够放在select子句后,也能够放在insert子句前,以下两句是等价的 hive> FROM invites a INSERT OVERWRI ...

  2. How to search a table in a store proc and open the store proc

    1. select*fromdba_dependencieswherereferenced_name='CNTL_ISSUE'andTYPE='PROCEDURE' 2. selecttextfrom ...

  3. MSSQL - 存储过程Return返回值

    1.存储过程中不使用外部参数. 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========================== ...

  4. 相遇Qt5

    使用Qt5.x版本中的不同方面来开发应用程序,着重于新的Qt Quick的技术,提供了编写C++后端的必要内容,并扩展了Qt Quick.     本章提供了关于Qt5高层次的概述.它对开发者有效的展 ...

  5. 例解 autoconf 和 automake 生成 Makefile 文件

    本文介绍了在 linux 系统中,通过 Gnu autoconf 和 automake 生成 Makefile 的方法.主要探讨了生成 Makefile 的来龙去脉及其机理,接着详细介绍了配置 Con ...

  6. 怎样使用 App Studio 高速定制你自己的 Universal Windows App

    今天之所以在写一篇关于 App Studio 的文章是由于,App Studio 经过了几次升级功能得到了明显提升还能够调用系统功能了.而且能够更方便的和应用商店关联公布 Universal Wind ...

  7. MySQL 暂时文件夹

    MySQL数据文件夹/data/mysql所在的上层文件夹/data磁盘空间不足导致MySQL启动失败,所以清理了/data文件夹下除了mysql子文件夹外的其它无用文件夹.重新启动发现还是失败.检查 ...

  8. Swift - 系统声音服务的使用(播放声音,提醒,震动)

    1,系统声音服务介绍: 系统声音服务提供了一个Api,用于播放不超过30秒的声音.它支持的文件格式有限,具体的说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. 但此函数没有提供操 ...

  9. Eclipse ADT 更换主题

    如果Eclipse 版本3.6以上 在 Help→Eclipse Marketplace 搜索 Theme 之后安装即可 如果Eclipse版本3.5 一下, 通过地址安装插件: http://ecl ...

  10. matlab中plot使用方法

    MATLAB有非常强的图形功能,能够方便地实现数据的视觉化.强大的计算功能与图形功能相结合为MATLAB在科学技术和教学方面的应用提供了更加广阔的天地.以下着重介绍二维图形的画法,对三维图形仅仅作简单 ...