QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#901342#2813. Weirdtreepies_0x#0 298ms56008kbC++144.7kb2025-02-15 21:06:582025-02-15 21:07:04

Judging History

This is the latest submission verdict.

  • [2025-02-15 21:07:04]
  • Judged
  • Verdict: 0
  • Time: 298ms
  • Memory: 56008kb
  • [2025-02-15 21:06:58]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 3e5 + 10;
const ll INF = 1e18;

struct QueryResult {
    ll max1;
    ll max2;
    int pos_max;
    ll sum;
};

struct Node {
    int l, r;
    ll max1, max2, sum;
    int cnt_max, pos_max;
    Node() : max1(-INF), max2(-INF), sum(0), cnt_max(0), pos_max(-1) {}
} tree[4 * MAXN];

ll h[MAXN];

void pushup(int id) {
    int lc = id << 1, rc = id << 1 | 1;
    tree[id].max1 = max(tree[lc].max1, tree[rc].max1);
    tree[id].sum = tree[lc].sum + tree[rc].sum;

    if (tree[lc].max1 > tree[rc].max1) {
        tree[id].pos_max = tree[lc].pos_max;
        tree[id].cnt_max = tree[lc].cnt_max;
    } else if (tree[rc].max1 > tree[lc].max1) {
        tree[id].pos_max = tree[rc].pos_max;
        tree[id].cnt_max = tree[rc].cnt_max;
    } else {
        tree[id].pos_max = min(tree[lc].pos_max, tree[rc].pos_max);
        tree[id].cnt_max = tree[lc].cnt_max + tree[rc].cnt_max;
    }

    vector<ll> candidates;
    if (tree[lc].max1 < tree[id].max1) candidates.push_back(tree[lc].max1);
    candidates.push_back(tree[lc].max2);
    if (tree[rc].max1 < tree[id].max1) candidates.push_back(tree[rc].max1);
    candidates.push_back(tree[rc].max2);

    ll max2 = -INF;
    for (ll c : candidates) {
        if (c > max2) max2 = c;
    }
    tree[id].max2 = max2;
}

void build(int id, int l, int r) {
    tree[id].l = l;
    tree[id].r = r;
    if (l == r) {
        tree[id].max1 = h[l];
        tree[id].max2 = -INF;
        tree[id].sum = h[l];
        tree[id].cnt_max = 1;
        tree[id].pos_max = l;
        return;
    }
    int mid = (l + r) >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    pushup(id);
}

void update(int id, int pos, ll val) {
    if (tree[id].l == tree[id].r) {
        tree[id].max1 = val;
        tree[id].sum = val;
        tree[id].max2 = -INF;
        tree[id].cnt_max = 1;
        tree[id].pos_max = pos;
        return;
    }
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (pos <= mid) update(id << 1, pos, val);
    else update(id << 1 | 1, pos, val);
    pushup(id);
}

QueryResult query(int id, int l, int r) {
    if (tree[id].l >= l && tree[id].r <= r) {
        return {tree[id].max1, tree[id].max2, tree[id].pos_max, tree[id].sum};
    }
    int mid = (tree[id].l + tree[id].r) >> 1;
    QueryResult res;
    res.max1 = -INF;
    res.max2 = -INF;
    res.sum = 0;
    res.pos_max = -1;

    if (l <= mid) {
        QueryResult left = query(id << 1, l, r);
        if (left.max1 > res.max1) {
            res.max1 = left.max1;
            res.pos_max = left.pos_max;
        } else if (left.max1 == res.max1) {
            res.pos_max = min(res.pos_max, left.pos_max);
        }
        res.sum += left.sum;

        vector<ll> candidates;
        if (left.max1 < res.max1) candidates.push_back(left.max1);
        candidates.push_back(left.max2);
        for (ll c : candidates) {
            if (c > res.max2) res.max2 = c;
        }
    }
    if (r > mid) {
        QueryResult right = query(id << 1 | 1, l, r);
        if (right.max1 > res.max1) {
            res.max1 = right.max1;
            res.pos_max = right.pos_max;
        } else if (right.max1 == res.max1) {
            if (res.pos_max == -1 || right.pos_max < res.pos_max) {
                res.pos_max = right.pos_max;
            }
        }
        res.sum += right.sum;

        vector<ll> candidates;
        if (right.max1 < res.max1) candidates.push_back(right.max1);
        candidates.push_back(right.max2);
        for (ll c : candidates) {
            if (c > res.max2) res.max2 = c;
        }
    }
    return res;
}

ll sum_query(int id, int l, int r) {
    if (tree[id].l >= l && tree[id].r <= r) {
        return tree[id].sum;
    }
    int mid = (tree[id].l + tree[id].r) >> 1;
    ll sum = 0;
    if (l <= mid) sum += sum_query(id << 1, l, r);
    if (r > mid) sum += sum_query(id << 1 | 1, l, r);
    return sum;
}

void initialise(int N, int Q, int h[]) {
    for (int i = 1; i <= N; ++i) {
        ::h[i] = h[i];
    }
    build(1, 1, N);
}

void cut(int l, int r, int k) {
    while (k > 0) {
        QueryResult qr = query(1, l, r);
        if (qr.max1 == 0) break;

        ll t;
        if (qr.max2 == -INF) {
            t = min((ll)k, qr.max1);
        } else {
            t = min((ll)k, qr.max1 - qr.max2);
        }

        int pos = qr.pos_max;
        ll new_val = qr.max1 - t;
        update(1, pos, new_val);
        k -= t;
    }
}

void magic(int i, int x) {
    update(1, i, x);
}

long long int inspect(int l, int r) {
    return sum_query(1, l, r);
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 54028kb

input:

966 1000
188363740 589476690 819684757 475179567 162289921 733331939 680003760 423847214 703730312 291752235 351463201 937522268 64588573 399012809 272561165 599780539 83270822 164102043 624995073 120374612 678210514 488108346 941579981 767236037 850406512 515467244 934426708 262361378 733612602 464...

output:

Unauthorized output

result:

wrong answer 1st lines differ - expected: '99386228771', found: 'Unauthorized output'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #5:

score: 0
Wrong Answer
time: 74ms
memory: 53336kb

input:

937 1000
631216009 869613152 930472391 565464603 615860285 225788550 621532305 671044759 686011029 102483970 507799388 976017264 586239272 91471532 773404833 981261100 664781538 691746892 973047425 562711051 792865846 686480962 800771605 626015452 783329411 478894142 826983440 279108379 994766235 23...

output:

Unauthorized output

result:

wrong answer 1st lines differ - expected: '95606780168', found: 'Unauthorized output'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 298ms
memory: 56008kb

input:

279629 300000
864485544 147664426 873456004 602824795 902744016 20056943 260905686 609162276 241739883 338354289 437560714 444081255 584613844 200551305 963158452 282143442 169245526 10832409 265203076 576549337 275863148 94296798 887754059 15388512 25015579 800125936 979301246 68177101 30414420 446...

output:

Unauthorized output

result:

wrong answer 1st lines differ - expected: '139687223836955', found: 'Unauthorized output'

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

0%