牛課練習賽34 Flittle w and Discretization 主席樹維護Mex
阿新 • • 發佈:2018-12-15
主席樹維護Mex。
每個右端點 r 維護出一棵 在[1, r ] 區間中 其他所有的 值離這個 r 最近的的位置是多少。
然後詢問區間[L,R]的時候,從rt[R] 出發,然後如果左兒子的中所有出線位置的最小值 >= L, 則說明他們所有的點都出線在這個區間內了,然後往右走。
否則就說明左邊有點沒有出線過,需要往左走。
程式碼:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #defineView CodeLL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N =3e5+ 100; struct Node{ int ls, rs, mn, num; Node(){ls = rs = mn = num = 0;} }tr[N<<5]; int rt[N]; int tot; int build(int l, intr){ int x = ++tot; if(l == r) return x; int m = l+r >> 1; tr[x].ls = build(l,m); tr[x].rs = build(m+1,r); return x; } int Update(int L, int p, int lst, int l, int r){ int x = ++tot; tr[x] = tr[lst]; if(l == r){ tr[x].mn = p; ++tr[x].num; return x; } int m = l+r >> 1; if(L <= m) tr[x].ls = Update(L, p, tr[lst].ls, l, m); else tr[x].rs = Update(L, p, tr[lst].rs, m+1, r); tr[x].mn = min(tr[tr[x].ls].mn, tr[tr[x].rs].mn); tr[x].num = tr[tr[x].ls].num + tr[tr[x].rs].num; return x; } int Query(int L, int x, int x2){ // cout << L << ' ' << tr[x].mn << ' ' << tr[x].num - tr[x2].num << endl; if(tr[x].mn >= L) return tr[x].num - tr[x2].num; if(!tr[x].ls) return 0; int ret = 0; if(tr[tr[x].ls].mn >= L) ret = tr[tr[x].ls].num-tr[tr[x2].ls].num + Query(L,tr[x].rs,tr[x2].rs); else ret = Query(L,tr[x].ls,tr[x2].ls); return ret; } void Q(int p, int l, int r){ cout << l << " " << r << " " << tr[p].mn << ' ' << tr[p].num << endl; if(l == r) return ; int m = l+r >> 1; Q(tr[p].ls, l, m); Q(tr[p].rs, m+1, r); } int a[N]; int main(){ int n, m; scanf("%d", &n); rt[0] = build(1,n); for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); if(a[i] <= n) rt[i] = Update(a[i], i, rt[i-1], 1, n); else rt[i] = rt[i-1]; } // Q(rt[4],1,n); scanf("%d", &m); int L, R; for(int i = 1; i <= m; ++i){ scanf("%d%d", &L, &R); printf("%d\n", (R-L+1)-Query(L, rt[R], rt[L-1])); } return 0; }