1. 程式人生 > >P1903 數顏色

P1903 數顏色

tps ace || stream algo () pri names ons

題目

帶修莫隊題。

在詢問上多加一個變量,記錄是在那次修改之後的。

然後暴力修改。

就沒了。

不過有一些修改的小技巧

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int manx=50010;
int read()//千萬不要用快讀
{
    char c=getchar();
    int res=0;
    while(c>'9'||c<'0') c=getchar();
    while
(c>='0'&&c<='9') { res=(res<<3)+(res<<1)+c-'0'; c=getchar(); } return res; } bool judge()//我是貞德毒瘤 { char c=getchar(); while(c<'A'||c>'Z') c=getchar(); if(c=='Q') return false; return
true; } struct node { int l,r; int t;//哪一次修改之後 int pos; }; int qua; bool compare(const node &a,const node &b) { return a.l/qua==b.l/qua ? a.r<b.r : a.l < b.l; } node query[manx]; int cha[manx][2],t1,t2;//t1為修改次數,t2為詢問次數 int base[manx]; int cont[manx<<5]; int ans[manx]; int
l=1,r=0; int answer;//簡單的莫隊一些所需要的 void osc(int t)//調整時間 { while(t1<t) { t1++; if(cha[t1][0]>=l&&cha[t1][0]<=r) { cont[cha[t1][1]]+=1,cont[base[cha[t1][0]]]-=1;//如果在當前莫隊的區間內,就要單獨處理 if(cont[cha[t1][1]]==1) answer+=1; if(cont[base[cha[t1][0]]]==0) answer-=1; } swap(cha[t1][1],base[cha[t1][0]]);//這裏便是一個小技巧,直接交換,我們可以自己稍微模擬一下233,這樣是不改變正確性的。 } while(t1>t)//刪除同理,很具有對稱性 { swap(cha[t1][1],base[cha[t1][0]]); if(cha[t1][0]>=l&&cha[t1][0]<=r) { cont[cha[t1][1]]-=1,cont[base[cha[t1][0]]]+=1; if(cont[cha[t1][1]]==0) answer-=1; if(cont[base[cha[t1][0]]]==1) answer+=1; } t1--; } return ; } void add(int pos) { if(!cont[base[pos]]) answer+=1; cont[base[pos]]+=1; return; }//簡單的單點修改 void del(int pos) { cont[base[pos]]-=1; if(!cont[base[pos]]) answer-=1; return ; } int main() { int n=read(),m=read(); qua=pow(n,0.5); for(int i=1;i<=n;i++) base[i]=read(); int a,b; for(int i=1;i<=m;i++) { bool f=judge(); a=read(),b=read(); if(f) cha[++t1][0]=a,cha[t1][1]=b; else query[++t2].l=a,query[t2].r=b,query[t2].t=t1,query[t2].pos=t2; } sort(query+1,query+t2+1,compare); t1=0; for(int i=1;i<=t2;i++) { osc(query[i].t);//除了這一句和上面錄入數據不一樣,其他和普通莫隊就是一樣了233 while(r<query[i].r) add(++r); while(r>query[i].r) del(r--); while(l<query[i].l) del(l++); while(l>query[i].l) add(--l); ans[query[i].pos]=answer; } for(int i=1;i<=t2;i++) printf("%d\n",ans[i]); }

P1903 數顏色