Submission #1236844

#TimeUsernameProblemLanguageResultExecution timeMemory
1236844Ekber_EkberFountain (eJOI20_fountain)C++20
30 / 100
408 ms37660 KiB
#include <bits/stdc++.h> #define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define int long long #define itn int #define endl "\n" #define ff first #define ss second #define pb push_back #define ppb pop_back #define ins insert #define lb lower_bound #define ub upper_bound #define bs binary_search #define count1 __builtin_popcount #define all(v) v.begin(), v.end() using namespace std; struct point{ }; int ctoi(char x) { return (int)x - '0'; } int sumab(int a, int b) { return (a + b) * (b - a + 1) / 2; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a%b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } void print(vector <int> &v) { for (const int &i : v) cout << i << ' '; } constexpr int MAX = 1e+5 + 3, INF = 2e+9, MOD = 16714589, K = 18; int temp, temp1, temp2, temp3; vector <int> g; pair<int, int> up[K+3][MAX]; void _() { int n, q; cin >> n >> q; vector <int> v(n+1), dia(n+1); for (int i=1; i <= n; i++) { cin >> dia[i] >> v[i]; } v[0] = dia[0] = INF; g.assign(n+1, 0); // build tree // O(n * log(n)) multiset <pair<int, int>> ms; ms.insert({dia[0], 0}); for (int i = n; i > 0; i--) { auto it = ms.lb({dia[i]+1, 0}); g[i] = (*it).ss; ms.insert({dia[i], i}); } // correct // build up // O(n + n * log(n)) for (int i=1; i <= n; i++) { up[0][i] = {g[i], v[i]}; } up[0][0] = {0, INF}; for (int i=1; i <= K; i++) { for (int j=1; j <= n; j++) { if (up[i-1][j].ff == 0) { up[i][j] = {0, INF}; } else { if (up[i-1][up[i-1][j].ff].ff == 0) { up[i][j] = {0, INF}; } else { up[i][j].ff = up[i-1][up[i-1][j].ff].ff; up[i][j].ss = up[i-1][j].ss + up[i-1][up[i-1][j].ff].ss; } } } } // correct // queries // O(q * log(n)) while (q--) { int x, y; cin >> x >> y; int l=0, r=n, ans = -1; while (l <= r) { int mid = (l + r) / 2; int a = x, b = y; bool c = 1; for (int i = K-1; i >= 0; i--) { if (mid & (1 << i)) { if (b <= up[i][a].ss) { c = 0; break; } b -= up[i][a].ss; a = up[i][a].ff; } } // cout << b << ' ' << mid << endl; if (c) { ans = a; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } } signed main() { GOOD_LUCK int tests=1; // cin >> tests; while (tests--) { _(); // cout << endl; } return 0; } // Problem X // by Ekber_Ekber /* 8 6 3 5 4 3 2 2 1 3 3 8 5 10 4 5 7 30 4 10 1 3 7 35 6 38 5 8 2 50 8 1 3 5 4 3 2 2 1 3 3 8 5 10 4 5 7 30 7 35 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...