답안 #918044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918044 2024-01-29T12:48:08 Z borisAngelov 금 캐기 (IZhO14_divide) C++17
48 / 100
1000 ms 10444 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 100005;
const long long inf = (1LL << 62);

int n;

struct Element
{
    int pos;
    int gold;
    int energy;
};

Element a[maxn];

long long prefEenrgy[maxn];
long long prefGold[maxn];

struct SegmentTree
{
    long long tree[4 * maxn];
    long long lazy[4 * maxn];

    void build(int node, int l, int r)
    {
        lazy[node] = 0;

        if (l == r)
        {
            tree[node] = prefEenrgy[l] - a[l].pos;
            return;
        }

        int mid = (l + r) / 2;

        build(2 * node, l, mid);
        build(2 * node + 1, mid + 1, r);

        tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    }

    void pushLazy(int node, int l, int r)
    {
        tree[node] += lazy[node];

        if (l != r)
        {
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }

        lazy[node] = 0;
    }

    void update(int node, int l, int r, int ql, int qr, long long delta)
    {
        pushLazy(node, l, r);

        if (l > qr || r < ql)
        {
            return;
        }

        if (ql <= l && r <= qr)
        {
            lazy[node] += delta;
            pushLazy(node, l, r);
            return;
        }

        int mid = (l + r) / 2;

        update(2 * node, l, mid, ql, qr, delta);
        update(2 * node + 1, mid + 1, r, ql, qr, delta);

        tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    }

    long long query(int node, int l ,int r, int ql, int qr)
    {
        pushLazy(node, l, r);

        if (l > qr || r < ql)
        {
            return -inf;
        }

        if (ql <= l && r <= qr)
        {
            return tree[node];
        }

        int mid = (l + r) / 2;

        return max(query(2 * node, l, mid, ql, qr), query(2 * node + 1, mid + 1, r, ql, qr));
    }

    void build()
    {
        build(1, 1, n);
    }

    void update(int l, int r, long long delta)
    {
        update(1, 1, n, l, r, delta);
    }

    long long query(int l, int r)
    {
        return query(1, 1, n, l, r);
    }
};

SegmentTree tree;

bool check(long long targetGold)
{
    tree.build();

    //cout << "checking for " << targetGold << endl;

    for (int i = 1; i <= n; ++i)
    {
        if (a[i].gold >= targetGold)
        {
            return true;
        }

        int l = i + 1;
        int r = n;

        while (l <= r)
        {
            int mid = (l + r) / 2;

            if (prefGold[mid] - prefGold[i - 1] >= targetGold)
            {
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }

        //cout << i << " :: " << ptr << " " << currentSum << endl;

        int pos = l;

        if (pos > n)
        {
            break;
        }

        if (tree.query(pos, n) >= 0)
        {
            return true;
        }

        if (i + 1 <= n)
        {
            tree.update(i + 1, n, (a[i + 1].pos - a[i].pos) - a[i].energy);
        }
    }

    return false;
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i].pos >> a[i].gold >> a[i].energy;
        prefEenrgy[i] = prefEenrgy[i - 1] + a[i].energy;
        prefGold[i] = prefGold[i - 1] + a[i].gold;
    }

    for (int i = 2; i <= n; ++i)
    {
        a[i].pos -= a[1].pos;
    }

    a[1].pos = 0;

    long long sum = 0;

    for (int i = 1; i <= n; ++i)
    {
        sum += a[i].gold;
    }

    long long l = 0;
    long long r = sum;

    while (l <= r)
    {
        long long mid = (l + r) / 2;

        if (check(mid) == true)
        {
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }

    cout << r << endl;

    return 0;
}

/*
4
0 5 1
1 7 2
4 4 1
7 15 1

4
1 5 1
2 7 2
5 4 1
8 15 1

2
2 4 1
5 5 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 5 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 4 ms 4444 KB Output is correct
22 Correct 10 ms 4444 KB Output is correct
23 Correct 34 ms 4700 KB Output is correct
24 Correct 34 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 5 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 4 ms 4444 KB Output is correct
22 Correct 10 ms 4444 KB Output is correct
23 Correct 34 ms 4700 KB Output is correct
24 Correct 34 ms 4696 KB Output is correct
25 Correct 2 ms 4700 KB Output is correct
26 Correct 3 ms 6748 KB Output is correct
27 Correct 4 ms 6852 KB Output is correct
28 Correct 16 ms 6744 KB Output is correct
29 Correct 21 ms 6748 KB Output is correct
30 Correct 40 ms 8320 KB Output is correct
31 Correct 27 ms 8284 KB Output is correct
32 Correct 29 ms 8276 KB Output is correct
33 Correct 30 ms 8480 KB Output is correct
34 Correct 905 ms 8360 KB Output is correct
35 Execution timed out 1077 ms 10444 KB Time limit exceeded
36 Halted 0 ms 0 KB -