164392025-04-30 15:35:08algoproSzökőkútcpp17Runtime error 60/100493ms262144 KiB
// UUID: 319d183e-f24e-44a8-874a-1fdd5faccb91
#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);
			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;
			}
			visited[i]=true;
		}
	}
	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
subtask130/30
1Accepted1ms316 KiB
2Accepted2ms316 KiB
3Accepted2ms508 KiB
4Accepted2ms564 KiB
5Accepted2ms316 KiB
6Accepted3ms1336 KiB
7Accepted2ms564 KiB
subtask230/30
1Accepted92ms8972 KiB
2Accepted101ms8636 KiB
subtask30/40
1Accepted2ms572 KiB
2Runtime error493ms262144 KiB
3Accepted136ms9928 KiB
4Runtime error479ms262144 KiB
5Accepted165ms21580 KiB
6Accepted143ms18228 KiB