QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#701437#9528. New Energy Vehiclefoolnine#ML 3ms15900kbC++203.2kb2024-11-02 14:12:242024-11-02 14:12:31

Judging History

This is the latest submission verdict.

  • [2024-11-02 14:12:31]
  • Judged
  • Verdict: ML
  • Time: 3ms
  • Memory: 15900kb
  • [2024-11-02 14:12:24]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define ls (rt << 1)
#define rs (rt << 1 | 1)

using i64 = long long;

struct Segment {
    vector<i64> f, tag;

    void init(int l, int r, int rt, int a, int b, i64 c) {
        if (l >= a && r <= b) {
            f[rt] = c;
            return;
        }

        int mid = (l + r) >> 1;
        if (mid >= a) {
            init(l, mid, ls, a, b, c);
        }
        if (mid + 1 <= b) {
            init(mid + 1, r, rs, a, b, c);
        }
        push_up(rt);
    }

    void push_up(int rt) {
        f[rt] = f[ls] + f[rs];
    }

    void push_down(int rt) {
        if (tag[rt] != 0) {
            i64 t = tag[rt];
            tag[rt] = 0;
            int t1 = min(f[ls], t), t2 = t - t1;
            f[ls] -= t1, tag[ls] += t1;
            f[rs] -= t2, tag[rs] += t2;
        }
    }

    void update(int l, int r, int rt, int a, int b, i64 &c) {
        if (l >= a && r <= b) {
            i64 t = min(f[rt], c);
            f[rt] -= t;
            c -= t;
            tag[rt] += t;
            return;
        }

        push_down(rt);
        int mid = (l + r) >> 1;
        if (mid >= a) {
            update(l, mid, ls, a, b, c);
        }
        if (mid + 1 <= b) {
            update(mid + 1, r, rs, a, b, c);
        }
        push_up(rt);
    }

    i64 query(int l, int r, int rt, int a, int b) {
        if (l >= a && r <= b) {
            return f[rt];
        }

        push_down(rt);
        int mid = (l + r) >> 1;
        i64 ret = 0;
        if (mid >= a) {
            ret += query(l, mid, ls, a, b);
        }
        if (mid + 1 <= b) {
            ret += query(mid + 1, r, rs, a, b);
        }
        return ret;
    }

    void In(int n) {
        f.assign(n << 2, 0);
        tag.assign(n << 2, 0);
    }

    void Re(int n) {
        for (int i = 1; i < (n << 2); i++) {
            f[i] = tag[i] = 0;
        }
    }
};

Segment f;

void solve() {
    int n, m;
    cin >> n >> m;

    i64 b = 0;
    vector<i64> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b += a[i];
    }

    vector<int> x(m + 1, 0), t(m + 1);
    for (int i = 1; i <= m; i++) {
        cin >> x[i] >> t[i];
        t[i]--;
        f.init(1, n, 1, i, i, x[i] - x[i - 1]);
    }

    i64 ans = 0;

    vector<int> last(n, 0);

    for (int i = 1; i <= m; i++) {
        i64 cost = x[i] - x[i - 1];
        if (b < cost) {
            ans = x[i - 1] + b;
            break;
        } else {
            i64 cal = f.query(1, n, 1, last[t[i]] + 1, i), can = min(a[t[i]], i64(x[i] - x[last[t[i]]]));
            if (cal >= can) {
                b = b - cost + can;
                f.update(1, n, 1, last[t[i]] + 1, i, can);
            } else {
                b = b - cost + cal;
                f.update(1, n, 1, last[t[i]] + 1, i, cal);
            }

            last[t[i]] = i;

            if (i == m) {
                ans = x[i] + b;
            }
        }
    }

    cout << ans << endl;

    f.Re(n);
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    f.In(2e5);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 15624kb

input:

2
3 1
3 3 3
8 1
2 2
5 2
1 2
2 1

output:

12
9

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 15900kb

input:

6
3 2
2 2 2
6 1
7 1
2 2
3 3
2 1
6 2
2 3
2 2
5 1
7 2
9 1
2 2
3 3
2 1
6 2
1 1
999999999
1000000000 1
1 1
1000000000
1000000000 1

output:

9
11
4
11
999999999
2000000000

result:

ok 6 lines

Test #3:

score: -100
Memory Limit Exceeded

input:

10
230 8042
599 1039 69 1011 1366 824 14117 1523 806 5002 332 55 3769 996 359 1040 255 1135 3454 3609 6358 2509 3695 8785 3890 1304 3394 14611 33 89 2245 508 22 1043 10411 628 1279 714 903 585 7413 5099 845 148 4689 2110 8683 1613 143 3263 2599 110 244 3297 4742 1571 425 1822 15692 572 9397 328 1691...

output:


result: