QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566214#4616. 某位歌姬的故事Rikku_eq100 ✓63ms4000kbC++143.5kb2024-09-15 23:23:202024-09-15 23:23:23

Judging History

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

  • [2024-09-15 23:23:23]
  • 评测
  • 测评结果:100
  • 用时:63ms
  • 内存:4000kb
  • [2024-09-15 23:23:20]
  • 提交

answer

#include <bits/stdc++.h>
#define mxQ 504
#define mod 998244353
using namespace std;
typedef long long ll;

int T, n, Q, A;
struct Pnt {
    int l, r, m;
    bool operator< (const Pnt &x) const
    {
        if (m<x.m) { return true; }
        if (m>x.m) { return false; }
        if (l<x.l) { return true; }
        if (l>x.l) { return false; }
        return r>x.r;
    }
} p[mxQ], q1[mxQ], q2[mxQ];

int num_mx[mxQ], tot_mx, vec[mxQ*2], dp[mxQ*2], tg[mxQ*2], sum[mxQ*2];

ll pw (ll bs, ll t)
{
    ll res=1;
    while (t) {
        if (t&1) { res=res*bs%mod; }
        bs=bs*bs%mod; t>>=1;
    }
    return res;
}

int main ()
{
    // freopen("0test.in", "r", stdin);
    // freopen("0test.out", "w", stdout);

    scanf("%d", &T);
    while (T--) {
        scanf("%d %d %d", &n, &Q, &A);
        for (int i=1; i<=Q; i++) {
            scanf("%d %d %d", &p[i].l, &p[i].r, &p[i].m);
            num_mx[i]=p[i].m;
        }

        sort(num_mx+1, num_mx+Q+1);
        tot_mx=unique(num_mx+1, num_mx+Q+1)-(num_mx+1);

        sort(p+1, p+Q+1);

        int pos=1; ll ans=1;

        for (int t=1; t<=tot_mx; t++) {
            int tot1=0, tot2=0, totv=0;
            while (pos<=Q && p[pos].m==num_mx[t]) { q1[++tot1]=p[pos]; pos++; }

            int mnr=n+1;
            for (int i=tot1; i>=1; i--) {
                if (q1[i].r<mnr) { q2[++tot2]=q1[i]; mnr=q1[i].r; }
            }
            for (int i=1; i<=(tot2/2); i++) { swap(q2[i], q2[tot2-i+1]); }

            ll frac=(num_mx[t]-1)*pw(num_mx[t], mod-2) %mod;
            dp[0]=1;
            for (int i=1; i<=tot2; i++) {
                dp[i]=0;
                for (int j=0; j<i; j++) {
                    dp[i] += (-1)*dp[j]*pw(frac, q2[i].r-max(q2[j].r, q2[i].l-1)) %mod;
                    dp[i]%=mod;
                }
            }

            /*---------------------------------------*/

            for (int i=1; i<=tot1; i++) {
                vec[++totv]=q1[i].l;
                vec[++totv]=q1[i].r+1;
            }
            vec[++totv]=n+1;

            sort(vec+1, vec+totv+1);
            totv=unique(vec+1, vec+totv+1)-(vec+1);

            fill(tg, tg+totv+1, 0);
            for (int i=1; i<=tot1; i++) {
                int idl=lower_bound(vec+1, vec+totv+1, q1[i].l)-vec;
                tg[idl]++;
                int idr=lower_bound(vec+1, vec+totv+1, q1[i].r+1)-vec;
                tg[idr]--;
            }
            for (int i=1; i<=totv; i++) { tg[i]+=tg[i-1]; }

            fill(sum, sum+totv+1, 0);
            for (int i=1; i<=totv; i++) {
                sum[i]=sum[i-1]+(tg[i-1]!=0)*(vec[i]-vec[i-1]);
            }

            for (int i=pos; i<=Q; i++) {
                int id=upper_bound(vec+1, vec+totv+1, p[i].l)-vec;

                p[i].l=p[i].l-(sum[id]-(tg[id-1]!=0)*(vec[id]-1-p[i].l))+(tg[id-1]!=0);
                id=upper_bound(vec+1, vec+totv+1, p[i].r)-vec;
                p[i].r=p[i].r-(sum[id]-(tg[id-1]!=0)*(vec[id]-1-p[i].r));

                if (p[i].l>p[i].r) { ans=0; break; }
            }
            if (!ans) { break; }

            n-=sum[totv];

            /*---------------------------------------*/

            ll res=0;
            for (int i=0; i<=tot2; i++) { res+=dp[i]; res%=mod; }
            res*=pw(num_mx[t], sum[totv]); res%=mod;
            ans*=res; ans%=mod;
        }

        ans*=pw(A, n); ans%=mod;
        ans=(ans+mod)%mod;

        printf("%lld\n", ans);
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 1ms
memory: 3920kb

input:

20
6 4 1
5 5 1
5 5 1
5 5 1
3 5 1
7 5 7
5 7 7
4 6 7
2 4 5
3 5 5
1 3 5
7 3 6
3 5 4
3 4 2
1 3 5
6 1 3
3 4 1
4 7 4
2 2 1
2 3 3
1 3 3
1 4 4
1 3 3
2 4 4
2 3 3
6 2 1
4 6 1
5 6 1
7 6 7
3 4 7
4 5 7
1 2 7
5 6 3
2 3 3
6 7 7
7 4 7
1 7 5
1 7 5
1 7 5
1 7 5
7 7 3
1 7 3
3 3 1
3 5 2
1 7 3
1 7 3
5 7 3
4 6 2
4 7 4
2 3...

output:

1
6195
972
81
3
1
25
61741
54
16
7
400
41859
143
1183
12
12
1
12
64

result:

ok 20 numbers

Test #2:

score: 10
Accepted
time: 2ms
memory: 3948kb

input:

20
10 500 646098556
6 9 439257586
2 7 524941340
7 10 507301801
2 7 162445168
5 10 58861448
2 4 451481122
5 10 349055919
5 10 89612238
2 7 641048847
5 9 38567126
1 5 531484921
6 9 343313803
3 10 535728521
7 8 112419475
2 3 90864238
4 6 491653596
1 5 172462162
2 10 133836077
4 9 50604524
2 3 354655571...

output:

0
785753854
156467067
842608010
1
1
1
65080808
120479126
582985999
891688309
91244033
78042626
31118384
0
241916859
915781081
517074614
477100780
833883870

result:

ok 20 numbers

Test #3:

score: 8
Accepted
time: 0ms
memory: 3992kb

input:

20
500 10 521327020
117 310 159438587
146 205 433825396
51 410 138794979
151 462 137367357
99 421 120070335
293 403 271955116
67 246 365612156
56 404 420622033
12 448 158177194
5 43 184891672
500 10 529119698
95 500 528644956
227 430 528644956
208 495 528644956
383 490 525955333
17 304 526606735
92 ...

output:

0
230174842
900883843
981761584
480974848
152668605
926260367
663343443
470671250
42151157
324469203
824181502
679427405
84160574
827744043
191552214
38437915
564301127
575376836
908079152

result:

ok 20 numbers

Test #4:

score: 12
Accepted
time: 6ms
memory: 4000kb

input:

20
500 500 2
83 365 2
29 164 2
83 346 2
23 441 2
79 309 2
101 466 2
28 84 2
117 139 2
265 400 2
287 298 2
61 277 2
13 232 2
78 268 2
50 177 2
196 363 2
304 338 2
45 312 2
414 428 2
293 293 2
50 219 2
56 474 2
8 422 2
131 430 2
200 391 2
405 489 2
10 124 2
37 386 2
53 211 2
58 367 2
199 284 2
302 498...

output:

406764698
515151012
21558941
394822836
7212197
775320158
600133081
106289678
822807564
136800535
346785572
724614806
953563089
6047210
376692770
313369647
835457935
75129990
569690395
184696869

result:

ok 20 numbers

Test #5:

score: 18
Accepted
time: 63ms
memory: 3956kb

input:

20
612027408 394 2
232590695 574485803 2
89242319 434460742 2
57493231 520220209 2
504470622 578712531 2
242647449 321373282 2
132690606 169348786 2
118190243 400862798 2
372700144 511314042 2
95882021 286428925 2
379258861 538261075 2
48879630 396521752 2
295911071 449876518 2
137206644 316639382 2...

output:

887912832
526692745
250028819
190314505
51966514
505488497
371736074
643216595
332311397
74726821
596637585
408435985
328014774
630675513
148307420
562291033
921207148
991814385
258944268
71971454

result:

ok 20 numbers

Test #6:

score: 28
Accepted
time: 9ms
memory: 3904kb

input:

20
500 500 627462097
381 497 193820503
30 177 373307767
260 370 78016656
25 333 339693801
67 151 11299992
185 308 88517981
24 150 83570816
29 494 89389548
39 490 80542300
151 431 122858186
205 388 74084393
12 295 494963048
19 462 58976306
249 458 260365820
282 412 74179098
83 124 306839416
86 420 16...

output:

0
648613483
459041299
873360203
858833319
70918460
550191431
911911467
47251824
926717651
0
0
169237496
136122004
536618592
406525074
341178043
200489234
171734665
309277411

result:

ok 20 numbers

Test #7:

score: 19
Accepted
time: 29ms
memory: 3944kb

input:

20
900000000 500 900000000
177791949 475435391 135941333
335045482 707931893 286169628
223530173 538361922 421212969
3261445 679091117 298460202
69535464 399644653 27138730
443447575 586014111 66581660
134313405 618065813 681793222
220114523 850475758 44428195
225634512 316561096 521493890
299811441...

output:

0
622092866
675094097
424095313
44255675
170958328
802284221
983055967
426224570
987025914
888746941
497865105
959246255
718342353
18741982
454228400
227400091
547081787
187977900
915664108

result:

ok 20 numbers

Extra Test:

score: 0
Extra Test Passed