Submission #909837

#TimeUsernameProblemLanguageResultExecution timeMemory
909837kachuFountain (eJOI20_fountain)C++17
60 / 100
750 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]; map<int, int> 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][C[ptr]] = ptr; } else{ prev[ptr] = (--v[i].end())->first; v[i][prev[ptr] + C[ptr]] = ptr; } ptr = next[ptr]; } } while (Q--){ int a, x; cin >> a >> x; x = x + prev[a]; a = root[a]; auto ans = v[a].lower_bound(x); if (ans == v[a].end()) cout << 0 << endl; else cout << (*ans).second << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...