Dynamic len

来源:互联网 发布:python自学 编辑:程序博客网 时间:2024/05/15 06:10

Description

有n个数编号从0→n-1,两种操作:
Q L R:询问编号为L→R-1的数中共有多少种不同的数
M X Y:将编号为X的数改为Y
共有m个操作
n,m<=50000

Solution

有点懒,第一题复制题意。(简洁明了)
显然原题
直接带修莫队。不解释

Code

#include<cstdio>#include<cstring>#include<algorithm>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)#define N 50005#define M 1305using namespace std;struct note{int l,r,id,x;}ask[N];struct node{int x,pre,v;}p[N];int n,m,x,y,ans,tot,top,pl,pr,now,an[N],c[N],pos[N],last[N],num[N*20];bool vis[N];char s[1];bool cmp(note x,note y){    return pos[x.l]<pos[y.l]||    pos[x.l]==pos[y.l]&&pos[x.r]<pos[y.r]||    pos[x.l]==pos[y.l]&&pos[x.r]==pos[y.r]&&x.x<y.x;}void updata(int x) {    if (vis[x]) {        if (!--num[c[x]]) ans--;    } else if (++num[c[x]]==1) ans++;    vis[x]^=1;}void change(int x,int y) {    if (vis[x]) {        updata(x);c[x]=y;updata(x);    } else c[x]=y;}int main() {    freopen("len.in","r",stdin);    freopen("len.out","w",stdout);    scanf("%d%d",&n,&m);    fo(i,1,n) scanf("%d",&c[i]),last[i]=c[i],pos[i]=(i+1)/M;    fo(i,1,m) {        scanf("%s%d%d",s,&x,&y);x++;        if (s[0]=='Q') ask[++tot].id=tot,ask[tot].l=x,ask[tot].r=y,ask[tot].x=top;        else p[++top].x=x,p[top].pre=last[x],p[top].v=y,last[x]=y;    }    sort(ask+1,ask+tot+1,cmp);pl=1;    fo(i,1,tot) {        if (now<ask[i].x) fo(j,now+1,ask[i].x) change(p[j].x,p[j].v);        else fd(j,now,ask[i].x+1) change(p[j].x,p[j].pre);        if (pl<ask[i].l) fo(j,pl,ask[i].l-1) updata(j);        else fo(j,ask[i].l,pl-1) updata(j);        if (pr<ask[i].r) fo(j,pr+1,ask[i].r) updata(j);        else fo(j,ask[i].r+1,pr) updata(j);        an[ask[i].id]=ans;pl=ask[i].l;pr=ask[i].r;now=ask[i].x;    }    fo(i,1,tot) printf("%d\n",an[i]);}
0 0