#include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } #include using Modint = atcoder::static_modint<998244353>; using namespace std; #include namespace nachia { template< class S, S(*op)(S a, S b) > struct PersistentSegtree { struct Node { int l, r; S v; }; int xN; int N, logN; S me; vector V; void merge(int p){ V[p].v = op(V[V[p].l].v, V[V[p].r].v); } PersistentSegtree() {} PersistentSegtree(int z, S e) : xN(z), N(1), logN(0), me(e) { while(N < z){ N *= 2; logN++; } V.assign(N*2-1, { -1,-1,me }); for(int i=N-2; i>=0; i--){ V[i].l = i*2+1; V[i].r = i*2+2; } } PersistentSegtree(const vector& v, S e) : PersistentSegtree(v.size(), e) { for(int i=0; i=0; i--) merge(i); } int copy_node(int p){ V.push_back(V[p]); return V.size() - 1; } int set(int t, int p, S v){ vector P; P.push_back(copy_node((t == -1) ? 0 : t)); for(int d=logN-1; d>=0; d--){ int pp = P.back(); if(p & (1<=0; i--) merge(P[i]); return P.front(); } int where(int t, int p){ int s = (t == -1) ? 0 : t; for(int d=logN-1; d>=0; d--) s = ((p & (1<=0; d--) s = ((p & (1< G; G.reserve(logN*2+2); int s = (t == -1) ? 0 : t; G.push_back({ 0,N,s }); S res = me; while(G.size()){ DFSV g = G.back(); G.pop_back(); int a = g.l, b = g.r, i = g.i; if(r <= a || b <= l) continue; if(l <= a && b <= r){ res = op(res, V[i].v); continue; } G.push_back({ (a+b)/2, b, V[i].r }); G.push_back({ a, (a+b)/2, V[i].l }); } return res; } private: template int minLeft2(int r, E cmp, int i, int a = 0, int b = 0) const { static S x; if(b == 0){ a=0; b=N; x=me; } if(r <= a) return a; if(b <= r){ S nx = op(V[i].v, x); if(cmp(nx)){ x = nx; return a; } } if(b-a == 1) return b; int q = minLeft2(r, cmp, V[i].r, (a+b)/2, b); if(q > (a+b)/2) return q; return minLeft2(r, cmp, V[i].l, a, (a+b)/2); } template int maxRight2(int l, E cmp, int i, int a = 0, int b = 0) const { static S x; if(b == 0){ a=0; b=N; x=me; } if(b <= l) return b; if(l <= a){ S nx = op(x, V[i].v); if(cmp(nx)){ x = nx; return b; } } if(b-a == 1) return a; int q = maxRight2(l, cmp, V[i].l, a, (a+b)/2); if(q < (a+b)/2) return q; return maxRight2(l, cmp, V[i].r, (a+b)/2, b); } public: // bool cmp(S) template int minLeft(int t, int r, E cmp) const { int s = (t == -1) ? 0 : t; return minLeft2(r, cmp, s); } // bool cmp(S) template int maxRight(int t, int l, E cmp) const { int s = (t == -1) ? 0 : t; int x = maxRight2(l, cmp, s); return x > xN ? xN : x; } }; } int maxop(int l, int r){ return max(l,r); } void testcase(){ int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; using Ds = nachia::PersistentSegtree; vector ds; rep(t,3) ds.emplace_back(vector(N+1, N+1), -1); vector> T(N); { vector> B(N); rep(i,N) B[i].fill(N+1); array t; t.fill(-1); for(int i=N-1; i>=0; i--){ if(A[i] < N){ auto& b = B[A[i]]; b[2] = b[1]; b[1] = b[0]; b[0] = i+1; rep(s,3) t[s] = ds[s].set(t[s], A[i], b[s]); } T[i] = t; } } int Q; cin >> Q; int ans = 0; rep(qi,Q){ int a,b; cin >> a >> b; int l = a ^ ans; int r = b ^ ans; l--; int C[3] = {}; rep(s,3) C[s] = ds[s].maxRight(T[l][s], 0, [r](int x){ return x <= r; }); ans = C[0] + C[1] + C[2]; rep(s,3) if(C[s] == 0) chmin(ans, r-l-3+s); cout << ans << '\n'; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }