Submission #909869

#TimeUsernameProblemLanguageResultExecution timeMemory
909869kachuFountain (eJOI20_fountain)C++17
30 / 100
72 ms12268 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); } vector<pii> query[N + 1]; int ans[Q]; for (int i = 0; i < Q; i++){ int a, x; cin >> a >> x; query[a].pb(mp(x, i)); } bool visited[N + 1]; memset(visited, 0, sizeof visited); for (int i = 1; i <= N; i++){ if (visited[i]) continue; pque<pii, vector<pii>, greater<pii>> pq; int ptr = i, prev = 0, cap = 0; while (ptr != 0){ if (visited[ptr]) break; visited[ptr] = 1; cap += C[ptr]; for (auto qu : query[ptr]) pq.push(mp(qu.first + prev, qu.second)); while (!pq.empty() && pq.top().first <= cap){ int qid = pq.top().second; pq.pop(); ans[qid] = ptr; } prev += C[ptr]; ptr = next[ptr]; } while (!pq.empty()){ int qid = pq.top().second; pq.pop(); ans[qid] = 0; } } for (int i = 0; i < Q; i++) cout << ans[i] << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...