POJ-1565-Counting Black
来源:互联网 发布:传输层的端口号 编辑:程序博客网 时间:2024/04/25 09:27
题目大意是说给你一个初始全为白色的矩阵,然后每次有3种操作(1、将矩阵x,y->x+l-1,y+l-1全部变为染黑 2、将矩阵x,y->x+l-1,y+l-1全部变为染白 3、求矩阵x,y->x+l-1,y+l-1中含有多少个白色)
用二维树状数组可以完成,对于染色分别更新1和-1,注意如果当前颜色与更新颜色相同,则不需要更新
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=101;int n,t[maxn][maxn],color[maxn][maxn];int lowbit(int x){ return x&(-x);}void Update(int row,int col,int val){ if(color[row][col]==val)return; color[row][col]=val; for(int i=row;i<maxn;i+=lowbit(i))for(int j=col;j<maxn;j+=lowbit(j)) t[i][j]+=val;}int Sum(int row,int col){ int ans=0; for(int i=row;i>0;i-=lowbit(i))for(int j=col;j>0;ans+=t[i][j],j-=lowbit(j)); return ans;}int main(){ scanf("%d",&n); memset(color,-1,sizeof(color)); while(n--) {char op[10];int x,y,l;scanf("%s%d%d%d",op,&x,&y,&l);if(op[0]=='B') for(int i=x;i<x+l;i++)for(int j=y;j<y+l;j++) Update(i,j,1);else if(op[0]=='W') for(int i=x;i<x+l;i++)for(int j=y;j<y+l;j++) Update(i,j,-1);else printf("%d\n",Sum(x+l-1,y+l-1)-Sum(x-1,y+l-1)-Sum(x+l-1,y-1)+Sum(x-1,y-1)); } return 0;}
- POJ-1565-Counting Black
- POJ 1656 Counting Black
- poj 1656 Counting Black
- POJ 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- poj 1656 Counting Black
- POJ 1656 Counting Black
- Poj 1656 Counting Black
- poj 1656 Counting Black 水题
- POJ-1656-Counting Black- 四分树
- POJ 1656 Counting Black(水~)
- poj-1656-Counting Black-(树状数组)
- POJ-1656 Counting Black (二维树状数组 入门题)
- PKU 1656 Counting black
- poj_1656 Counting Black
- Formats of input argument do not match() in function cvFilter2D cvfilter.cpp
- 百度百科取消拓展阅读 独特感想
- fork“裂变"
- 2013腾讯编程马拉松初赛第一场(3月21日) 小明系列故事——师兄帮帮忙 !!快速幂取摸
- Fighting: A way to technic
- POJ-1565-Counting Black
- ORACLE监控数据库的常用Shell脚本
- Ubuntu server 与Ubuntu desktop
- POJ 2092 Grandpa is Famous【水---找出现第二多的数】
- MFC ASSERT 详解
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
- jdk 环境变量添加
- NDK : Build X86 binary for Intel Atom platform
- ce.bib and Reginit.ini 如何怎么生成