QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140533#6672. Colorful Segmentstraining4usacoAC ✓428ms18336kbC++173.0kb2023-08-16 03:26:402023-08-16 03:26:43

Judging History

This is the latest submission verdict.

  • [2023-08-16 03:26:43]
  • Judged
  • Verdict: AC
  • Time: 428ms
  • Memory: 18336kb
  • [2023-08-16 03:26:40]
  • Submitted

answer

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

#define int long long
const int MAXN = 1e5 + 5;
const int MOD = 998244353;

struct Segment {
	int l, r, c;

    bool operator<(const Segment &other) const {
        return r < other.r;
    }
};

int n;
int t[4 * MAXN][2];
int lazy[4 * MAXN][2];
Segment segments[MAXN];

inline void apply(int u, int tag, int type) {
    t[u][type] *= tag; t[u][type] %= MOD;
    lazy[u][type] *= tag; lazy[u][type] %= MOD;
}

inline void pushdown(int u, int l, int r, int type) {
    if(lazy[u][type] == 1) return;

    assert(l != r);

    apply(2 * u, lazy[u][type], type);
    apply(2 * u + 1, lazy[u][type], type);

    lazy[u][type] = 1;
}

void upd(int u, int l, int r, int pos, int v, int type) {
    if(pos < l || pos > r) return;

    if(l == r) {
        t[u][type] += v; t[u][type] %= MOD;
        return;
    }

    pushdown(u, l, r, type);
    int mid = (l + r) / 2;
    upd(2 * u, l, mid, pos, v, type); upd(2 * u + 1, mid + 1, r, pos, v, type);
    t[u][type] = t[2 * u][type] + t[2 * u + 1][type]; t[u][type] %= MOD;
}

void upd2(int u, int l, int r, int pos, int type) {
    if(pos < l) return;
    if(r <= pos) {
        apply(u, 2, type);
        return;
    }

    pushdown(u, l, r, type);
    int mid = (l + r) / 2;
    upd2(2 * u, l, mid, pos, type); upd2(2 * u + 1, mid + 1, r, pos, type);
    t[u][type] = t[2 * u][type] + t[2 * u + 1][type]; t[u][type] %= MOD;
}

int qry(int u, int l, int r, int pos, int type) {
    if(pos < l) return 0;
    if(r <= pos) return t[u][type];

    pushdown(u, l, r, type);
    int mid = (l + r) / 2;
    return (qry(2 * u, l, mid, pos, type) + qry(2 * u + 1, mid + 1, r, pos, type)) % MOD;
}


void solve() {
    cin >> n;

    for(int i = 0; i <= 4 * n; ++i) t[i][0] = t[i][1] = 0, lazy[i][0] = lazy[i][1] = 1;

    for(int i = 1; i <= n; ++i) {
        cin >> segments[i].l >> segments[i].r >> segments[i].c;
    }
    segments[0].l = segments[0].r = 0;

    sort(segments + 1, segments + 1 + n);
    upd(1, 0, n, 0, 1, 0); upd(1, 0, n, 0, 1, 1);

    int ans = 1;
    for(int i = 1; i <= n; ++i) {
        int lo = -1, hi = i;
        while(hi - lo > 1) {
            int mid = (lo + hi) / 2;
            if(segments[mid].r < segments[i].l) lo = mid;
            else hi = mid;
        }

        // cout << "LO: " << lo << endl;
        int type = segments[i].c;
        int val = qry(1, 0, n, lo, 1 - type);
        // cout << "VAL: " << val << endl;
        upd(1, 0, n, i, val, type); upd2(1, 0, n, lo, 1 - type);
        ans += val; ans %= MOD;

        // cout << "trees: " << endl;
        // for(int i = 1; i <= n; ++i) {
        //     cout << qry(1, 0, n, i, 0) << " ";
        // }
        // cout << endl;
        // for(int i = 1; i <= n; ++i) {
        //     cout << qry(1, 0, n, i, 1) << " ";
        // }
        // cout << endl;
    }
    cout << ans << "\n";
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    int t; cin >> t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7580kb

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: 176ms
memory: 9700kb

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: 428ms
memory: 18336kb

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: 329ms
memory: 18324kb

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: 283ms
memory: 18328kb

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"