QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#832172#6842. Popcount WordsKangliyangML 585ms510896kbC++142.8kb2024-12-25 19:23:522024-12-25 19:23:54

Judging History

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

  • [2024-12-25 19:23:54]
  • 评测
  • 测评结果:ML
  • 用时:585ms
  • 内存:510896kb
  • [2024-12-25 19:23:52]
  • 提交

answer

#include <bits/stdc++.h>
#define in(x) freopen(#x".in","r",stdin)
#define out(x) freopen(#x".out","w",stdout)
#define make(x) freopen(#x".in","w",stdout)
#define ll long long
#define int long long
#ifdef MY_COMPUTER
#define debug(...) fprintf(stderr,##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
#define vector basic_string
#define pii array<int,2>

using namespace std;

inline int read()
{
	int s=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*f;
}
template<typename _it>
void read(_it a,_it b){
	while(a!=b) (*a)=read(),a++;
}
template<typename _it>
void write(const char* form,_it a,_it b,int op=0){
	while(a!=b) printf(form,*a),a++;
	if(op) puts("");
}
int n,t,l[500010],r[500010];
char s[500010];
int tr[500010][2],fail[500010],siz[500010],cnt,ed[500010];
queue<int>q;
int f[500010][31][2],g[500010][31][2];
const int M=1ll<<31;
vector<int>e[500010];
void dfs(int u){for(int v:e[u]) dfs(v),siz[u]+=siz[v];}
signed main()
{
	//ios::sync_with_stdio(false);
	n=read(),t=read();
	for(int i=1;i<=n;i++) l[i]=read(),r[i]=read();
	for(int i=0,xl;i<t;i++){
		scanf("%s",s),xl=strlen(s);
		int x=0;
		for(int j=0,p;j<xl;j++){
			p=s[j]-'0';
			if(!tr[x][p]) tr[x][p]=++cnt;
			x=tr[x][p];
		}
		ed[i]=x;
	}
	if(tr[0][0]) q.push(tr[0][0]);
	if(tr[0][1]) q.push(tr[0][1]);
	while(!q.empty()){
		int u=q.front();q.pop();
		e[fail[u]]+=u;
		for(int x:{0,1})
			if(tr[u][x]) fail[tr[u][x]]=tr[fail[u]][x],q.push(tr[u][x]);
			else tr[u][x]=tr[fail[u]][x];
	}
	for(int i=0;i<=cnt;i++)
		f[i][0][0]=tr[i][0],f[i][0][1]=tr[i][1];
	for(int i=1;i<=30;i++)
		for(int u=0;u<=cnt;u++)
			f[u][i][0]=f[f[u][i-1][0]][i-1][1],
			f[u][i][1]=f[f[u][i-1][1]][i-1][0];
	for(int i=1,L,R,x,s=0;i<=n;i++){
		vector<pii>sp1,sp2;
		sp1.clear(),sp2.clear();
		L=l[i],R=r[i];
		for(L+=M-1,R+=M+1,x=0;L^R^1ll;L>>=1,R>>=1,x++){
			if(~L&1) sp1+={{x,__builtin_popcountll(l[i])&1}},l[i]+=1ll<<x;
			if(R&1) sp2+={{x,__builtin_popcountll(r[i]-(1<<x)+1)&1}},r[i]-=1ll<<x;
		}
		reverse(sp2.begin(),sp2.end());
		// for(auto v:sp1) printf("%lld %lld\n",v[0],v[1]);
		// for(auto v:sp2) printf("%lld %lld\n",v[0],v[1]);
		// puts("");
		for(auto v:sp1)
			g[s][v[0]][v[1]]++,s=f[s][v[0]][v[1]];//,printf("add %lld\n",s);
		for(auto v:sp2)
			g[s][v[0]][v[1]]++,s=f[s][v[0]][v[1]];//,printf("add %lld\n",s);
		// puts("");
	}
	for(int i=30;i>=1;i--)
		for(int u=0;u<=cnt;u++){
			g[u][i-1][0]+=g[u][i][0],g[f[u][i-1][0]][i-1][1]+=g[u][i][0];
			g[u][i-1][1]+=g[u][i][1],g[f[u][i-1][1]][i-1][0]+=g[u][i][1];
		}
	for(int i=0;i<=cnt;i++)
		siz[tr[i][0]]+=g[i][0][0],siz[tr[i][1]]+=g[i][0][1];
	dfs(0);
	for(int i=0;i<t;i++) printf("%lld\n",siz[ed[i]]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 28664kb

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: 6ms
memory: 30092kb

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: 51ms
memory: 68556kb

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: 585ms
memory: 510896kb

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

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: