QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#353242#6842. Popcount WordsieeML 585ms506304kbC++232.2kb2024-03-13 23:45:072024-03-13 23:45:07

Judging History

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

  • [2024-03-13 23:45:07]
  • 评测
  • 测评结果:ML
  • 用时:585ms
  • 内存:506304kb
  • [2024-03-13 23:45:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 5e5 + 5;
int q, trie[N][2], tot = 1, fail[N], f[N][30][2], g[N][30][2], occ[N];
vector<int> vec[N], son[N];
void insert(string s, int id) {
	int now = 1;
	for (char c : s) {
		int &nxt = trie[now][c - '0'];
		if (!nxt) nxt = ++tot;
		now = nxt;
	}
	vec[now].push_back(id);
}
void build() {
	queue<int> Q;
	for (int i : {0, 1}) {
		if (trie[1][i]) {
			fail[trie[1][i]] = 1;
			Q.push(trie[1][i]);
		} else {
			trie[1][i] = 1;
		}
	}
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (int i : {0, 1}) {
			if (trie[u][i]) {
				fail[trie[u][i]] = trie[fail[u]][i];
				Q.push(trie[u][i]);
			} else {
				trie[u][i] = trie[fail[u]][i];
			}
		}
	}
	for (int i = 2; i <= tot; i++) {
		son[fail[i]].push_back(i);
	}
}
signed main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n >> q;
	vector<array<int, 2>> seg;
	for (int i = 0, l, r; i < n; i++) {
		cin >> l >> r;
		auto check = [&](int k) {
			if (l + (1 << k) - 1 > r || l % (1 << k)) return;
			seg.push_back({k, __builtin_parity(l)});
			l += (1 << k);
		};
		for (int k = 0; k < 30; k++) {
			if (l >> k & 1) {
				check(k);
			}
		}
		for (int k = 29; k >= 0; k--) {
			check(k);
		}
	}
	for (int i = 0; i < q; i++) {
		string s;
		cin >> s;
		insert(s, i);
	}
	build();
	for (int i = 1; i <= tot; i++) {
		for (int j : {0, 1}) {
			f[i][0][j] = trie[i][j];
		}
	}
	for (int k = 1; k < 30; k++) {
		for (int i = 1; i <= tot; i++) {
			for (int j : {0, 1}) {
				f[i][k][j] = f[f[i][k - 1][j]][k - 1][!j];
			}
		}
	}
	int now = 1;
	for (auto [k, v] : seg) {
		g[now][k][v]++;
		now = f[now][k][v];
	}
	for (int k = 28; k >= 0; k--) {
		for (int i = 1; i <= tot; i++) {
			for (int j : {0, 1}) {
				g[i][k][j] += g[i][k + 1][j];
				g[f[i][k][j]][k][!j] += g[i][k + 1][j];
			}
		}
	}
	for (int i = 1; i <= tot; i++) {
		occ[trie[i][0]] += g[i][0][0];
		occ[trie[i][1]] += g[i][0][1];
	}
	vector<int> ans(q);
	function<void(int)> dfs = [&](int u) {
		for (int v : son[u]) {
			dfs(v);
			occ[u] += occ[v];
		}
		for (int i : vec[u]) {
			ans[i] = occ[u];
		}
	};
	dfs(1);
	for (int x : ans) {
		cout << x << "\n";
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 11712kb

input:

3 5
2 6
1 3
4 8
0
1
11
101
0011010

output:

6
7
2
2
1

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 11824kb

input:

3 10
2 6
1 3
4 8
0
1
11
101
0011010
0
1
11
101
0011010

output:

6
7
2
2
1
6
7
2
2
1

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 56ms
memory: 54068kb

input:

100000 37701
605224571 605224571
681034454 681034463
468837041 468837048
323235128 323235135
400367345 400367345
394938241 394938241
221026001 221026007
183872918 183872926
881878131 881878138
374491962 374491967
588030380 588030381
109143398 109143401
727016146 727016149
857189138 857189138
1940312...

output:

273828
273405
99633
174195
174195
99209
16229
83404
91124
83071
83404
90791
83070
16138
3449
12780
43045
40359
43221
47903
70380
12690
12780
70624
48079
42712
40183
42887
12690
3448
413
3036
6576
6204
11678
31367
34167
6191
6484
36737
16633
31270
33957
36423
9697
2993
3036
9744
36469
34155
31543
165...

result:

ok 37701 lines

Test #4:

score: 0
Accepted
time: 577ms
memory: 496816kb

input:

100000 2064
155864032 155864033
351106162 351106162
63569309 63569310
446198383 446198387
844050143 844050148
28666643 28666652
948049121 948049128
422938918 422938925
590576664 590576664
118827333 118827339
248813547 248813549
222041903 222041911
481862624 481862626
39190429 39190429
373420004 3734...

output:

274777
274799
99973
174803
174803
99996
16241
83732
91053
83750
83732
91070
83750
16246
3457
12784
43222
40510
43091
47961
70993
12757
12784
70948
47831
43239
40641
43109
12757
3489
459
2998
6565
6219
11607
31614
34345
6165
6467
36624
16421
31540
34510
36483
9693
3064
2998
9786
36657
34291
31484
163...

result:

ok 2064 lines

Test #5:

score: 0
Accepted
time: 544ms
memory: 504488kb

input:

100000 264
863548123 863548131
726674730 726674731
23567654 23567655
388640944 388640951
11253180 11253185
364951459 364951461
954258329 954258336
370336558 370336567
783663445 783663448
948622704 948622711
241717161 241717163
167305985 167305985
559579744 559579746
686340873 686340882
110381066 110...

output:

275122
274500
100192
174929
174929
99571
16458
83734
91338
83591
83734
91194
83591
15980
3543
12915
43156
40578
43255
48082
70962
12629
12915
70819
48181
43013
40479
43112
12629
3351
482
3061
6476
6439
11559
31596
34378
6200
6644
36611
16564
31518
34274
36688
9720
2909
3061
9854
36680
34139
31696
16...

result:

ok 264 lines

Test #6:

score: 0
Accepted
time: 577ms
memory: 506304kb

input:

100000 82
440580187 440580195
363746917 363746923
253327641 253327648
77285448 77285454
16654500 16654506
701949354 701949357
335798407 335798407
510898124 510898124
728200505 728200507
657089802 657089806
811878897 811878897
740279233 740279234
2002051 2002056
201584556 201584557
905281625 90528163...

output:

274946
275169
99944
175001
175001
100168
16163
83781
91017
83984
83781
91219
83984
16184
3463
12700
42944
40837
42987
48029
71306
12678
12700
71081
48072
43147
40794
43190
12678
3506
483
2980
6395
6305
11456
31487
34601
6236
6440
36547
16623
31406
34670
36636
9614
3064
2980
9720
36549
34532
31531
16...

result:

ok 82 lines

Test #7:

score: 0
Accepted
time: 585ms
memory: 502940kb

input:

100000 65
990903704 990903704
488837026 488837029
520610697 520610700
84740156 84740158
145465213 145465219
802621365 802621370
637129208 637129216
138040766 138040770
789708694 789708702
122215439 122215439
805389806 805389815
726116821 726116826
90711329 90711332
980579500 980579500
877100759 8771...

output:

275313
275152
100356
174957
174957
100194
16549
83807
90996
83960
83807
91150
83961
16233
3570
12979
43006
40801
43164
47832
71205
12755
12979
70828
47990
43159
40643
43318
12755
3478
456
3114
6506
6473
11758
31248
34498
6303
6609
36555
16430
31401
34399
36806
9714
3041
3114
9865
36500
34328
31406
1...

result:

ok 65 lines

Test #8:

score: 0
Accepted
time: 549ms
memory: 496972kb

input:

100000 15
622051406 622051410
809727709 809727711
305371768 305371773
184626015 184626015
485560137 485560143
683120615 683120618
494143101 494143106
820622384 820622384
428188273 428188280
192875194 192875198
420425386 420425394
692993018 692993020
892081956 892081958
149770059 149770067
971481390 ...

output:

274897
275773
99624
175272
175273
100500
16106
83518
91087
84185
83518
91754
84185
16315
1

result:

ok 15 lines

Test #9:

score: 0
Accepted
time: 83ms
memory: 96484kb

input:

100000 37701
22481700 108517447
365920329 760528107
176789774 293610871
626946693 749768996
176851510 945414284
28086800 69412398
975208777 978832901
602518142 777697473
926108743 981817845
102894249 424879855
807095920 982351889
387442755 443766483
240971703 255490115
877218778 889992331
212556206 ...

output:

12456513055026
12456513055194
4152171031482
8304342023544
8304342023543
4152171031650
16684
4152171014798
4152171008685
4152171014858
4152171014797
4152171008746
4152171014858
16792
2837
13847
2076085501601
2076085513196
2076085501674
2076085507011
4152171000901
13957
13847
4152171000950
20760855070...

result:

ok 37701 lines

Test #10:

score: 0
Accepted
time: 147ms
memory: 169584kb

input:

100000 2054
813088861 920847292
829798259 830042915
622260050 902589939
569332566 921851528
972439729 974981697
635612829 663768316
943300178 959626806
230028555 866755106
611037825 984956272
692908117 957931507
729299439 899435491
80797300 566831546
243465192 697136951
725914708 807308097
708248233...

output:

12554055607628
12554055608034
4184685215101
8369370392526
8369370392527
4184685215507
16502
4184685198598
4184685193836
4184685198690
4184685198599
4184685193928
4184685198690
16817
2672
13830
2092342594137
2092342604461
2092342594041
2092342599795
4184685184641
14049
13830
4184685184768
20923425996...

result:

ok 2054 lines

Test #11:

score: -100
Memory Limit Exceeded

input:

100000 263
6495900 814924814
644264642 810226616
759397666 929669679
226052948 465198786
249046056 917890552
661740585 837290607
22998153 309770047
517914688 817207504
674138911 997925375
477982599 826121049
290788401 535952288
408265984 890129151
737268128 980648065
544692460 914764596
999867912 99...

output:

12483708370790
12483708370964
4161236136548
8322472234241
8322472234242
4161236136722
16648
4161236119900
4161236114095
4161236120146
4161236119900
4161236114341
4161236120146
16576
2785
13863
2080618054177
2080618065723
2080618054212
2080618059883
4161236106337
13809
13863
4161236106037
20806180599...

result: