2120: 数颜色

2120: 数颜色

Time Limit: 6 Sec  Memory Limit: 259 MB
Submit: 4151  Solved: 1644
[Submit][Status][Discuss]

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6

Sample Output

4
4
3
4

HINT

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。


2016.3.2新加数据两组by Nano_Ape

Source

带修改区间莫队

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1e4+5,F=1e3+5,Z=1e6+5;
int n,m,a[N],bsize,qcnt,mcnt;
int nowans,ans[N],vcnt[Z];
struct Q{
    int l,r,t,id;
    bool operator <(const Q &a)const{
        return l/bsize!=a.l/bsize ? l/bsize<a.l/bsize :
               r/bsize!=a.r/bsize ? r/bsize<a.r/bsize :
               t<a.t;
    }
}q[N];
struct M{int pos,s,t;}mod[F];
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void ins(int x){
    nowans+=vcnt[x]++==0;
}
inline void del(int x){
    nowans-=--vcnt[x]==0;
}
void mo_solve(){
    int l=1,r=0,t=mcnt;
    for(int i=0;i<qcnt;i++){
        while(r<q[i].r) ins(a[++r]);
        while(l>q[i].l) ins(a[--l]);
        while(r>q[i].r) del(a[r--]);
        while(l<q[i].l) del(a[l++]);
        while(t<q[i].t){//时间推演 
            if(l<=mod[t].pos&&mod[t].pos<=r) del(mod[t].s),ins(mod[t].t);
            a[mod[t].pos]=mod[t].t;
            t++;
        }
        while(t>q[i].t){//时间倒流 
            t--;
            if(l<=mod[t].pos&&mod[t].pos<=r) del(mod[t].t),ins(mod[t].s);
            a[mod[t].pos]=mod[t].s;
        }
        ans[q[i].id]=nowans;
    }
}
int main(){
    freopen("nt2011_color.in","r",stdin);
    freopen("nt2011_color.out","w",stdout);
    n=read();m=read();
    bsize=pow(n,2.0/3.0);
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1,x,y;i<=m;i++){
        char ch=0;
        while(ch!='Q'&&ch!='R') ch=getchar();
        x=read();y=read();
        if(ch=='Q')
            q[qcnt]=(Q){x,y,mcnt,qcnt++};
        else
            mod[mcnt++]=(M){x,a[x],y},a[x]=y; 
    }
    sort(q,q+qcnt);
    mo_solve();
    for(int i=0;i<qcnt;i++) printf("%d
",ans[i]);
    return 0;
} 
原文地址:https://www.cnblogs.com/shenben/p/6435752.html