bzoj 2453: 维护队列
2453: 维护队列
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1079 Solved: 503
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2
Q 1 2
R 1 2
Q 1 2
Sample Output
1
HINT
对于100%的数据,有1 ≤ N ≤ 10000, 1 ≤ M ≤ 10000,小朋友A不会修改超过1000次,所有颜色均用1到10^6的整数表示。
Source
带修莫队
#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 10001
#define M 1000001
using namespace std;
int n,m,col[M],sum[M],ans[N];
int tot,now,siz,tmp;
struct Query
{
int l,r,bl,id,tim;
bool operator < (Query p) const
{
if(bl!=p.bl) return bl<p.bl;
if(r!=p.r) return r<p.r;
return tim<p.tim;
}
}e[N];
struct Change
{
int be,af,pos;
}g[];
void update(int p,int w)
{
sum[p]+=w;
if(sum[p]== && w==) tmp++;
if(!sum[p] && w==-) tmp--;
}
int main()
{
scanf("%d%d",&n,&m);
siz=sqrt(n);
for(int i=;i<=n;i++) scanf("%d",&col[i]);
char s[]; int l,r;
while(m--)
{
scanf("%s%d%d",s,&l,&r);
if(s[]=='Q')
{
e[++tot].l=l; e[tot].r=r; e[tot].bl=(l-)/siz; e[tot].tim=now;
e[tot].id=tot;
}
else
{
g[++now].pos=l; g[now].af=r;
}
}
sort(e+,e+tot+);
int L=,R=; now=;
for(int i=;i<=tot;i++)
{
while(now<e[i].tim)
{
now++;
g[now].be=col[g[now].pos];
if(g[now].pos>=L&&g[now].pos<=R)
{
update(g[now].be,-);
update(g[now].af,);
}
col[g[now].pos]=g[now].af;
}
while(now>e[i].tim)
{
if(g[now].pos>=L&&g[now].pos<=R)
{
update(g[now].af,-);
update(g[now].be,);
}
col[g[now].pos]=g[now].be;
now--;
}
while(e[i].l<L) update(col[--L],);
while(e[i].l>L) update(col[L++],-);
while(e[i].r>R) update(col[++R],);
while(e[i].r<R) update(col[R--],-);
ans[e[i].id]=tmp;
}
for(int i=;i<=tot;i++) printf("%d\n",ans[i]);
}
优化后:
读入优化、1,-1改成 true,false
优化一半
#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 10001
#define M 1000001
using namespace std;
int n,m,col[M],sum[M],ans[N];
int tot,now,siz,tmp;
struct Query
{
int l,r,bl,id,tim;
bool operator < (Query p) const
{
if(bl!=p.bl) return bl<p.bl;
if(r!=p.r) return r<p.r;
return tim<p.tim;
}
}e[N];
struct Change
{
int be,af,pos;
}g[];
void update(int p,bool w)
{
if(w)
{
sum[p]++;
if(sum[p]==) tmp++;
}
else
{
sum[p]--;
if(!sum[p]) tmp--;
}
}
void read(int &x)
{
x=; char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') { x=x*+c-''; c=getchar(); }
}
int main()
{
read(n); read(m);
siz=sqrt(n);
for(int i=;i<=n;i++) read(col[i]);
char s[]; int l,r;
while(m--)
{
scanf("%s",s);
read(l); read(r);
if(s[]=='Q')
{
e[++tot].l=l; e[tot].r=r; e[tot].bl=(l-)/siz; e[tot].tim=now;
e[tot].id=tot;
}
else
{
g[++now].pos=l; g[now].af=r;
}
}
sort(e+,e+tot+);
int L=,R=; now=;
for(int i=;i<=tot;i++)
{
while(now<e[i].tim)
{
now++;
g[now].be=col[g[now].pos];
if(g[now].pos>=L&&g[now].pos<=R)
{
update(g[now].be,false);
update(g[now].af,true);
}
col[g[now].pos]=g[now].af;
}
while(now>e[i].tim)
{
if(g[now].pos>=L&&g[now].pos<=R)
{
update(g[now].af,false);
update(g[now].be,true);
}
col[g[now].pos]=g[now].be;
now--;
}
while(e[i].l<L) update(col[--L],true);
while(e[i].l>L) update(col[L++],false);
while(e[i].r>R) update(col[++R],true);
while(e[i].r<R) update(col[R--],false);
ans[e[i].id]=tmp;
}
for(int i=;i<=tot;i++) printf("%d\n",ans[i]);
}
bzoj 2453: 维护队列的更多相关文章
- Bzoj 2453: 维护队列 && Bzoj 2120: 数颜色 分块,bitset
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 578 Solved: 247[Submit][Status][Discuss] ...
- bzoj 2453 : 维护队列 带修莫队
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 952 Solved: 432[Submit][Status][Discuss] ...
- BZOJ 2453 维护队列 | 分块
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2453 题解: 考虑维护每个位置的颜色上一次出现在哪里,计为pre[i],在询问l到r的时候, ...
- BZOJ.2453.维护队列([模板]带修改莫队)
题目链接 带修改莫队: 普通莫队的扩展,依旧从[l,r,t]怎么转移到[l+1,r,t],[l,r+1,t],[l,r,t+1]去考虑 对于当前所在的区间维护一个vis[l~r]=1,在修改值时根据是 ...
- 【BZOJ 2453|bzoj 2120】 2453: 维护队列 (分块+二分)
2453: 维护队列 Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有 ...
- BZOJ 2120 数颜色&2453 维护队列 [带修改的莫队算法]【学习笔记】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 3665 Solved: 1422[Submit][Status][Discuss] ...
- Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 2645 Solved: 1039[Submit][Status][Discuss] ...
- 【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)
先说正解:把所有相同的数相成一个链在每一个区间里的种数就是不同链的链头,那么记录每个数的上个相同数所在位置,那么只要找出l到r之间前驱值在l之前的数的个数就可以了 本人打的暴力,有一个小技巧,用cha ...
- [bzoj] 2453 维护数列 || 单点修改分块
原题 询问区间有种个颜色,单点修改某个位置. 修改次数<=1000 维护pre[i]为前一个与当前位置颜色一样的位置. 询问时以pre为关键字sort,lower_bound找pre<x的 ...
随机推荐
- SpringMVC相关的面试题
1.什么是springMVC springmvc是spirng框架的一个模块,是一个基于MVC框架的web框架 2.springmvc的流程 a.客户端发送请求 b.前端控制器DispatcherSe ...
- python爬虫调用搜索引擎及图片爬取实战
实战三-向搜索引擎提交搜索请求 关键点:利用搜索引擎提供的接口 百度的接口:wd="要搜索的内容" 360的接口:q="要搜索的内容" 所以我们只要把我们提交给 ...
- C语言问卷调查表
你对自己的未来有什么规划?做了哪些准备? 对未来比较迷茫,现在的主要任务是学好专业课 你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 活到老学到老,学习是一辈子的事.在学习的过程 ...
- DPDK报文分类与访问控制
原创翻译,转载请注明出处. dpdk提供了一个访问控制库,提供了基于一系列分类规则对接收到的报文进行分类的能力.ACL库用来在一系列规则上执行N元组查找,可以实现多个分类和对每个分类查找最佳匹配(最高 ...
- C#中委托的理解
请注意,这只是个人关于C#中委托的一点点理解,参考了一些博客,如有不周之处,请指出,谢谢! 委托是一种函数指针,委托是方法的抽象,方法是委托的实例.委托是C#语言的一道坎,明白了委托才能算是C#真正入 ...
- openssl 基本加密
openssl命令行工具详解(openssl的命令众多,请酌情处理与记忆) 在命令行输入:openssl asdf,可以显示openssl的命令说明 1:Standard commands(标准命 ...
- vue-cli脚手架搭建
我们使用vue-cli来搭建整个项目,vue-cli就是一个脚手架,步骤很简单,输入几个命令之后就会生成整个项目,里面包括了webpack.ESLint.babel很多配置等等,省了很多事 Vue+ ...
- 1st 本周工作量及进度统计
1. 项目:英文文章词频统计 项目类型:个人项目 项目完成情况:已完成 项目日期:2016.9.6 C(类别) C(内容) S(开始时间) ST(结束时间) I(耽误时间) △(实际时间) 分析 需求 ...
- PictureBox使用异常
PictureBox的使用 代码: 显示部分 当我切换不同位置之间的照片时,出现这种问题: 但是当我代码改成下面的代码时,则错误消除 但我并不清楚原因
- 第117天:Ajax实现省市区三级联动
Ajax实现省市区三级联动 思路: (1)首先获取省份信息 (2)发起Ajax请求,注意dataType中的T大写 (3)封装回调函数,回调函数success中用$.each循环每一条数据,动态创建o ...