QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#665741#7901. Basic Substring Structureqzez#WA 19ms21816kbC++142.1kb2024-10-22 15:04:172024-10-22 15:04:19

Judging History

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

  • [2024-10-22 15:04:19]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:21816kb
  • [2024-10-22 15:04:17]
  • 提交

answer

#include<bits/stdc++.h>
#define LB lower_bound
#define fi first
#define se second
using namespace std;using ll=long long;
const int N=2e5+5,mod=998244353;
int n,A[N];
namespace SA{
	ll sum[N];
	const int bas=998244353;
	const ll mod=ll(1e18)+3;
	using LL=__int128;
	ll pw[N];
	void build(){
		for(int i=1;i<=n;i++) sum[i]=(LL(sum[i-1])*bas+A[i])%mod;
		for(int i=pw[0]=1;i<=n;i++) pw[i]=LL(pw[i-1])*bas%mod;
	}
	LL qryHa(int x,int y){
		return (sum[y]+LL(mod-sum[x-1])*pw[y-x+1])%mod;
	}
	int LCP(int x,int y){
		int l=0,r=min(n-x+1,n-y+1)+1,mid;
		while(l+1<r) mid=l+r>>1,(qryHa(x,x+mid-1)==qryHa(y,y+mid-1)?l:r)=mid;
		return l;
	}
}
struct bit{
	ll f[N];
	void clr(){fill(f+1,f+n+1,0ll);}
	void add(int x,ll y){while(x<=n) f[x]+=y,x+=x&-x;}
	ll qry(int x){ll ans=0;while(x) ans+=f[x],x-=x&-x;return ans;}
}f1,f2,f3;
int len[N];
ll sum[N];
map<int,ll> f[N];
void Solve(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&A[i]);
	SA::build();
	for(int i=1;i<=n;i++) sum[i]=n;
	for(int i=2;i<=n;i++) len[i]=SA::LCP(1,i);
	f1.clr();f2.clr();
	for(int i=n;i;i--){
		sum[i]+=f1.qry(i-1)+(f2.qry(n)-f2.qry(i-1))*(i-1);
		if(len[i]){
			f1.add(len[i],len[i]);
			f2.add(len[i],1);
		}
	}
	// for(int i=1;i<=n;i++) cerr<<sum[i]<<' ';cerr<<'\n';
	f1.clr();f2.clr();f3.clr();
	for(int i=2;i<=n;i++){
		if(len[i]){
			f1.add(i+len[i]-1,len[i]);f2.add(i+len[i]-1,i-1);f3.add(i+len[i]-1,1);
		}
		sum[i]+=f1.qry(i-1)+(i-1)*(f3.qry(n)-f3.qry(i-1))-(f2.qry(n)-f2.qry(i-1));
	}
	for(int i=1;i<=n;i++) f[i].clear();
	for(int i=2;i<=n;i++) if(i+len[i]<=n){
		int w=len[i]+1+SA::LCP(i+len[i]+1,len[i]+2);
		if(w>=i+len[i]-1){
			if(A[i+(i+len[i])-1]==A[len[i]+1]){
				w+=1+SA::LCP(i+(i+len[i]),i+len[i]+1);
			}else w=i+len[i]-1;
		}
		f[i+len[i]][A[len[i]+1]]+=w-len[i];
		if(len[i]+1<i){
			f[len[i]+1][A[i+len[i]]]+=1+SA::LCP(len[i]+2,i+len[i]+1);
		}
	}
	ll tot=0;
	for(int i=1;i<=n;i++){
		ll mx=0;
		for(auto [u,v]:f[i]) mx=max(mx,v);
		tot+=(sum[i]+mx^i);
		// cerr<<sum[i]+mx<<'\n';
	}
	printf("%lld\n",tot);
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 21776kb

input:

2
4
2 1 1 2
12
1 1 4 5 1 4 1 9 1 9 8 10

output:

15
217

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 19ms
memory: 21816kb

input:

10000
8
2 1 2 1 1 1 2 2
9
2 2 1 2 1 2 1 2 1
15
2 1 2 1 1 1 1 2 2 1 2 1 2 2 1
2
1 1
10
2 1 1 1 2 2 1 1 2 2
3
2 1 2
11
1 2 2 1 1 2 1 2 2 1 1
14
2 1 1 1 1 2 1 1 1 2 2 1 2 1
12
2 2 2 1 2 2 2 1 1 2 1 2
4
2 1 1 2
8
1 2 2 2 1 2 1 1
8
1 1 2 1 2 1 1 1
6
2 1 1 1 2 2
14
2 2 1 1 1 1 2 2 2 1 2 2 1 1
10
1 2 2 1 1...

output:

94
128
347
3
211
9
265
363
278
15
95
114
58
348
225
3
335
364
377
316
3
19
122
65
15
83
36
261
10
64
28
90
85
104
252
191
21
48
303
63
102
20
24
68
316
362
266
309
354
281
326
281
231
312
3
330
54
328
8
69
32
147
322
39
338
89
242
3
165
346
245
20
155
3
404
393
392
81
269
360
20
54
21
279
3
17
351
3...

result:

wrong answer 24th lines differ - expected: '66', found: '65'