hdu-2642 Stars---二维树状数组(细节处理)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2642
题目大意:
B x y:将星星x y点亮
D x y:将星星x y熄灭
Q x1 x2 y1 y2:询问该区域内有多少亮的星
解题思路:
二维树状数组模拟即可
注意:
1、下标+1
2、同一位置星星可以点亮多次,熄灭多次,需要用bool数组记录星星状态再更改树状数组
3、输入的区域是x1 x2 y1 y2,一开始错认为是x1 y1 x2 y2,而且没有大小顺序,需要自己判断
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbit(i) (i&(-i))
using namespace std;
int star[][];
bool light[][]; void add(int x, int y, int d)
{
for(int i = x; i <= ; i += lowbit(i))
{
for(int j = y; j <= ; j += lowbit(j))
star[i][j] += d;
}
}
int sum(int x, int y)
{
int ans = ;
for(int i = x; i; i -= lowbit(i))
{
for(int j = y; j; j -= lowbit(j))
ans += star[i][j];
}
return ans;
}
int main()
{
int n, x, y, x1, x2, y1, y2;
while(scanf("%d", &n) != EOF)
{
memset(light, , sizeof(light));
memset(star, , sizeof(star));
char c[];
while(n--)
{
scanf("%s", c);
if(c[] == 'B')
{
scanf("%d%d", &x, &y);
x++, y++;
if(!light[x][y])add(x, y, );
light[x][y] = ;
}
else if(c[] == 'D')
{
scanf("%d%d", &x, &y);
x++, y++;
if(light[x][y])add(x, y, -);
light[x][y] = ;
}
else if(c[] == 'Q')
{
scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
x1++, y1++, x2++, y2++;
if(x1 > x2)swap(x1, x2);
if(y1 > y2)swap(y1, y2);
cout<<(sum(x2, y2) + sum(x1 - , y1 - ) - sum(x1 - , y2) - sum(x2, y1 - ))<<endl;
}
}
}
return ;
}
hdu-2642 Stars---二维树状数组(细节处理)的更多相关文章
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- HDU 5517 【二维树状数组///三维偏序问题】
题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...
- HDU 4456(二维树状数组+坐标转换)
题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Submiss ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- HDU 5465 Clarke and puzzle Nim游戏+二维树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 26 ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
随机推荐
- Idea如何设置代码超出长度限制时自动换行
在[File]-->[Settings]-->[Code Sytle]中勾选[Wrap on typing]选项
- getResourceAsStream properties
try (InputStream is = getClass().getResourceAsStream("/test.properties")) { Properties pro ...
- window.open()总结
今天找父页面和子页面调去问题. 父页面js 1.打开子页面 可以设置子页面长宽高和位置 win= window.open(url, "", "top=45,left=20 ...
- (转)Mat, vector<point2f>,Iplimage等等常见类型转换
在mfc c++ 以及opencv 编写程序当中,很多常用的类型转换,现在总结一下.(注意加相应的头文件,这里不罗嗦) 提纲: 1. Mat ---> Iplimage 2. Iplimage ...
- 22-----BBS论坛
BBS论坛(二十二) 22.1.七牛js上传轮播图图片 (1)common/zlqiniu.js 'use strict'; var zlqiniu = { 'setup': function (ar ...
- g++ -I(大写i) 与-L(大写l)-l(小写l) 的作用与学习
linux 下 g++编译程序时,-I(大写i) 与-L(大写l)-l(小写l) 的作用 作为一个linux入门级使用者,gcc/g++ 的简单操作已经用过多次, 但是有时稍微复杂一点的程序就会使用到 ...
- HBuilder的常用快捷键
Ctrl + d 删除整行内容 Ctrl + Shift +R 复制当前行到下一行 Ctrl + Shift +D 重新编辑 Ctrl + 方向键 当前行整行内容上移或下移 Alt + ↓ 跳转到下一 ...
- PlayMaker Int的两个数进行比较 Int Compare
Integer 1 和 Integer 2 进行比较,Integer 1 和 Integer 2 相等的时候 执行 ChangeToGreen; Integer 1 比 Integer 2 大的时 ...
- 修改linux文件权限命令
修改linux文件权限命令:chmod Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文 ...
- Spring Cloud学习笔记之微服务架构
目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务 微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的. 其中每个小型服务都运行在自己的进程中,并经常采 ...