QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#392637#6842. Popcount WordskgqyML 65ms70224kbC++142.6kb2024-04-17 18:30:182024-04-17 18:30:19

Judging History

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

  • [2024-04-17 18:30:19]
  • 评测
  • 测评结果:ML
  • 用时:65ms
  • 内存:70224kb
  • [2024-04-17 18:30:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
	int w=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) w=w*10+ch-'0',ch=getchar();
	return w;
}
int que[500005];
int ql=1,qr;
int tr[500005][2],tot=0;
int f[500005][35][2],g[500005][35][2];
int id[500005];
int sum[500005],fail[500005];
char ch[500005];
vector<int> to[500005];
int insert(){
	int n=strlen(ch+1);
	int nw=0;
	for(int i=1;i<=n;i++){
		int way=ch[i]-'0';
		if(!tr[nw][way]) tr[nw][way]=++tot;
		nw=tr[nw][way];
	}
	return nw;
}
void build(){
	if(tr[0][0]) que[++qr]=tr[0][0];
	if(tr[0][1]) que[++qr]=tr[0][1];
	while(ql<=qr){
		int u=que[ql++];
		for(int k=0;k<2;k++){
			if(tr[u][k]){
				fail[tr[u][k]]=tr[fail[u]][k];
				que[++qr]=tr[u][k];
			}else tr[u][k]=tr[fail[u]][k];
		}
	}
	for(int i=0;i<=tot;i++) f[i][0][0]=tr[i][0],f[i][0][1]=tr[i][1];
	for(int k=1;k<30;k++){
		for(int i=0;i<=tot;i++){
			f[i][k][0]=f[f[i][k-1][0]][k-1][1];
			f[i][k][1]=f[f[i][k-1][1]][k-1][0];
		}
	}
	for(int i=2;i<=tot;i++) to[fail[i]].push_back(i);
}
int n,q;
int sl[100005],sr[100005];
int nw=0;
void solve(int nl,int nr){
	nr++;
	for(int i=0;i<30;i++){
		if((nl&(1<<i))&&nl+(1<<i)<=nr){
			int p=__builtin_popcount(nl);
			g[nw][i][p&1]++;
			// printf("%d %d %d\n",p&1,nw,i);
			nw=f[nw][i][p&1];
			nl+=(1<<i);
		}
	}
	for(int i=29;i>=0;i--){
		if((nr&(1<<i))&&!(nl&(1<<i))){
			int p=__builtin_popcount(nl);
			// printf("%d %d %d\n",p&1,nw,i);
			g[nw][i][p&1]++;
			nw=f[nw][i][p&1];
			nl+=(1<<i);
		}
	}
}
void pushtag(){
	for(int k=29;k;k--){
		for(int i=0;i<=tot;i++){
			g[i][k-1][0]+=g[i][k][0];
			g[f[i][k-1][0]][k-1][1]+=g[i][k][0];
			g[i][k-1][1]+=g[i][k][1];
			g[f[i][k-1][1]][k-1][0]+=g[i][k][1];
		}
	}
	for(int i=0;i<=tot;i++){
		for(int k=0;k<2;k++){
			sum[tr[i][k]]+=g[i][0][k];
		}
	}
}
void bfs(){
	for(int i=qr;i;i--){
		int u=que[i];
		sum[fail[u]]+=sum[u];
	}
}
main(){
	scanf("%lld%lld",&n,&q);
	for(int i=1;i<=n;i++) scanf("%lld%lld",&sl[i],&sr[i]);
	for(int i=1;i<=q;i++){
		scanf("%s",ch+1);
		id[i]=insert();
	}
	build();
	for(int i=1;i<=n;i++) solve(sl[i],sr[i]);
	pushtag();
	// for(int i=0;i<=tot;i++){
	// 	printf("%d %d %d %d",i,tr[i][0],tr[i][1],fail[i]);
	// 	printf("\n%d\n",sum[i]);       
	// }
	bfs();
	// for(int i=0;i<=tot;i++){
	// 	printf("%d %d %d %d",i,tr[i][0],tr[i][1],fail[i]);
	// 	printf("\n%d\n",sum[i]);       
	// }
	for(int i=1;i<=q;i++) printf("%lld\n",sum[id[i]]);
}
/*
3 7
2 6
1 3
4 8
0
00
1
11
101
0011010
1010011010011

1 1
2 3
10

1010011010011
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 24432kb

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: 0ms
memory: 24736kb

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: 65ms
memory: 70224kb

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: -100
Memory Limit Exceeded

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: