제출 #909848

#제출 시각아이디문제언어결과실행 시간메모리
909848kachuFountain (eJOI20_fountain)C++17
60 / 100
523 ms524288 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> 
#define ofind find_by_order
#define okey order_of_key

#define int long long
#define double long double
#define pque priority_queue
#define dque deque
#define que queue
#define umap unordered_map
#define uset unordered_set
#define pipii pair<int, pair<int,int>>
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define iter iterator
#define endl '\n'
#define MOD 1000000007
#define INF 1e18 

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int N, Q;
	cin >> N >> Q;
	int D[N + 1], C[N + 1];
	
	for (int i = 1; i <= N; i++)
		cin >> D[i] >> C[i];
	
	int next[N + 1];
	stack<int> st;
	
	for (int i = N; i >= 1; i--){
		while (!st.empty() && D[st.top()] <= D[i])
			st.pop();
		
		if (!st.empty())
			next[i] = st.top();
		else
			next[i] = 0;
		
		st.push(i);
	}
	
	bool visited[N + 1];
	vector<pii> v[N + 1];
	int root[N + 1], prev[N + 1];
	
	memset(visited, 0, sizeof visited);
	
	for (int i = 1; i <= N; i++){
		if (visited[i]) continue;
		
		int ptr = i;
		while (ptr != 0){
			visited[ptr] = 1;
			root[ptr] = i;
			
			if (v[i].empty()){
				prev[ptr] = 0;
				v[i].pb(mp(C[ptr], ptr));
			}
			else{
				prev[ptr] = v[i].back().first;
				v[i].pb(mp(prev[ptr] + C[ptr], ptr));
			}
			
			ptr = next[ptr];
		}
	}
	
	while (Q--){
		int a, x;
		cin >> a >> x;
		
		x = x + prev[a];
		a = root[a];
		
		int low = 0, high = v[a].size() - 1;
		
		while (low <= high){
			int mid = (low + high) / 2;
			if (v[a][mid].first < x)
				low = mid + 1;
			else
				high = mid - 1;
		}

		if (high + 1 == (int) v[a].size())
			cout << 0 << endl;
		else
			cout << v[a][high + 1].second << endl;
	}

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...