2453: 维护队列

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1079  Solved: 503
[Submit][Status][Discuss]

Description

你小时候玩过弹珠吗?
小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N。为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少。当然,A有时候会依据个人喜好,替换队列中某个弹珠的颜色。但是A还没有学过编程,且觉得头脑风暴太浪费脑力了,所以向你来寻求帮助。

Input

输入文件第一行包含两个整数N和M。
第二行N个整数,表示初始队列中弹珠的颜色。
接下来M行,每行的形式为“Q L R”或“R x c”,“Q L R”表示A想知道从队列第L个弹珠到第R个弹珠中,一共有多少不同颜色的弹珠,“R x c”表示A把x位置上的弹珠换成了c颜色。

Output

对于每个Q操作,输出一行表示询问结果。

Sample Input

2 3
1 2
Q 1 2
R 1 2
Q 1 2

Sample Output

2
1

HINT

对于100%的数据,有1 ≤ N ≤ 10000, 1 ≤ M ≤ 10000,小朋友A不会修改超过1000次,所有颜色均用1到10^6的整数表示。

Source

2011福建集训

带修莫队

#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: 维护队列的更多相关文章

  1. Bzoj 2453: 维护队列 && Bzoj 2120: 数颜色 分块,bitset

    2453: 维护队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 247[Submit][Status][Discuss] ...

  2. bzoj 2453 : 维护队列 带修莫队

    2453: 维护队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 952  Solved: 432[Submit][Status][Discuss] ...

  3. BZOJ 2453 维护队列 | 分块

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2453 题解: 考虑维护每个位置的颜色上一次出现在哪里,计为pre[i],在询问l到r的时候, ...

  4. BZOJ.2453.维护队列([模板]带修改莫队)

    题目链接 带修改莫队: 普通莫队的扩展,依旧从[l,r,t]怎么转移到[l+1,r,t],[l,r+1,t],[l,r,t+1]去考虑 对于当前所在的区间维护一个vis[l~r]=1,在修改值时根据是 ...

  5. 【BZOJ 2453|bzoj 2120】 2453: 维护队列 (分块+二分)

    2453: 维护队列 Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有 ...

  6. BZOJ 2120 数颜色&2453 维护队列 [带修改的莫队算法]【学习笔记】

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 3665  Solved: 1422[Submit][Status][Discuss] ...

  7. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  8. 【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)

    先说正解:把所有相同的数相成一个链在每一个区间里的种数就是不同链的链头,那么记录每个数的上个相同数所在位置,那么只要找出l到r之间前驱值在l之前的数的个数就可以了 本人打的暴力,有一个小技巧,用cha ...

  9. [bzoj] 2453 维护数列 || 单点修改分块

    原题 询问区间有种个颜色,单点修改某个位置. 修改次数<=1000 维护pre[i]为前一个与当前位置颜色一样的位置. 询问时以pre为关键字sort,lower_bound找pre<x的 ...

随机推荐

  1. 欢迎来怼---作业要求 20171015 beta冲刺贡献分分配规则

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 基础分      每人占个人总分的百分之40% leangoo里面的得分    每人占个人总分里 ...

  2. Java中的静态变量static

    package com.wangcf; public class Test { String name="你好"; static String sex="男"; ...

  3. java调试器

    javac.exe是编译.java文件 java.exe是执行编译好的.class文件 javadoc.exe是生成Java说明文档 jdb.exe是Java调试器 javaprof.exe是剖析工具 ...

  4. Nginx 使用札记

    nginx是什么? nginx是俄罗斯人 Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的一个十分轻量级的HTTP服务器.它是一个高性能的HTTP和反向代理服务器,同时也可以作 ...

  5. Varnish是一款高性能的开源HTTP加速器

    如何衡量缓存系统的优劣性 1:缓存命中率: 在memcached服务器中,get_hits的值表示缓存命中的次数,get_misses的值表示没有命中的次数,那么命中率的计算公式就是:命中率=get_ ...

  6. 初学Objective - C语法之代码块(block)

    一.block声明 1.无参数,无返回值: void (^sayHi)(); 2.有参数,有返回值: NSInteger (^operateOfValue)(NSInteger num); block ...

  7. 【Python】python操作mysql

    pymysql模块对mysql进行 import pymysql # 创建连接 conn = pymysql.connect(host=, user='root', passwd='root', db ...

  8. 【bzoj1067】[SCOI2007]降雨量 倍增RMQ

    题目描述 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2004和200 ...

  9. 【JavaScript&jQuery】返回顶部

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. P4622 [COCI2012-2013#6] JEDAN

    题目背景 COCI 题目描述 有N个数排成一行(数值代表高度),最初所有的数都为零,你可以选择连续的一段等高的数,将它们都增加1(除了开头和结尾那个数)如下图表示了两次操作: 现在有一些数字看不清了, ...