cf1000F. One Occurrence(線段樹 set)

自為風月馬前卒發表於2019-01-06

題意

題目連結

Sol

(真後悔沒打這場EDU qwq)

首先把詢問離線,預處理每個數的(pre, nxt),同時線段樹維護(pre)(下標是(pre),值是(i)),同時維護一下最大值

那麼每次在((1, l – 1))內查詢最大值,如果最大值(>= l),那麼說明合法

但是(pre)可能會有相同的情況(0),直接開個set維護一下

然後用vector對(nxt)維護一個類似差分的東西,在(nxt_i)的位置刪除掉(i)的影響

// luogu-judger-enable-o2
/*
*/
#include<bits/stdc++.h> 
#define LL long long
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second 
using namespace std;
const int MAXN = 2e6 + 10;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}

int N, M, a[MAXN], pre[MAXN], nxt[MAXN], ans[MAXN], date[MAXN], num = 0;
vector<Pair> q[MAXN];
void Des() {
    for(int i = 1; i <= N; i++) date[i] = a[i];
    sort(date + 1, date + N + 1);
    num = unique(date + 1, date + N + 1) - date - 1;
    for(int i = 1; i <= N; i++) a[i] = lower_bound(date + 1, date + num + 1, a[i]) - date;
}
void Get() {
    static int las[MAXN];
    for(int i = 1; i <= N; i++) pre[i] = las[a[i]], las[a[i]] = i;
    for(int i = 1; i <= N; i++) las[i] = N + 1;
    for(int i = N; i >= 1; i--) nxt[i] = las[a[i]], las[a[i]] = i;
}
#define Getmid ((T[k].l + T[k].r) >> 1)
#define ls k << 1
#define rs k << 1 | 1
struct Node {
    int l, r, mx;
}T[MAXN];
void update(int k) {
    T[k].mx = max(T[ls].mx, T[rs].mx);
}
void Build(int k, int ll, int rr) {
    T[k].l = ll; T[k].r = rr; T[k].mx = 0;
    if(ll == rr) return ;
    int mid = Getmid;
    Build(ls, ll, mid); Build(rs, mid + 1, rr);
}
void Modify(int k, int pos, int v) {
    if(T[k].l == T[k].r) {T[k].mx = v; return ;}
    int mid = Getmid;
    if(pos <= mid) Modify(ls, pos, v);
    if(pos  > mid) Modify(rs, pos, v);
    update(k);
}
int Query(int k, int ll, int rr) {
    if(ll <= T[k].l && T[k].r <= rr) return T[k].mx;
    int mid = Getmid, ans = 0;
    if(ll <= mid) chmax(ans, Query(ls, ll, rr));
    if(rr  > mid) chmax(ans, Query(rs, ll, rr));
    return ans;
}
#undef ls
#undef rs 
#undef Getmid
void Solve() {
    set<int> s;
    static vector<int> v[MAXN];
    for(int i = 1; i <= N; i++) {
        for(int j = 0; j < v[i].size(); j++) {
            if(!pre[v[i][j]]) 
                s.erase(v[i][j]);
            else Modify(1, pre[v[i][j]], 0);
        }
        if(!pre[i]) 
            s.insert(i);
        else Modify(1, pre[i], i);
            
        v[nxt[i]].push_back(i);
        for(int j = 0; j < q[i].size(); j++) {
            int t = Query(1, 1, q[i][j].fi - 1);
            if(t >= q[i][j].fi) ans[q[i][j].se] = date[a[t]];
            if(!s.empty()) {
                set<int>::iterator it = s.end(); it--;
                if(*it >= q[i][j].fi) ans[q[i][j].se] = date[a[*it]];
            }
            
        }
            
    }
}
signed main() {
    N = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    Des();
    Get();
    Build(1, 1, N + 1);
    M = read();
    for(int i = 1; i <= M; i++) {
        int l = read(), r = read();
        q[r].push_back(MP(l, i));
    }
    Solve();
    for(int i = 1; i <= M; i++) printf("%d
", ans[i]);
    return 0;
}
/*
5
1 2 2 1 1
2
1 5
2 3 


10
5 9 6 4 8 7 4 9 7 6
1
4 8
*/

相關文章