Submission #1097288

#TimeUsernameProblemLanguageResultExecution timeMemory
1097288nguyen31hoang08minh2003Fountain (eJOI20_fountain)C++17
100 / 100
151 ms13440 KiB
#include <set> #include <map> #include <cmath> #include <queue> #include <deque> #include <stack> #include <math.h> #include <bitset> #include <vector> #include <string> #include <cstdio> #include <cctype> #include <numeric> #include <fstream> #include <sstream> #include <cstdlib> #include <iomanip> #include <cassert> #include <cstring> #include <stdio.h> #include <string.h> #include <iterator> #include <iostream> #include <algorithm> #include <strings.h> #include <functional> #include <unordered_set> #include <unordered_map> #define fore(i, a, b) for (int i = (a), i##_last = (b); i < i##_last; ++i) #define fort(i, a, b) for (int i = (a), i##_last = (b); i <= i##_last; ++i) #define ford(i, a, b) for (int i = (a), i##_last = (b); i >= i##_last; --i) #define fi first #define se second #define pb push_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using namespace std; using ll = long long; using ld = long double; template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;}; template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;}; typedef unsigned long long ull; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; typedef vector<vii> vvii; constexpr int MAX_N = 1E5 + 5, INF = 0X3F3F3F3F, LOG2_MAX_N = 20; signed result, h[MAX_N], ancestor[LOG2_MAX_N][MAX_N], floorLog2[MAX_N], sum[MAX_N]; int N, Q, R, V, D[MAX_N], C[MAX_N]; stack<int> s; int findAncestor(const int j, const int i) { if (i > N) return i; return ancestor[j][i]; } int findSum(const int x, const int y) { return sum[x] - sum[y] + C[y]; } signed main() { #ifdef LOCAL freopen("input.INP", "r", stdin); // freopen("output.OUT", "w", stdout); #endif // LOCAL cin.tie(0) -> sync_with_stdio(0); cout.tie(0); cin >> N >> Q; D[N + 1] = INF; fort(i, 1, N) cin >> D[i] >> C[i]; s.push(N + 1); ford(i, N, 1) { for (; !s.empty() && D[s.top()] <= D[i]; s.pop()); ancestor[0][i] = s.top(); h[i] = h[ancestor[0][i]] + 1; sum[i] = sum[ancestor[0][i]] + C[i]; s.push(i); } for (int j = 1; (1 << j) <= N; ++j) fort(i, 1, N - (1 << j) + 1) ancestor[j][i] = findAncestor(j - 1, findAncestor(j - 1, i)); fort(i, 2, N) floorLog2[i] = floorLog2[i >> 1] + 1; fort(_, 1, Q) { cin >> R >> V; if (V > sum[R]) { cout << 0 << '\n'; continue; } if (V <= C[R]) { cout << R << '\n'; continue; } result = R; ford(j, floorLog2[h[R]], 0) while (result != ancestor[j][result] && findSum(R, ancestor[j][result]) < V) result = ancestor[j][result]; result = ancestor[0][result]; cout << result << '\n'; } return 0; } /* Use binary-lifting (with prefix sum...) */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...