QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#697962#6672. Colorful Segments0x3fffffffAC ✓419ms23196kbC++234.0kb2024-11-01 16:36:512024-11-01 16:36:51

Judging History

This is the latest submission verdict.

  • [2024-11-01 16:36:51]
  • Judged
  • Verdict: AC
  • Time: 419ms
  • Memory: 23196kb
  • [2024-11-01 16:36:51]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;

using LL = long long;

template <class Node>
struct Segment_Tree {
    vector<vector<Node>> t;
    void init(int n) {
        t.resize(2);
        t[0].resize(n << 2);
        t[1].resize(n << 2);
        // build(1, 1, n,p);
    }
    void build(int x, int l, int r, int p) {
        t[p][x].l = l, t[p][x].r = r;
        if (l == r) {
            if (l == 0) {
                t[p][x].sum = 1;
            }
            else
                t[p][x].sum = 0;
            t[p][x].lazy = 1;
            return;
        }
        int mid = (l + r) >> 1;
        build(x << 1, l, mid, p);
        build(x << 1 | 1, mid + 1, r, p);
        t[p][x] = t[p][x << 1] + t[p][x << 1 | 1];
    }
    void updata(int x, int p) {
        if (t[p][x].lazy == 1)return;
        t[p][x << 1].sum *= t[p][x].lazy;
        t[p][x << 1].sum %= mod;
        t[p][x << 1 | 1].sum *= t[p][x].lazy;
        t[p][x << 1 | 1].sum %= mod;
        t[p][x << 1].lazy *= t[p][x].lazy;
        t[p][x << 1].lazy %= mod;
        t[p][x << 1 | 1].lazy *= t[p][x].lazy;
        t[p][x << 1 | 1].lazy %= mod;
        t[p][x].lazy = 1;
    }

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

    void upd(int x, int v, int w, int p) {
        int lc = t[p][x].l, rc = t[p][x].r;
        if (lc == v && rc == v) {
            t[p][x].sum += w;
            t[p][x].sum %= mod;
            return;
        }
        // updata(x, p);
        int mid = (lc + rc) >> 1;
        if (v <= mid)upd(x << 1, v, w, p);
        else  upd(x << 1 | 1, v, w, p);
        push_up(x, p);
    }

    void change(int x, int l, int r, int w, int p) {
        int lc = t[p][x].l, rc = t[p][x].r;
        if (lc >= l && rc <= r) {
            t[p][x].sum *= w;
            t[p][x].sum %= mod;
            t[p][x].lazy *= w;
            t[p][x].lazy %= mod;
            return;
        }
        updata(x, p);
        int mid = (lc + rc) >> 1;
        if (l <= mid)change(x << 1, l, r, w, p);
        if (r > mid)change(x << 1 | 1, l, r, w, p);
        push_up(x, p);
    }

    Node sum(int x, int l, int r, int p) {
        int lc = t[p][x].l, rc = t[p][x].r;
        if (lc >= l && rc <= r) return t[p][x];
        updata(x, p);
        int mid = (lc + rc) >> 1;
        if (r <= mid) return sum(x << 1, l, r, p);
        if (l > mid) return sum(x << 1 | 1, l, r, p);
        return sum(x << 1, l, r, p) + sum(x << 1 | 1, l, r, p);
    }
};

struct Node {
    int l, r;
    LL lazy = 1, sum;
};
Node operator + (const Node& a, const Node& b) {
    Node c;
    c.l = a.l, c.r = b.r;
    c.lazy = 1;
    c.sum = a.sum + b.sum;
    c.sum %= mod;
    return c;
}

struct node {
    int l, r, c;
};
void solve() {
    int n;cin >> n;
    vector<node>a(n + 1);
    for (int i = 1;i <= n;i++) {
        cin >> a[i].l >> a[i].r >> a[i].c;
    }
    sort(a.begin() + 1, a.end(), [&](node a, node b) {
        return a.r < b.r;
    });
    a[0].l = a[0].r = 0;
    Segment_Tree<Node>tr;
    tr.init(n);
    tr.build(1, 0, n, 0);
    tr.build(1, 0, n, 1);
    // tr.change(1, 1, n - 1, 3, 0);
    // cout << tr.sum(1, 1, n, 0).sum << "\n";
    LL ans = 1;
    for (int i = 1;i <= n;i++) {
        int l = 0, r = i;
        while (l + 1 < r) {
            int mid = l + r >> 1;
            if (a[mid].r < a[i].l)l = mid;
            else r = mid;
        }
        // cout << l << "\n";
        LL res = tr.sum(1, 0, l, a[i].c ^ 1).sum;
        // cout << i << " " << res << "\n";
        tr.upd(1, i, res, a[i].c);
        tr.change(1, 0, l, 2, a[i].c ^ 1);
        ans += res;
        ans %= mod;
    }
    cout << ans << "\n";



}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    cin >> tt;
    while (tt--)
        solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3848kb

input:

2
3
1 5 0
3 6 1
4 7 0
3
1 5 0
7 9 1
3 6 0

output:

5
8

result:

ok 2 number(s): "5 8"

Test #2:

score: 0
Accepted
time: 203ms
memory: 5304kb

input:

11120
9
336470888 481199252 1
642802746 740396295 1
396628655 579721198 0
202647942 971207868 1
46761498 268792718 0
16843338 125908043 1
717268783 787375312 0
519096230 693319712 1
762263554 856168102 1
5
274667941 279198849 0
155191459 421436316 1
140515506 286802932 0
233465964 451394766 1
105650...

output:

107
11
192
24
102
20
14
96
2
8
104
10
9
10
12
8
26
12
268
10
8
4
92
3
2
7
7
34
76
6
16
22
36
8
24
68
46
82
7
92
5
40
8
9
2
2
44
52
68
2
12
64
23
28
16
74
11
4
2
70
2
240
64
40
10
4
2
3
112
2
24
40
38
50
52
4
4
53
46
48
10
4
56
268
22
8
48
42
12
40
12
96
44
8
200
7
8
2
6
35
17
258
44
84
85
10
3
28
2
...

result:

ok 11120 numbers

Test #3:

score: 0
Accepted
time: 419ms
memory: 23196kb

input:

5
100000
54748096 641009859 1
476927808 804928248 1
503158867 627937890 0
645468307 786026685 1
588586447 939887597 0
521365644 710156469 1
11308832 860350786 0
208427221 770562147 0
67590963 726478310 0
135993561 255361535 0
46718075 851555000 1
788412453 946936715 1
706350235 771067386 0
16233727 ...

output:

357530194
516871115
432496137
637312504
617390759

result:

ok 5 number(s): "357530194 516871115 432496137 637312504 617390759"

Test #4:

score: 0
Accepted
time: 352ms
memory: 23152kb

input:

5
100000
848726907 848750009 0
297604744 297778695 0
838103430 838114282 0
39072414 39078626 0
600112362 600483555 0
792680646 792687230 0
580164077 580183653 0
921627432 921685087 0
308067290 308143197 0
111431618 111465177 0
626175211 626270895 0
593284132 593292158 0
497427809 497437386 0
3493551...

output:

538261388
538261388
538261388
538261388
538261388

result:

ok 5 number(s): "538261388 538261388 538261388 538261388 538261388"

Test #5:

score: 0
Accepted
time: 287ms
memory: 23104kb

input:

5
100000
49947 49948 1
44822 44823 0
91204 91205 0
46672 46673 1
18538 18539 1
25486 25487 1
68564 68565 1
63450 63451 1
4849 4850 1
85647 85648 1
6019 6020 0
81496 81497 0
62448 62449 1
50039 50040 1
67911 67912 1
64304 64305 0
68727 68728 0
22340 22341 0
27265 27266 1
74123 74124 0
92270 92271 0
2...

output:

688810885
178370005
333760229
155298895
925722609

result:

ok 5 number(s): "688810885 178370005 333760229 155298895 925722609"