hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 785 Accepted Submission(s): 335
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.
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.
B 581 145
B 581 145
Q 0 600 0 200
D 581 145
Q 0 600 0 200
0
#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 二维树状数组 单点更新区间查询 模板水题的更多相关文章
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- TZOJ 2725 See you~(二维树状数组单点更新区间查询)
描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...
- hdu2642二维树状数组单点更新+区间查询
http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意:一个星空,二维的.上面有1000*1000的格点,每个格点上有星星在闪烁.一开始时星星全部暗淡着 ...
- 【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)
试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡 ...
- SPOJ - MATSUM 二维树状数组单点更新
忘记了单点更新时要在树状数组中减去原值..wa了一发 /* 矩形求和,单点更改 */ #include<iostream> #include<cstring> #include ...
- hdu2642二维树状数组单点更新
碰到这种题一定要注意坐标是不是有序的,也要注意坐标是不是有0的,有的话需要+1处理 #include<bits/stdc++.h> using namespace std; #define ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
随机推荐
- 【解决方法】System.IO.FileNotFoundException
错误日志 See the end of this message for details on invoking just-in-time (JIT) debugging instead of thi ...
- Android播放音乐时跳动的屏谱demo
Android实现播放音频时的跳动频谱,并实现可以调节的均衡器. Main.java package com.wjq.audiofx; import android.app.Activity; imp ...
- 收货MIGO
FUNCTION zrfc_mm003. *"---------------------------------------------------------------------- * ...
- linux: 可重入函数与不可重入函数
1. 可重入函数与线程安全 摘自 多线程和多进程的区别(小结) http://blog.csdn.net/hairetz/article/details/4281931 要确保函数线程安全,主要需要考 ...
- 数据层交换和高性能并发处理(开源ETL大数据治理工具--KETTLE使用及二次开发 )
ETL是什么?为什么要使用ETL?KETTLE是什么?为什么要学KETTLE? ETL是数据的抽取清洗转换加载的过程,是数据进入数据仓库进行大数据分析的载入过程,目前流行的数据进入仓库的 ...
- 教会你如何编写makefile文件
最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...
- Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)
游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...
- mac 修改系统配置参数 主机名 等
mac 修改系统配置参数,可以使用 命令 scutil 参考网址: https://developer.apple.com/library/mac/documentation/Darwin/Refer ...
- delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)
前言:网上有很多介绍delphi创建闪屏的代码,大多只是在程序开启前显示一个闪屏,但是却没有说如何在闪屏上显示程序加载的进度,于是笔者有意思介绍一下这种闪屏方式. 1.创建一个窗体(TfrmSplas ...
- Boost Thread学习笔记二
除了thread,boost种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive ...