제출 #1236934

#제출 시각아이디문제언어결과실행 시간메모리
1236934Ekber_EkberFountain (eJOI20_fountain)C++20
0 / 100
416 ms34852 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]; vector <int> t(4*MAX+2); void build(int v, int tl, int tr, vector <int> &a) { if (tl == tr) { t[v] = a[tl]; return; } int tm = (tl + tr) / 2; build(v*2, tl, tm, a); build(v*2+1, tm+1, tr, a); t[v] = max(t[v*2], t[v*2+1]); } int find(int v, int tl, int tr, int l, int r) { if (l > r) { return 0; } if (tl == l && tr == r) { return t[v]; } int tm = (tl + tr) / 2; int r1 = find(v*2, tl, tm, l, min(r, tm)); int r2 = find(v*2+1, tm+1, tr, max(l, tm+1), r); return max(r1, r2); } 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) + n * log(n) * log(n)) build(1, 0, n, dia); for (int i=1; i <= n; i++) { int l = i+1, r = n, ans=0; while (l <= r) { int m = (l + r) / 2; if (find(1, 1, n, i, m) <= dia[i]) { l = m + 1; } else { ans = m; r = m - 1; } } g[i] = ans; } // correct // for (int i=1; i <= n; i++) cout << g[i] << ' '; // cout << endl; // 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-1; 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...