【BZOJ-1452】Count 树状数组 套 树状数组
2024-08-26 16:25:08
1452: [JSOI2009]Count
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1769 Solved: 1059
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
Sample Output
1
2
2
HINT
Source
Solution
忽略标题的说法...撞壁用的....
简单题,裸树状数组套树状数组
颜色数目$c<=100$很小,考虑对于每种颜色单独进行处理
那么对于每个颜色单独开一个二维树状数组维护
另开一个二维数组记录下当前各个位置的颜色
1操作,将原有颜色树状数组中的$(x,y)$点置成0,在新颜色树状数组中置成1,并更新颜色的状态
2操作,区间查询,应用前缀和,进行一下加减即可得到当前子矩阵和
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
int n,m,q;int zt[][];
struct Treenode
{
int tree[][];
Treenode(){memset(tree,,sizeof(tree));}
int lowbit(int x){return x&-x;}
void change(int x,int y,int del)
{
for (int i=x; i<=n; i+=lowbit(i))
for (int j=y; j<=m; j+=lowbit(j))
tree[i][j]+=del;
}
int query(int x,int y)
{
int ans=;
for (int i=x; i; i-=lowbit(i))
for (int j=y; j; j-=lowbit(j))
ans+=tree[i][j];
return ans;
}
int ask(int x1,int y1,int x2,int y2)
{
return query(x2,y2)-query(x2,y1-)-query(x1-,y2)+query(x1-,y1-);
}
}color[]; int main()
{
n=read(); m=read();
for (int i=; i<=n; i++)
for (int col,j=; j<=m; j++)
col=read(),color[col].change(i,j,),zt[i][j]=col;
q=read();
for (int i=; i<=q; i++)
{
int opt=read(),x1,y1,x2,y2,col;
if (opt==)
{
x1=read(),y1=read(),col=read();
color[zt[x1][y1]].change(x1,y1,-);
zt[x1][y1]=col;
color[col].change(x1,y1,);
continue;
}
x1=read(),x2=read(),y1=read(),y2=read(),col=read();
printf("%d\n",color[col].ask(x1,y1,x2,y2));
}
return ;
}
5分钟不到,看、想、写、1A系列....
最新文章
- css选择器中:first-child与:first-of-type的区别
- Docker的单主机容器网络
- Oracle用户信息查询
- Struts输出流向jsp页面写入图片乱码
- 20151214 jquery插件代码备份
- Hadoop, Python, and NoSQL lead the pack for big data jobs
- SQLSERVER 中GO的作用详解
- MFC CSplitterWnd的用法
- 用XAML做网页!!—页头
- maven系列--maven常用命令
- hdu 5591 BestCoder Round #65(博弈)
- Android开发学习之路--RxAndroid之lambda
- centOS连接没问题,使用SecureCRT就不能连接
- Java 身份证号码验证
- 置顶菜单demo
- iOS - App Extension 整体总结
- BZOJ5314 [Jsoi2018]潜入行动 【背包类树形dp】
- uid
- python greenlet背景介绍与实现机制
- 【SpringMVC学习06】SpringMVC中的数据校验
热门文章
- UESTC 31 饭卡(Card) --背包问题
- java 21 - 10 文本文件和集合之间互相存储数据
- 关于软件测试人员能力模型的建立(from知乎)
- ArcGis 统计方法
- 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结
- R语言利器之ddply和aggregate
- [tools]tcp/udp连通性测试
- svn命令行修改已提交的版本备注
- 未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
- OpenGL 4.3配置教程