164372025-04-30 15:32:33algoproSzökőkútcpp17Wrong answer 30/100141ms14132 KiB
// UUID: 33a4b86b-dd63-4b07-95fd-c9e3818ff33d
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, q;cin>>n>>q;
	vector<int> a(n);
	vector<int> c(n);
	vector<int> hova(n, -1);
	stack<int> s;
	for(int i=0;i<n;i++){
		cin>>a[i]>>c[i];
		while(!s.empty() && a[s.top()]<a[i]){
			hova[s.top()]=i;
			s.pop();
		}
		s.push(i);
	}
	vector<vector<long long>> prefix(n);
	vector<vector<int>> index(n);
	vector<bool> visited(n);
	vector<int> holtalalom(n, -1);
	vector<int> hanyadikban(n, -1);
	for(int i=0;i<n;i++){
		if(visited[i]){
			continue;
		}else{
			prefix[i].push_back(0);
			index[i].push_back(0);
			visited[i]=true;
			int x=i;
			while(x!=-1){
				prefix[i].push_back(c[x]+prefix[i].back());
				index[i].push_back(x);
				x=hova[x];
				if (x == -1) break;
				if(visited[x]){
					break;
				}
				visited[x]=true;
				holtalalom[x]=i;
				hanyadikban[x]=prefix[i].size()-1;
			}
		}
	}
	while(q--){
		int x, y;cin>>x>>y;
		if(holtalalom[x-1]!=-1){
			int h=holtalalom[x-1], z=hanyadikban[x-1];
			int ossz=prefix[h][z];
			y+=ossz;
			int hi=lower_bound(prefix[h].begin(), prefix[h].end(), y)-prefix[h].begin();
			if(hi==prefix[h].size()){
				cout<<0<<'\n';
			}else{
				cout<<index[h][hi]+1<<'\n';
			}
		}else{
			int hi=lower_bound(prefix[x-1].begin(), prefix[x-1].end(), y)-prefix[x-1].begin();
			if(hi==prefix[x-1].size()){
				cout<<0<<'\n';
			}else{
				cout<<index[x-1][hi]+1<<'\n';
			}
		}
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/30
1Wrong answer1ms508 KiB
2Wrong answer1ms316 KiB
3Wrong answer2ms316 KiB
4Wrong answer2ms316 KiB
5Accepted2ms316 KiB
6Wrong answer2ms316 KiB
7Wrong answer2ms564 KiB
subtask230/30
1Accepted94ms8928 KiB
2Accepted101ms8632 KiB
subtask30/40
1Wrong answer2ms720 KiB
2Wrong answer59ms5940 KiB
3Accepted135ms9944 KiB
4Wrong answer123ms10548 KiB
5Wrong answer116ms10804 KiB
6Wrong answer141ms14132 KiB