POJ 1656 Counting Black
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9772 | Accepted: 6307 |
Description

We may apply three commands to the board:
1. WHITE x, y, L // Paint a white square on the board,
// the square is defined by left-top grid (x, y)
// and right-bottom grid (x+L-1, y+L-1) 2. BLACK x, y, L // Paint a black square on the board,
// the square is defined by left-top grid (x, y)
// and right-bottom grid (x+L-1, y+L-1) 3. TEST x, y, L // Ask for the number of black grids
// in the square (x, y)- (x+L-1, y+L-1)
In the beginning, all the grids on the board are white. We apply a series of commands to the board. Your task is to write a program to give the numbers of black grids within a required region when a TEST command is applied.
Input
Output
Sample Input
5
BLACK 1 1 2
BLACK 2 2 2
TEST 1 1 3
WHITE 2 1 1
TEST 1 1 3
Sample Output
7
6
题目大意:输入n代表有n行指令,每行指令包括一个字符串cmd和三个数字x,y,nLen,如果cmd为“BLACK”表示将以x,y为左上角的边长为nLen的正方形里面的方块全部变成黑色,如果cmd为“WHITE”表示将以x,y为左上角的边长为nLen的正方形里面的方块全部变成白色,如果cmd为“TEST”则查询以x,y为左上角的边长为nLen的正方形里面的黑色方块共有多少个。
解题方法:而为树状数组。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int maze[][];
int color[][]; int lowbit(int x)
{
return x & -x;
} void add(int x, int y, int n)
{
if (color[x][y] == n)
{
return ;
}
color[x][y] = n;
for (int i = x; i <= ; i += lowbit(i))
{
for (int j = y; j <= ; j += lowbit(j))
{
maze[i][j] += n;
}
}
} int getsum(int x, int y)
{
int sum = ;
for (int i = x; i >= ; i -= lowbit(i))
{
for (int j = y; j >= ; j -= lowbit(j))
{
sum += maze[i][j];
}
}
return sum;
} int main()
{
int n;
int x, y, nLen;
char cmd[];
scanf("%d", &n);
memset(color, -, sizeof(color));
while(n--)
{
scanf("%s%d%d%d", cmd, &x, &y, &nLen);
if (strcmp(cmd, "BLACK") == )
{
for (int i = x; i < x + nLen; i++)
{
for (int j = y; j < y + nLen; j++)
{
add(i, j, );
}
}
}
else
{
if (strcmp(cmd, "WHITE") == )
{
for (int i = x; i < x + nLen; i++)
{
for (int j = y; j < y + nLen; j++)
{
add(i, j, -);
}
}
}
else
{
printf("%d\n", getsum(x + nLen - , y + nLen - ) - getsum(x - , y + nLen - ) - getsum(x + nLen - , y - ) + getsum(x - , y - ));
}
}
}
return ;
}
POJ 1656 Counting Black的更多相关文章
- 数黑格有多少个,模拟题,POJ(1656)
题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...
- POJ 1971-Parallelogram Counting,暴力1063ms!
Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...
- POJ Ant Counting DP
dp[i][j]表示前i种蚂蚁组成元素个数为j的集合有多少种. 则dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][ max(0,j-a[i]) ...
- POJ 1656
#include<iostream>//chengdacaizi 08 .11. 12 #include<string> using namespace std; ][]={} ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
随机推荐
- PostgreSQL: epoch 新纪元时间的使用
新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用 ...
- ucos-ii核心算法分析(转)
μC/OS-Ⅱ是一种免费公开源代码.结构小巧.具有可剥夺实时内核的实时操作系统.其 内核提供任务调度与管理.时间管理.任务间同步与通信.内存管理和中断服务等功能.适合小型控制系统,具有执行效率高.占用 ...
- 2018.4.13 用java配置/生成Xml文件 结合IO流知识点
自己创建本地文件Hello.txt 里面有数据 小明/23/增城/广东 小花/12/浦东/上海 StudentManager.java package com.lanqiao.dmeo7; impor ...
- _variant_t的使用
我们先看看COM所支持的一些类型的基本类: (微软提供,在comdef.h中定义) 在COM中使用的标准类Class如下所示: _bstr_t:对BSTR类型进行打包,并提供有用的操作和方法: _co ...
- Bootstrap历练实例:默认的Well
Well 是一种会引起内容凹陷显示或插图效果的容器 <div>.为了创建 Well,只需要简单地把内容放在带有 class .well 的 <div> 中即可.下面的实例演示了 ...
- 01_9_Struts用ModelDriven接收参数
01_9_Struts用ModelDriven接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...
- Unity3d 判断物体是否在可见范围内
unity中自带两个回调函数: void OnBecameVisible()//当物体可见时,回调一次. void OnBecameInvisible()//当物体不可见时,回调一次. 在untiy编 ...
- iOS中的数据存储方式_Plist
plist文件只能存储OC常用数据类型(NSString.NSDictionary.NSArray.NSData.NSNumber等类型)而不能直接存储自定义模型对象; 我们拿NSData举例: /* ...
- Angular-constructor和ngOnInit区别
参考文档:https://blog.csdn.net/u010730126/article/details/64486997 总结:constructor做依赖注入,避免业务操作: ngOninit做 ...
- java解析多层嵌套json字符串
java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...