#include #include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(int)(n); i++) using Modint = atcoder::static_modint<998244353>; int SegN = 1<<18; struct PersistentSegtreeNode{ PersistentSegtreeNode *l; PersistentSegtreeNode *r; int sum; static PersistentSegtreeNode* up( PersistentSegtreeNode* l, PersistentSegtreeNode* r ){ PersistentSegtreeNode* res = new PersistentSegtreeNode(); res->l = l; res->r = r; res->sum = l->sum + r->sum; return res; } }; PersistentSegtreeNode* nodeSeg(int val){ PersistentSegtreeNode* res = new PersistentSegtreeNode(); res->l = res->r = nullptr; res->sum = val; return res; } PersistentSegtreeNode* initSeg(int a, int b){ if(b-a == 1) return nodeSeg(0); int mid = (a + b) / 2; return PersistentSegtreeNode::up(initSeg(a, mid), initSeg(mid, b)); } PersistentSegtreeNode* setSeg( PersistentSegtreeNode* p, int pos, int val, int a = -1, int b = -1 ){ if(a == -1){ a = 0; b = SegN; } assert(a <= pos); assert(pos < b); if(b-a == 1) return nodeSeg(val); int mid = (a + b) / 2; if(pos < mid){ return PersistentSegtreeNode::up( setSeg(p->l, pos, val, a, mid), p->r ); } else{ return PersistentSegtreeNode::up( p->l, setSeg(p->r, pos, val, mid, b) ); } } int main(){ int N; cin >> N; vector P(N); rep(i,N) cin >> P[i]; vector I(N); rep(i,N) I[P[i]-1] = i; vector seg; seg.push_back(initSeg(0, SegN)); rep(i,N) seg.push_back(setSeg(seg.back(), I[i], 1)); int Q; cin >> Q; rep(i,Q){ int u,v; cin >> u >> v; int x = v-1; int a = 0, b = SegN; vector> T; T.push_back({ seg[v], 1 }); while(b-a > 1){ int m = (a+b) / 2; int tmp = 0; for(auto& Tn : T){ auto q = Tn.first; auto w = Tn.second; tmp += q->l->sum * w; } if(tmp <= u){ a = m; for(auto& Tn : T) Tn.first = Tn.first->r; u -= tmp; } else{ b = m; for(auto& Tn : T) Tn.first = Tn.first->l; } } int ans = max(a, I[x]) + 1; cout << ans << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } ios_do_not_sync_instance;