QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#392562#6842. Popcount WordskgqyWA 63ms69240kbC++142.7kb2024-04-17 17:21:252024-04-17 17:21:25

Judging History

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

  • [2024-04-17 17:21:25]
  • 评测
  • 测评结果:WA
  • 用时:63ms
  • 内存:69240kb
  • [2024-04-17 17:21:25]
  • 提交

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){
	nl--;
	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 outputsum(){
	puts("QWQ");
	for(int i=29;i>=0;i--){
		int sum=0;
		for(int j=0;j<=tot;j++) sum+=g[j][i][0]+g[j][i][1];
		printf("%d ",sum);
	}
	puts("");
}
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 5
2 6
1 3
4 8
0
1
11
101
0011010

 
10 10 011010011
*/

詳細信息

Test #1:

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

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

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: -100
Wrong Answer
time: 63ms
memory: 69240kb

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:

273775
273458
99576
174198
174198
99260
16269
83307
91035
83163
83307
90890
83163
16097
3545
12724
42947
40360
43090
47945
70565
12598
12724
70583
48087
42803
40217
42945
12598
3499
473
3072
6370
6354
11642
31305
34226
6134
6432
36658
16561
31384
34104
36460
9538
3060
3072
9652
36577
34006
31447
166...

result:

wrong answer 1st lines differ - expected: '273828', found: '273775'