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的 ...
随机推荐
- 冲刺ing-2
第二次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 分配任务 蔺皓雯 编写博客,查阅资料 蔡晨旸 查阅资料 曾茜 暂无 鲁婧楠 暂无 杨池宇 暂无 成员遇到的问题 队员 问题 吴伟华 暂无 ...
- 按照Right-BICEP要求设计的测试用例
测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...
- Alpha冲刺——第五天
Alpha第五天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- hdu1010--Tempter of the Bone(迷宫)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Jav ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- lintcode-491-回文数
491-回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未必了. 样例 11, 121, 1 ...
- 使用Kettle导出excel
1.开发背景 在web项目中,经常会需要查询数据导出excel,以前比较常见的就是用poi.使用poi的时候也有两种方式,一种就是直接将集合一次性导出为excel,还有一种是分批次追加的方式适合数据量 ...
- MySql中的varchar类型
转载:http://www.cnblogs.com/doit8791/archive/2012/05/28/2522556.html 1.varchar类型的变化 MySQL 数据库的varchar类 ...
- windows查看端口占用指令
1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 协议 本地地址 外部地址 ...
- 在js和C#中split应用和去除字符串分组后的空值
如字符串 string answer="A,B,D,",在 js和 C#按","分成数组 js: , useranswer.length - ).split(& ...