QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#138404#1140. Distributing Candiesbashkort30 631ms49288kbC++204.9kb2023-08-11 17:45:392023-08-11 17:47:25

Judging History

你现在查看的是最新测评结果

  • [2023-08-11 17:47:25]
  • 评测
  • 测评结果:30
  • 用时:631ms
  • 内存:49288kb
  • [2023-08-11 17:45:39]
  • 提交

answer

#include "candies.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll infLL = 3e18;

struct Info {
    pair<ll, int> mx{-infLL, 0}, mn{infLL, 0};
    ll mxans = -infLL, mnans = infLL;
    Info() = default;
    Info(int i, ll x) : mx(x, i), mn(x, i) {}
};

pair<ll, int> umin(pair<ll, int> a, pair<ll, int> b) {
    return a.first != b.first ? min(a, b) : a.second > b.second ? a : b;
}

Info operator+(const Info &l, const Info &r) {
    Info res{};
    res.mx = max(l.mx, r.mx);
    res.mn = umin(l.mn, r.mn);
    res.mxans = max({l.mxans, r.mxans, r.mx.first - l.mn.first});
    res.mnans = min({l.mnans, r.mnans, r.mn.first - l.mx.first});
    return res;
}

namespace SegmentTree {
    vector<Info> t;
    vector<ll> tag;
    int sz = 1;

    void apply(int x, ll tg) {
        tag[x] += tg;
        t[x].mx.first += tg, t[x].mn.first += tg;
    }

    void push(int x) {
        if (tag[x]) {
            apply(x << 1, tag[x]);
            apply(x << 1 | 1, tag[x]);
            tag[x] = 0;
        }
    }

    void pull(int x) {
        t[x] = t[x << 1] + t[x << 1 | 1];
    }

    void init(int n) {
        sz = 1 << __lg(n) + !!(n & (n - 1));
        t.assign(sz << 1, {}), tag.assign(sz << 1, 0);
        for (int i = 0; i < n; ++i) {
            t[i + sz] = Info(i, 0);
        }
        for (int i = sz - 1; i > 0; --i) {
            pull(i);
        }
    }

    void rangeAdd(int l, int r, int tg, int x = 1, int lx = 0, int rx = sz) {
        if (l >= rx || lx >= r) {
            return;
        }
        if (l <= lx && rx <= r) {
            return apply(x, tg);
        }
        int mid = lx + rx >> 1;
        push(x);
        rangeAdd(l, r, tg, x << 1, lx, mid);
        rangeAdd(l, r, tg, x << 1 | 1, mid, rx);
        pull(x);
    }

    ll query(int i, int x = 1, int lx = 0, int rx = sz) {
        while (lx + 1 < rx) {
            push(x);
            int mid = lx + rx >> 1;
            if (i < mid) {
                rx = mid;
                x = x << 1;
            } else {
                lx = mid;
                x = x << 1 | 1;
            }
        }
        return t[x].mx.first;
    }

    int findSegmentBiggerThanC(int C, ll mx = -3e18, int x = 1, int lx = 0, int rx = sz) {
        if (lx + 1 == rx) {
            return lx;
        }
        push(x);
        int mid = lx + rx >> 1;
        if (t[x << 1 | 1].mxans >= C || mx - t[x << 1 | 1].mn.first >= C) {
            return findSegmentBiggerThanC(C, mx, x << 1 | 1, mid, rx);
        } else {
            return findSegmentBiggerThanC(C, max(mx, t[x << 1 | 1].mx.first), x << 1, lx, mid);
        }
    }

    int findSegmentSmallerThanC(int C, ll mn = 3e18, int x = 1, int lx = 0, int rx = sz) {
        if (lx + 1 == rx) {
            return lx;
        }
        push(x);
        int mid = lx + rx >> 1;
        if (t[x << 1 | 1].mnans <= C || mn - t[x << 1 | 1].mx.first <= C) {
            return findSegmentSmallerThanC(C, mn, x << 1 | 1, mid, rx);
        } else {
            return findSegmentSmallerThanC(C, min(mn, t[x << 1 | 1].mn.first), x << 1, lx, mid);
        }
    }

    Info rangeQuery(int l, int r, int x = 1, int lx = 0, int rx = sz) {
        if (l >= rx || lx >= r) {
            return {};
        }
        if (l <= lx && rx <= r) {
            return t[x];
        }
        int mid = lx + rx >> 1;
        push(x);
        return rangeQuery(l, r, x << 1, lx, mid) + rangeQuery(l, r, x << 1 | 1, mid, rx);
    }
};

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> L,
                                    std::vector<int> R, std::vector<int> V) {
    int n = c.size(), q = size(L);

    std::vector<int> answers(n);
    vector<vector<int>> updates(n + 1);

    for (int i = 0; i < q; ++i) {
        updates[L[i]].push_back(i);
        updates[R[i] + 1].push_back(~i);
    }

    SegmentTree::init(q + 1);

    ll sumAll = 0;

    for (int i = 0; i < n; ++i) {
        for (int j : updates[i]) {
            if (j >= 0) {
                sumAll += V[j];
                SegmentTree::rangeAdd(j + 1, q + 1, V[j]);
            } else {
                j = ~j;
                sumAll -= V[j];
                SegmentTree::rangeAdd(j + 1, q + 1, -V[j]);
            }
        }
        int l1 = SegmentTree::t[1].mxans < c[i] ? q + 1 : SegmentTree::findSegmentBiggerThanC(c[i]);
        int r1 = SegmentTree::rangeQuery(l1 + 1, q + 1).mx.second;
        int l2 = SegmentTree::t[1].mnans > -c[i] ? q + 1 : SegmentTree::findSegmentSmallerThanC(-c[i]);
        int r2 = max(SegmentTree::rangeQuery(0, q + 1).mn.second, SegmentTree::rangeQuery(l2 + 1, q + 1).mn.second);
        if (r2 <= r1) {
            answers[i] = c[i] + sumAll - SegmentTree::query(r1);
        } else {
            answers[i] = sumAll - SegmentTree::query(r2);
        }
    }

    return answers;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 0ms
memory: 3744kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
8
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
8
0 7 1
0 7 1
0 7 300000000
0 7 994967293
0 7 1
0 7 1000000000
0 7 1000000000
0 7 1000000000

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
10
283 43634 101056 10340 6009 5133 30 2 3677888 210
10
1 8 26416
2 7 -51219
2 4 17793
3 7 75426
3 7 22307
1 1 60258
3 7 -29824
0 8 24839
2 8 -60304
0 1 -26411

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 17223 0 0 0 0 0 0 0 0

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 3ms
memory: 3868kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
10
5895610 429664 3124 17993 758457 101345 5102817 1127952 59 81146
2000
6 7 44356
5 7 77812
1 4 -41353
1 7 -81697
2 5 -26607
4 9 84461
4 7 -44947
1 6 42622
3 5 -99951
0 1 -77687
2 6 52280
5 9 5073
1 9 67601
6 8 -6669
0 6 42368
4 6 22221
1 3 48306
3 6 -23492
...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
1520966 0 0 0 20922 69708 15240 91107 0 78774

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 3768kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
100
1281 616650 26929 344 1231 263 183010 1 1 46 144770 17 1735 9520 7 1 39535 8307 2 5 32940 498570 644480 10107 1645 21 443708 4 28177 2857127 2 1 1350 17506 5 36 1985 42978 24123 73 114 230034 5561405 11263 9754875 4671 44 3 8982 299 452 15 2619 9 7 6259 8...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 19101 26929 344 1231 263 14968 1 1 46 40142 17 1735 9520 7 1 31323 8307 2 5 16804 87522 84360 5468 1645 21 93788 4 28177 158016 2 1 1350 17506 5 36 1985 42978 24123 73 114 230034 248401 11263 290026 4671 44 3 8982 299 452 15 2619 9 7 6259 68282 104034 17...

result:

ok 3 lines

Test #5:

score: 0
Accepted
time: 4ms
memory: 3872kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
2000
30 43135 3633 8815565 7 10656 4747283 11 1955823 368399 1933641 1354338 121930 8151786 1 2 8 3693091 433 39590 3210647 4211 49 288876 9195 37 129470 1370232 65473 2 10572 49 15282 452 8700630 9946 17 3 39005 1040244 167569 37 2 24552 115 7 117 350655 198...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
30 43135 3633 342743 7 10656 352396 11 498344 368399 425526 434636 121930 524601 1 2 8 427431 433 39590 398492 4211 49 288876 9195 37 129470 481647 65473 2 10572 49 15282 452 865424 9946 17 3 39005 791920 135995 37 2 24552 115 7 117 319081 1988 18 846056 7...

result:

ok 3 lines

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 561ms
memory: 49212kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
11408901 370732653 37843 28 53693 15782410 103 297546 1112427 170319071 26 1 6172 11614171 431 884673599 1 3 50085 154 57 28200 145886 898969 74758 72 845768 6 69787 11 31012465 55362 253 2363145 47186217 1103 19622 594 7867 1 4299 28130 48 4689582 12 ...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
87153 87153 37843 28 53693 406667 103 297546 766665 971468 26 1 6172 1257294 431 1536484 1 3 50085 154 57 28200 145886 898969 74758 72 845768 6 69787 11 3043755 55362 253 2363145 3459533 1103 19622 594 7867 1 4299 28130 48 4317336 12 431 123 4531465 4806 3...

result:

wrong answer 3rd lines differ - expected: '87153 87153 37843 28 53693 406...46468 9 1756 429030 247071 1629', found: '87153 87153 37843 28 53693 406...46468 9 1756 429030 247071 1629'

Subtask #3:

score: 27
Accepted

Test #9:

score: 27
Accepted
time: 1ms
memory: 3684kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
1000
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 3 lines

Test #10:

score: 0
Accepted
time: 310ms
memory: 39020kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
2000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
1049802 936230 884511 204101 406074 550834 961642 2076245 1975297 2101513 2254310 1990108 1398298 1917785 2864344 2475931 2270774 401698970 402327667 402506418 401068637 399388438 398687119 398672863 397137012 397262070 396255448 395961553 394643443 394646...

result:

ok 3 lines

Test #11:

score: 0
Accepted
time: 132ms
memory: 10656kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 3 lines

Test #12:

score: 0
Accepted
time: 622ms
memory: 49288kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
27608 64753 120270 150200 99177 99177 69754 163659 163659 234722 182586 182586 182586 115212 115212 36356 36356 6028 6028 972950338 973018128 973018128 973018128 973018128 973123904 973123904 973124098 973020904 973039972 973136885 973173147 973200808 9730...

result:

ok 3 lines

Test #13:

score: 0
Accepted
time: 631ms
memory: 49240kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 3907113 13718455 22058386 22058386 22058386 17250429 17250429 22661114 22661114 13718455 26434847 24657287 31478180 40855091 34489940 34489940 34489940 34489940 34489940 34489940 34489940 41385683 41385683 45763396 45763396 45763396 45763396 40987755 409...

result:

ok 3 lines

Test #14:

score: 0
Accepted
time: 582ms
memory: 49208kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 0 0 0 0 0 0 0 0 0 0 920673668 920673668 920673668 920673668 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 555951031 55595103...

result:

ok 3 lines

Test #15:

score: 0
Accepted
time: 526ms
memory: 49212kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000...

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 ...

result:

ok 3 lines

Subtask #4:

score: 0
Wrong Answer

Test #16:

score: 0
Wrong Answer
time: 1ms
memory: 3704kb

input:

lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg
10
11 440 51 41 11 1 3 108 148 14
10
0 9 60
0 9 -9
0 9 -30
0 9 41
0 9 82
0 9 69
0 9 -79
0 9 -39
0 9 72
0 9 41

output:

4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq
OK
11 648 51 41 11 1 3 108 143 14

result:

wrong answer 3rd lines differ - expected: '11 208 51 41 11 1 3 108 143 14', found: '11 648 51 41 11 1 3 108 143 14'

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%