题解 CF1000F 【One Occurrence】

· · 题解

蒟蒻表示并不会线段树的做法,于是用莫队硬是卡了过去。。
莫队的板子就不说了,讲一下思路吧。

首先当然是要记录区间中每个数的出现次数。
若更新后这个数的出现次数变为1,就\text{add}一下(下面具体说)
如果以前是1,现在不是了,就\text{del}掉。

这里我用了一个类似栈的东西,每\text{add}进来一个数,就堆到顶上去,同时记录下来这个数的位置。
每次\text{del}一个数时,把栈顶的元素移到被删的数的位置。
对于每次查询的答案,直接取栈顶元素即可。
如果栈为空也不用担心,这时取出来的就是0,不影响答案正确性。

可以看出,上面的操作是\Theta(1)的。
然后大力莫队+卡常,这题就可以通过啦qwq

最后注意一点:
对询问排序,一定要用奇偶排序优化!
常数可以小大约一半。

时间复杂度:\Theta(n^\frac{3}{2})

上代码:

#pragma GCC optimize (3)
#pragma GCC optimize ("Ofast")
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 500007
#define reg register
using namespace std;

struct query{
    int l,r,id;
}q[N];

int a[N],cnt[N],pos[N],be[N];
int stk[N],ans[N];
int n,m,top,unit;

inline void read(int &x);
void print(int x);
inline bool cmp(query x,query y);
inline void add(int t);
inline void del(int t);
inline char gc();

int main(){
    reg int l,r;
    read(n);
    unit = sqrt(n);
    for(reg int i=1;i<=n;++i){
        read(a[i]);
        be[i] = i/unit+1;
    }
    read(m);
    for(reg int i=1;i<=m;++i){
        read(q[i].l),read(q[i].r);
        q[i].id = i;
    }
    sort(q+1,q+1+m,cmp);
    l = r = 1;
    add(a[1]);
    for(reg int i=1;i<=m;++i){
        while(r<q[i].r) add(a[++r]);
        while(r>q[i].r) del(a[r--]);
        while(l<q[i].l) del(a[l++]);
        while(l>q[i].l) add(a[--l]);
        ans[q[i].id] = stk[top];
    }
    for(reg int i=1;i<=m;++i){
        print(ans[i]);
        putchar('\n');
    }
    return 0;
} 

inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void add(int t){
    ++cnt[t];
    if(cnt[t]==1){
        stk[++top] = t; //放到栈顶
        pos[t] = top; //更新栈顶元素所在位置
    }else if(cnt[t]==2){
        stk[pos[t]] = stk[top]; //栈顶元素替换t的位置
        pos[stk[top]] = pos[t]; //把位置更新一下
        stk[top--] = pos[t] = 0; //原来的地方要清0
    }
}

inline void del(int t){
    --cnt[t];
    if(cnt[t]==1){
        stk[++top] = t;
        pos[t] = top;
    }else if(!cnt[t]){
        stk[pos[t]] = stk[top];
        pos[stk[top]] = pos[t];
        stk[top--] = pos[t] = 0;
    }
}

inline bool cmp(query a,query b){ 
    return (be[a.l]^be[b.l])?a.l<b.l:((be[a.l]&1)?a.r<b.r:a.r>b.r); //传说中的奇偶排序优化
}

inline void read(int &x){
    x = 0;
    char c = gc();
    while(c<'0'||c>'9') c = gc();
    while(c>='0'&&c<='9'){
        x = (x<<3)+(x<<1)+(c^48);
        c = gc();
    }
} 

void print(int x){
    if(x>9) print(x/10);
    putchar(x%10+'0');
}