QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#139050#4429. Gebyte's Grind8BQube#AC ✓6526ms624644kbC++204.4kb2023-08-12 17:04:412023-08-12 17:04:44

Judging History

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

  • [2023-08-12 17:04:44]
  • 评测
  • 测评结果:AC
  • 用时:6526ms
  • 内存:624644kb
  • [2023-08-12 17:04:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define pb push_back
#define ALL(v) v.begin(), v.end()

const ll INF = 1e18;

struct Tag {
    ll k, b, c;
    Tag(ll _k = 0, ll _b = 0, ll _c = 0): k(_k), b(_b), c(_c) {}
    Tag operator+(const Tag &t) const {
        Tag res(*this);
        if (t.k != 0)
            res.k = t.k, res.b = res.c = 0;
        res.b += t.b, res.c += t.b;
        res.c = max(res.c, t.c);
        return res;
    }
};

ll operator+(const ll &a, const Tag &t) {
    ll res = a;
    if (t.k != 0) res = t.k;
    res += t.b, res = max(res, t.c);
    return res;
}

struct Node {
    int key, pri;
    ll oval, min_val;
    Tag tag;
    Node *l, *r;
    Node(int k = 0, ll h = 0): key(k), pri(rand()), oval(h), min_val(h), tag(), l(0), r(0) {}
    void up() {
        min_val = oval;
        if (l) min_val = min(min_val, l->min_val);
        if (r) min_val = min(min_val, r->min_val);
    }
    void give_tag(Tag t) {
        oval = oval + t;
        min_val = min_val + t;
        tag = tag + t;
    }
    void down() {
        if (l) l->give_tag(tag);
        if (r) r->give_tag(tag);
        tag = Tag();
    }
};

Node *merge(Node *a, Node *b) {
    if (!a || !b) return a ? a : b;
    if (a->pri < b->pri)
        return a->down(), a->r = merge(a->r, b), a->up(), a;
    return b->down(), b->l = merge(a, b->l), b->up(), b;
}

void split(Node *o, Node *&a, Node *&b, int k) {
    if (!o) return a = b = 0, void();
    o->down();
    if (o->key <= k) 
        a = o, split(o->r, a->r, b, k), a->up();
    else
        b = o, split(o->l, a, b->l, k), b->up();
}

void insert(Node *&o, Node *p) {
    Node *a, *b;
    split(o, a, b, p->key);
    o = merge(a, merge(p, b));
}

void print(Node *&o) {
    if (!o) return;
    o->down();
    print(o->l);
    //cerr << o->key << ": " << o->oval << "\n";
    print(o->r);
}

vector<vector<pair<Tag, int>>> tags(2000005);
vector<vector<Node*>> qrys(2000005);
int ans[4000005], org[4000005], output[4000005];
Node node[4000005];

void del(Node *&o, ll gap, int cur) {
    if (!o || o->min_val > gap) return;
    o->down();
    del(o->l, gap, cur);
    del(o->r, gap, cur);
    if (o->oval <= gap) {
        ans[o->key] = cur - 1;
        o = merge(o->l, o->r);
    }
    else
        o->up();
}

void solve() {
    int n, q;
    ll h;
    cin >> n >> q >> h;
    fill_n(output + 1, q, 0);
    fill_n(ans + 1, q, n);
    for (int i = 1; i <= n; ++i) {
        tags[i].clear(), qrys[i].clear();
        char c;
        ll x;
        cin >> c >> x;
        if (c == 'B')
            tags[i].pb(make_pair(Tag(0, -x, 0), 0));
        else if (c == 'K')
            tags[i].pb(make_pair(Tag(x, 0, 0), 0));
        else if (c == 'C')
            tags[i].pb(make_pair(Tag(0, 0, x), 0));
        else
            assert(0);
    }
    for (int i = 1; i <= q; ++i) {
        char c;
        int pos;
        cin >> c >> pos;
        if (c == 'Z') {
            ll x;
            cin >> c >> x;
            if (c == 'B')
                tags[pos].pb(make_pair(Tag(0, -x, 0), i));
            else if (c == 'K')
                tags[pos].pb(make_pair(Tag(x, 0, 0), i));
            else if (c == 'C')
                tags[pos].pb(make_pair(Tag(0, 0, x), i));
            else
                assert(0);
        }
        else if (c == 'D') {
            org[i] = pos;
            node[i] = Node(i, h);
            qrys[pos].pb(node + i);
            output[i] = 1;
        }
        else
            assert(0);
    }
    Node *root = 0;
    for (int i = 1; i <= n; ++i) {
        for (auto p : qrys[i])
            insert(root, p);
        //cerr << "before " << i << "\n";
        //print(root);
        reverse(ALL(tags[i]));
        Node *res = 0;
        for (auto [tag, t] : tags[i]) {
            Node *r;
            split(root, root, r, t);
            if (tag.k != 0) del(r, tag.k - 1, i);
            else if (tag.b != 0) del(r, -tag.b, i);
            if (r) r->give_tag(tag);
            res = merge(r, res);
        }
        root = res;
        /*cerr << "after " << i << "\n";
        print(root);*/
    }
    for (int i = 1; i <= q; ++i)
        if (output[i]) {
            if (ans[i] == org[i] - 1) cout << "-1\n";
            else cout << ans[i] << "\n";
        }
}

int main() {
    srand(time(NULL));
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4752ms
memory: 594932kb

input:

1
2000000 4000000 1000000000000
B 2982992025
B 1226542907
B 2299259203
B 1223702056
B 1407121251
B 340783317
B 1259091208
B 2101980047
B 2611543443
B 2010658889
B 4233714406
B 3112120167
B 2311876255
B 2789960428
B 3008572010
B 1
B 2464951378
B 1364240867
B 2829584762
B 2511437438
B 692948679
B 1113...

output:

833302
238155
1007466
1912727
1483707
996123
913464
595444
1956432
168794
1224759
214012
1606540
541216
578117
1279590
1652446
647870
900696
1010723
1723225
1909185
765245
1770241
994028
135066
146309
423001
379625
188229
561102
1020950
25262
1007466
624537
1150303
892424
856521
175916
1187336
16896...

result:

ok 2911608 lines

Test #2:

score: 0
Accepted
time: 2845ms
memory: 624644kb

input:

1
2000000 4000000 900000000000
B 18921988072
B 1
B 162148155907
C 1000000000000
B 331992393119
K 428836058413
B 440712983778
B 534362851268
B 923013640108
B 472973869469
B 21294011490
B 1
B 1000000000000
B 76485869786
C 1000000000000
C 333159763195
B 1
B 277856669933
B 1
C 445619227450
B 86360111824...

output:

1625020
1618321
511712
1446036
1302385
244605
534722
1807821
1673978
-1
-1
1286986
650766
1419851
-1
-1
510520
-1
1996906
814567
719623
1737532
180266
285086
-1
1455059
11092
1030131
1479157
372584
399911
1897918
1585202
566085
1522965
63081
1721860
673731
1530763
-1
-1
134340
1467445
-1
1410807
-1
...

result:

ok 1999947 lines

Test #3:

score: 0
Accepted
time: 6526ms
memory: 622372kb

input:

1
2000000 4000000 1000000000000
B 17342013
B 14555766
B 26630469
B 66103720
B 62383790
B 17433493
B 66256092
B 74496332
B 66470344
B 17971159
B 46755192
B 33871545
B 47843886
B 17737257
B 91180218
B 2450298
B 31650513
B 2066148
B 82311128
B 56600828
B 12144701
B 83637831
B 73789298
B 108092
B 684688...

output:

6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
6137
...

result:

ok 2002501 lines

Test #4:

score: 0
Accepted
time: 522ms
memory: 346736kb

input:

100000
30 36 694566294336
B 399381235378
K 116128223667
B 571309322680
B 3999476334
C 694813305215
B 242568539922
K 275534906854
B 627467159442
C 603373692516
B 736482925501
B 857566416940
B 192161825500
B 709599212240
B 402172637373
B 400573894654
B 256573224769
B 294629292373
B 267978037726
B 7412...

output:

-1
16
21
-1
-1
23
9
7
-1
2
6
-1
24
22
-1
14
-1
26
-1
5
-1
-1
-1
14
-1
-1
6
-1
14
5
6
-1
-1
-1
-1
2
-1
7
2
-1
-1
7
11
11
11
11
11
11
11
11
11
11
11
2
7
7
11
2
-1
2
11
4
8
6
10
4
15
15
15
-1
15
8
15
-1
-1
-1
15
7
15
-1
-1
15
7
15
-1
15
7
6
6
9
-1
12
6
6
12
6
9
7
7
8
3
-1
-1
-1
5
-1
-1
13
20
11
22
-1
-...

result:

ok 927823 lines

Extra Test:

score: 0
Extra Test Passed