제출 #585297

#제출 시각아이디문제언어결과실행 시간메모리
585297algorithm16Fountain (eJOI20_fountain)C++14
100 / 100
643 ms25848 KiB
#include <bits/stdc++.h> #define F first #define S second using namespace std; const int maxn = 1e5 + 7; typedef pair <int, int> pii; int n, q; int c[maxn], d[maxn], v[maxn], r[maxn], par[maxn][21], ps[maxn]; set <pii> s; vector <int> sus[maxn]; void pres (int x, int prf) { ps[x] = prf + c[x]; if (sus[x].size() == 0) { return; } for (auto it : sus[x]) { pres(it, prf + c[x]); } } int main () { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> d[i] >> c[i]; auto it = s.lower_bound({d[i], 0}); if (it == s.begin()) { s.insert({d[i], i}); continue; } do { --it; pii tr = *it; par[tr.S][0] = i; sus[i].push_back(tr.S); it = s.erase(it); } while (it != s.begin()); s.insert({d[i], i}); } auto it = s.begin(); while (!s.empty()) { pii tr = *it; sus[0].push_back(tr.S); it = s.erase(it); } for (int k = 1; k <= 20; k++) { for (int i = 1; i <= n; i++) { par[i][k] = par[par[i][k-1]][k-1]; } } pres(0, 0); /*for (int i = 0; i <= n; i++) { for (int k = 0; k < 4; k++) cout << par[i][k] << " \n"[k==3]; }*/ for (int i = 0; i < q; i++) { cin >> r[i] >> v[i]; int tr = r[i]; if (v[i] > ps[tr]) { cout << "0\n"; continue; } for (int k = 20; k >= 0; k--) { int cur = ps[tr] - ps[par[tr][k]]; //cout << "K: " << k << ", " << tr << " " << cur << " " << ps[tr] << " " << par[tr][k] << "\n"; if (v[i] > cur) { tr = par[tr][k]; v[i] -= cur; } } cout << tr << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...