Stars(二维树状数组)
Stars |
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) |
Total Submission(s): 111 Accepted Submission(s): 54 |
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 |
Sample Output
1 |
Author
teddy
|
Source
百万秦关终属楚
|
Recommend
teddy
|
/*
题意:二维坐标,然后三种操作,B x y 坐标(x,y)处的星星亮了,D x y坐标(x,y)处的星星灭了 Q X1 X2 Y1 Y2询问矩形内有多少颗
亮的星星 初步思路:二维树状数组,但是操作的时候先要查询一下当前位置星星的状态,还有就是查询的坐标可能不是x1<x2 y1<y2
*/
#include<bits/stdc++.h>
#define N 1010
#define lowbit(x) x&(-x)
using namespace std;
int t,X1,X2,Y1,Y2;
int n1,n2,m1,m2;
char op[];
int c[N][N];
void update(int x,int y,int val)
{
for(int i=x;i<N;i+=lowbit(i))
{
for(int j=y;j<N;j+=lowbit(j))
{
c[i][j]+=val;
}
}
// return val;//返回你实际搬运的东西
}
int getsum(int x,int y)
{
int s=;
for(int i=x;i>;i-=lowbit(i))
{
for(int j=y;j>;j-=lowbit(j))
{
s+=c[i][j];
}
}
return s;
}
void init(){
memset(c,,sizeof c);
}
int main(){
// freopen("in.txt","r",stdin);
init();
scanf("%d",&t);
while(t--){
scanf("%s",op);
if(op[]=='B'){
scanf("%d%d",&X1,&Y1);
X1++;
Y1++;
//先查询一下当前位置的星星是不是亮着的
if(getsum(X1,Y1)-getsum(X1-,Y1)-getsum(X1,Y1-)+getsum(X1-,Y1-)==){
// cout<<"有星星亮的"<<endl;
continue;
} update(X1,Y1,); }else if(op[]=='D'){
scanf("%d%d",&X1,&Y1);
X1++;
Y1++;
//先查询一下当前位置的星星是不是没亮
if(getsum(X1,Y1)-getsum(X1-,Y1)-getsum(X1,Y1-)+getsum(X1-,Y1-)==){
// cout<<"星星已经灭了"<<endl;
continue;
}
update(X1,Y1,-); }else{
scanf("%d%d%d%d",&X1,&X2,&Y1,&Y2);
X1++;X2++;
Y1++;Y2++;
// cout<<X1<<" "<<Y1<<" "<<X2<<" "<<Y2<<endl;
// cout<<getsum(X2,Y2)<<" "<<getsum(X1-1,Y2)<<" "<<getsum(X2,Y1-1)<<" "<<getsum(X1-1,Y1-1)<<endl;
n1=min(X1,X2),m1=min(Y1,Y2),n2=max(X1,X2),m2=max(Y1,Y2);
printf("%d\n",getsum(n2,m2)-getsum(n1-,m2)-getsum(n2,m1-)+getsum(n1-,m1-));
}
}
return ;
}
Stars(二维树状数组)的更多相关文章
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- HDU_2642_二维树状数组
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submi ...
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
随机推荐
- 分享一个图片上传插件(TP5.0)
效果预览图: 该插件主要功能是:可预览裁剪图片和保存原图片,执行裁剪图片后会删除 裁剪的原图片目录,以便减少空间.一.下载附件 地址:https://pan.baidu.com/s/1bpxZhM3 ...
- Dijkstra堆优化学习
最短路径例题 今天特地学习了Dijkstra的堆优化(主要是慕名已久). 我们需要一个堆来记录[编号,到编号这个点的最短路径值(当然只是当前的)] 与原来的Dijkstra操作基本一致,主要有以下几点 ...
- ASP.NET Core 认证与授权[1]:初识认证
在ASP.NET 4.X 中,我们最常用的是Forms认证,它既可以用于局域网环境,也可用于互联网环境,有着非常广泛的使用.但是它很难进行扩展,更无法与第三方认证集成,因此,在 ASP.NET Cor ...
- poj3468树状数组的区间更新,区间求和
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47174 ...
- .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控
一.crontab使用详解 概述:任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类: (1)系统工作:有些重要的工作必须周而 复始地执行. (2)个别用户工作:个别用户可能希望执 行 ...
- Ansible(一) - 入门及安装
Ⅰ. Ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置 ...
- 设置Intel网卡以抓取报文的vlan tag
一.实验环境 1.ThinkPad T450 Notebook 2.Notebook网卡Intel I218-V 二.设置步骤 1."设备管理器" -> "Inte ...
- struts2中的结果视图类型
实际上在Struts2框架中,一个完整的结果视图配置文件应该是: <action name="Action名称" class="Action类路径" me ...
- 固定GridView标题栏,冻结列功能实现
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> < ...
- SqlServer和Oracle中一些常用的sql语句4 局部/全局变量
--把wh1仓库号中姓名含有"平"字的职工工资在原来的基础上加288 update 职工备份 set 工资=工资+288 where 仓库号='wh1' and 姓名 like ' ...