QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#284158#4616. 某位歌姬的故事_LAP_100 ✓12ms3724kbC++143.1kb2023-12-16 11:13:002023-12-16 11:13:01

Judging History

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

  • [2023-12-16 11:13:01]
  • 评测
  • 测评结果:100
  • 用时:12ms
  • 内存:3724kb
  • [2023-12-16 11:13:00]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1010, MOD = 998244353;
inline int Plus(int a, int b) {return a + b >= MOD ? a + b - MOD : a + b; }
inline int Minus(int a, int b) {return a - b < 0 ? a - b + MOD : a - b; }
inline int ksm(long long a, int b) {
	long long r = 1;
	for(; b; b >>= 1, a = a * a % MOD)
		if(b & 1) r = r * a % MOD;
	return r;
}
int n, q, a, L[N], R[N], M[N], up[N], siz[N];
vector<int> lsh;
inline int get(int x) {return lower_bound(lsh.begin(), lsh.end(), x) - lsh.begin() + 1; }

inline void init() {
	for(int i = 1; i < lsh.size(); i ++)
		siz[i] = lsh[i] - lsh[i - 1];
	vector<vector<int>> vec(lsh.size() + 1);
	for(int i = 1; i <= q; i ++) 
		vec[L[i]].emplace_back(M[i]), vec[R[i]].emplace_back(-M[i]);
	map<int, int> Map;
	for(int i = 1; i <= lsh.size(); i ++) {
		for(auto x : vec[i]) {
			if(x > 0) Map[x] ++;
			else if(!--Map[-x]) Map.erase(-x);
		}
		up[i] = (Map.empty() ? -1 : Map.begin()->first);
	}
}

int f[N];
inline int calc_ans(vector<pair<int, int>> &Q, vector<int> &P) {
	int UP = up[*P.begin()];
	for(auto &pr : Q) {
		if(*P.begin() > pr.second || *P.rbegin() < pr.first) return 0;
		pr.first = lower_bound(P.begin(), P.end(), pr.first) - P.begin();
		pr.second = --upper_bound(P.begin(), P.end(), pr.second) - P.begin();
		if(pr.first > pr.second) return 0;
	}
	vector<vector<int>> lim(P.size() + 1);
	for(auto pr : Q) lim[pr.second + 1].emplace_back(pr.first + 1);
	for(int i = 0; i <= P.size(); i ++)
		f[i] = 0;
	f[0] = 1; int bound = 0;
	for(int i = 1; i <= P.size(); i ++) {
		int c1 = ksm(UP, siz[P[i - 1]]), c2 = ksm(UP - 1, siz[P[i - 1]]); c1 = Minus(c1, c2);
		for(int j = bound; j < i; j ++)
			f[i] = Plus(f[i], 1ll * f[j] * c1 % MOD), f[j] = 1ll * f[j] * c2 % MOD;
		if(lim[i].empty()) continue;
		int Lim = lim[i][0];
		for(auto x : lim[i]) Lim = max(Lim, x);
		if(Lim > bound) {
			for(int j = bound; j < Lim; j ++) f[j] = 0;
			bound = Lim;
		}
	}
	int res = 0;
	for(int i = bound; i <= P.size(); i ++)
		res = Plus(res, f[i]);
	return res;
}

inline int calc_ans() {
	map<int, vector<pair<int, int>>> MapQ;
	map<int, vector<int>> MapV;
	for(int i = 1; i <= q; i ++)
		MapQ[M[i]].push_back({L[i], R[i] - 1});
	for(int i = 1; i < lsh.size(); i ++)
		if(up[i] != -1) MapV[up[i]].emplace_back(i);
	int res = 1;
	for(auto &pr : MapQ) {
		if(!MapV.count(pr.first)) return 0;
		res = 1ll * res * calc_ans(pr.second, MapV[pr.first]) % MOD;
	}
	return res;
}

inline void solve() {
	lsh.clear();
	cin >> n >> q >> a;
	for(int i = 1; i <= q; i ++) {
		cin >> L[i] >> R[i] >> M[i]; R[i] ++;
		lsh.emplace_back(L[i]), lsh.emplace_back(R[i]);
	}
	lsh.emplace_back(1), lsh.emplace_back(n + 1);
	sort(lsh.begin(), lsh.end()), lsh.erase(unique(lsh.begin(), lsh.end()), lsh.end());
	for(int i = 1; i <= q; i ++)
		L[i] = get(L[i]), R[i] = get(R[i]);
	init();

	int res = 1;
	for(int i = 1; i < lsh.size(); i ++)
		if(up[i] == -1) res = 1ll * res * ksm(a, siz[i]) % MOD;
	cout << 1ll * res * calc_ans() % MOD << '\n';
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	
	int T; cin >> T;
	while(T --) solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3612kb

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: 1ms
memory: 3516kb

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: 5ms
memory: 3620kb

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: 9ms
memory: 3604kb

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: 8ms
memory: 3664kb

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: 12ms
memory: 3724kb

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