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. AJAX请求.net controller数据交互过程

    AJAX发出请求 $.ajax({ url: "/Common/CancelTaskDeal", //CommonController下的CancelTaskDeal方法 type ...

  2. 2018软工实践—Alpha冲刺(1)

    o## 队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作,对多个目标检测及文字识别模型进行评估.实验 ...

  3. P4语法(4)Control block

    Control block Control block之中用于放置设计好的Table和Action. 可以把control block认为是pipeline的一个模板,之前用的v1model中就是in ...

  4. Alpha-9

    前言 失心疯病源9 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 熬夜肝代码 代码签入github 明天的计划 最好能够完成对接环节 准备展示内容 还剩下哪 ...

  5. lintcode-451-两两交换链表中的节点

    451-两两交换链表中的节点 给一个链表,两两交换其中的节点,然后返回交换后的链表. 样例 给出 1->2->3->4, 你应该返回的链表是 2->1->4->3. ...

  6. WebService(三)

    JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...

  7. perf的采样模式和统计模式

    perf的采样模式和统计模式 统计模式和采样模式使用寄存器的方法不相同; 在统计模式下,每次调度之前设置寄存器,调度之后清理寄存器,留个下个进程使用;PMU寄存器的使用方法; 在采样模式下,每次 pm ...

  8. J2EE十三种技术规范介绍

    J2EE的十三个技术规范 J2EE体系结构 一.JDBC:Java Data Base Connectivity,数据库连接 我们大家对微软公司的ODBC数据库访问接口比较熟悉,而在Java中创建数据 ...

  9. DataTable Excel 导出:

    public static class CSVFileHelper { public static string ToHtmlTable(this DataTable target) { return ...

  10. 【bzoj1297】[SCOI2009]迷路 矩阵乘法

    题目描述 给出一个 $n$ 个点的有向图,每条边的权值都在 $[1,9]$ 之间.给出 $t$ ,求从 $1$ 到 $n$ ,经过路径边权和恰好为 $t$ 的方案数模2009. 输入 第一行包含两个整 ...