HDU_2642_二维树状数组
Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1628 Accepted Submission(s):
683
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.
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.
line.
void add(int k,int x)
{
for(int i=k;i<MAXN;i+=lowbit(i))
c[i]+=x;
}
lowbit():
int lowbit(int x) //取最低位
{
return x&(-x);
}
查询:
int get_sum(int k)
{
int res=;
for(int i=k;i>;i-=lowbit(i))
res+=c[i];
return res;
}
这道题是一道二维树状数组,原理其实也就是这样。
注意:题目中坐标从0开始,可能对一颗star做两次同样的操作。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stdlib.h>
#include<stack>
#include<vector>
using namespace std; const int MAXN=;
int a[MAXN][MAXN];
bool b[MAXN][MAXN]; int lowbit(int x)
{
return x&(-x);
} void modify(int x,int y,int data)
{
for(int i=x; i<MAXN; i+=lowbit(i))
for(int j=y; j<MAXN; j+=lowbit(j))
a[i][j]+=data;
} int getsum(int x,int y)
{
int res=;
for(int i=x; i>; i-=lowbit(i))
for(int j=y; j>; j-=lowbit(j))
res+=a[i][j];
return res;
} int main()
{
int n,x,y,x1,y1;
char str[];
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(str[]=='B')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]) continue;
modify(x,y,);
b[x][y]=;
}
else if(str[]=='D')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]==) continue;
modify(x,y,-);
b[x][y]=;
}
else
{
scanf("%d%d%d%d",&x,&x1,&y,&y1);
x++;x1++;y++;y1++;
if(x>x1) swap(x,x1);
if(y>y1) swap(y,y1);
int ans=getsum(x1,y1)-getsum(x-,y1)-getsum(x1,y-)+getsum(x-,y-);
printf("%d\n",ans);
}
}
return ;
}
HDU_2642_二维树状数组的更多相关文章
- 二维树状数组 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 ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- 如何应对SHA-1加密算法升级为SHA-256
经过权威机构证实,sha1加密算法的不安全性越来越高,sha指纹造假成本越来越低,随即微软.谷歌等IT巨头相继发布弃用sha1加密算法声明,第三方认证机构自2016年1月1日起,将全面停止签发SHA1 ...
- 【hiho一下 第145周】智力竞赛
[题目链接]:http://hihocoder.com/contest/hiho145/problem/1 [题意] [题解] 设f[i][j]表示做对i道题,做错j道题能够到达的最好状态是什么; 这 ...
- ARC 73 E - Ball Coloring
E - Ball Coloring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Ther ...
- [bzoj1131][POI2008]Sta_树形dp
Sta bzoj-1131 POI-2008 题目大意:给定一棵n个点的树,求一个根,使得深度和最大. 注释:$1\le n \le 10^6$. 想法:扭一扭即可. 扭的时候看看这个点当没当过根. ...
- [转]十五天精通WCF——第六天 你必须要了解的3种通信模式
wcf已经说到第六天了,居然还没有说到这玩意有几种通信模式,惭愧惭愧,不过很简单啦,单向,请求-响应,双工模式,其中的第二种“请求-响应“ 模式,这个大家不用动脑子都清楚,这一篇我大概来分析下. 一: ...
- 机器学习1k近邻
自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...
- CSS Display属性与盒模型
由于HTML流式文档的特性,页面布局往往是新手最为头疼的问题之中的一个. 每一个HTML元素都会渲染为一个Box,可分为inline Box和block Box. 依据display属性的不同.Box ...
- Java修改文件夹名称
Java修改文件夹名称 学习了:http://blog.csdn.net/yongh701/article/details/45063833 进行文件夹名字批量修改,注意,要写全路径: package ...
- GPS-Graph Processing System 改动源代码经验总结 (四)
HamaWhite原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 本文目的:在改动GPS源代码后,具体描写叙述怎样编译和分发到各Worker节点上. 以下以Graph ...
- 读取到配置文件的C语言的接口实现
/********************************************************************* * Author : Samson * Date ...