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) ...
随机推荐
- Tomcat常见问题
1. tomcat主页 http://localhost:8080 打不开 设置环境变量JAVA_HOME,确认端口为8080,查看webapps\ROOT文件夹是否存在 2. 访问tomcat管理页 ...
- Jenkins安装过程
1.安装环境 配置java环境 安装Tomcat 2.将Jenkins.war 包放入Tomcat的webapps目录 3.启动tomcat后,tomcat会解压war包,生成一个jenkins文件夹 ...
- 用dango框架搭建博客网站
1.我早先下载了Anaconda35.0.1.但是Anaconda自带的编辑器Spyder我用的不太熟练.所以还是使用Pycharm来编辑代码.我的Pycharm试用期已经到了,所以需要注册码来使用P ...
- c++11 多线程 1
第3章 线程间共享数据 本章主要内容 共享数据带来的问题 使用互斥量保护数据 数据保护的替代方案 保护共享数据结构的最基本的方式,是使用C++标准库提供的互斥量(mutex). 清单3.1 使用互斥量 ...
- Win10通过SSH与树莓派Raspbain系统互传文件
1.在Linux系统上安装ssh-server(由于Raspbain系统自带ssh-server,这个步骤可以省略) 查看ssh是否运行的命令: ps -ef | grep ssh 如果没有安装,则安 ...
- HBase 的Get(读),Put(写),Delete(删),Scan(扫描)和Increment(列值递增)
一.HBase介绍 1.基本概念 HBase是一种Hadoop数据库,经常被描述为一种稀疏的,分布式的,持久化的,多维有序映射,它基于行键.列键和时间戳建立索引,是一个可以随机访问的存储和检索数据的平 ...
- ajax禁止浏览器缓存
把cache 设置为false ,把 ifModified 设置为true //工作计划function workprogram(date_time){ $.ajax({ asyn ...
- linux 查看页大小
# getconf PAGE_SIZE 一般是4096
- Murano Weekly Meeting 2016.07.12
Meeting time: 2016.July.12 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- PHP函数库(core)
数组函数: array_change_key_case — 返回字符串键名全为小写或大写的数组 array_change_key_case() 将 input 数组中的所有键名改为全小写或大写.改变是 ...