답안 #1097288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097288 2024-10-06T16:17:19 Z nguyen31hoang08minh2003 Fountain (eJOI20_fountain) C++17
100 / 100
151 ms 13440 KB
#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...)

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 11740 KB Output is correct
2 Correct 103 ms 11596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 92 ms 11740 KB Output is correct
9 Correct 103 ms 11596 KB Output is correct
10 Correct 2 ms 480 KB Output is correct
11 Correct 50 ms 6916 KB Output is correct
12 Correct 151 ms 13440 KB Output is correct
13 Correct 114 ms 12880 KB Output is correct
14 Correct 64 ms 12304 KB Output is correct
15 Correct 50 ms 12580 KB Output is correct