1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 500006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) #define deadoralive puts("alive.") typedef long long ll; int n , m; int A[MAXN];
struct que { int l , r , id; bool operator < ( const que x ) const { return r > x.r; } } Q[MAXN] ;
bool cmp( que a , que b ) { return a.r == b.r ? a.l < b.l : a.r < b.r; }
int T[MAXN << 2] , idx[MAXN << 2]; void pu( int rt ) { if( T[rt << 1] < T[rt << 1 | 1] ) T[rt] = T[rt << 1] , idx[rt] = idx[rt << 1]; else T[rt] = T[rt << 1 | 1] , idx[rt] = idx[rt << 1 | 1]; } int tim; void mdfy( int rt , int l , int r , int p , int c ) { ++ tim; if( l == r ) { T[rt] = c , idx[rt] = l; return; } int m = l + r >> 1; if( p <= m ) mdfy( rt << 1 , l , m , p , c ); else mdfy( rt << 1 | 1 , m + 1 , r , p , c ); pu( rt ); } int qwq; pii que( int rt , int l , int r , int L , int R ) { if( L <= l && R >= r ) { return mp( T[rt] , idx[rt] ); } int m = l + r >> 1; pii ret = mp( 0x3f3f3f3f , 0 ); if( L <= m ) ret = min( ret , que( rt << 1 , l , m , L , R ) ); if( R > m ) ret = min( ret , que( rt << 1 | 1 , m + 1 , r , L , R ) ); return ret; }
int last[MAXN] , ans[MAXN];
void solve() { cin >> n; rep( i , 1 , n ) scanf("%d",A + i); cin >> m; rep( i , 1 , m ) { scanf("%d%d",&Q[i].l,&Q[i].r) , Q[i].id = i; } sort( Q + 1 , Q + m + 1 , cmp ); int cur = 1 , x , y; memset( T , 0x3f , sizeof T ); for( int i = 1 ; i <= n ; ++ i ) { qwq = i; if( last[A[i]] ) mdfy( 1 , 1 , n , last[A[i]] , 0x3f3f3f3f ); mdfy( 1 , 1 , n , i , last[A[i]] + 1 ); last[A[i]] = i; while( cur <= m && Q[cur].r == i ) { pii re = que( 1 , 1 , n , Q[cur].l , n ); ans[Q[cur].id] = re.fi > Q[cur].l ? 0 : A[re.se]; ++ cur; } } rep( i , 1 , m ) printf("%d\n",ans[i]); }
signed main() {
solve(); }
|