Submission #653242

# Submission time Handle Problem Language Result Execution time Memory
653242 2022-10-26T09:35:40 Z Blagoj Fountain (eJOI20_fountain) C++14
100 / 100
290 ms 20564 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define endl '\n';

int seg[300003];
int d[100003], c[100003];
pair<int, int> f[100003][22];

void build(int k, int l, int r)
{
    if (l == r)
    {
        seg[k] = d[l];
        return;
    }
    build(k * 2, l, (l + r) / 2);
    build(k * 2 + 1, (l + r) / 2 + 1, r);
    seg[k] = max(seg[k * 2], seg[k * 2 + 1]);
}

int ans = INT_MAX;

void search(int k, int l, int r, int i, int j, int v)
{
    if (l > j || r < i || seg[k] <= v)
    {
        return;
    }
    if (l == r && d[l] > v)
    {
        ans = min(ans, l);
        return;
    }
    if (i <= l && r <= j)
    {
        if (seg[k * 2] > v)
        {
            search(k * 2, l, (l + r) / 2, i, j, v);
        }
        else
        {
            if (seg[k * 2 + 1] > v)
            {
                search(k * 2 + 1, (l + r) / 2 + 1, r, i, j, v);
            }
        }
        return;
    }
    search(k * 2, l, (l + r) / 2, i, j, v);
    search(k * 2 + 1, (l + r) / 2 + 1, r, i, j, v);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> d[i] >> c[i];
    }

    build(1, 1, n);
    f[n][0].second = c[n];
    f[n][0].first = 0;

    for (int i = 1; i < n; i++)
    {
        ans = INT_MAX;
        search(1, 1, n, i + 1, n, d[i]);
        if (ans == INT_MAX)
        {
            ans = 0;
        }
        f[i][0].first = ans;
        f[i][0].second = c[i];
    }
    
    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j < 20; j++)
        {
            f[i][j].first = f[f[i][j - 1].first][j - 1].first;
            f[i][j].second = f[f[i][j - 1].first][j - 1].second + f[i][j - 1].second;
        }
    }

    while (q--)
    {
        int r, v;
        cin >> r >> v;
        for (int i = 19; i >= 0; i--)
        {
            if (v > f[r][i].second)
            {
                v -= f[r][i].second;
                r = f[r][i].first;
            }
        }
        cout << r << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 480 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 19204 KB Output is correct
2 Correct 230 ms 17868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 480 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 207 ms 19204 KB Output is correct
9 Correct 230 ms 17868 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 84 ms 11360 KB Output is correct
12 Correct 290 ms 20492 KB Output is correct
13 Correct 209 ms 20476 KB Output is correct
14 Correct 144 ms 20428 KB Output is correct
15 Correct 117 ms 20564 KB Output is correct