QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#184436#7088. Square GraphyllcmTL 56ms96264kbC++144.6kb2023-09-20 19:14:132023-09-20 19:14:13

Judging History

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

  • [2023-09-20 19:14:13]
  • 评测
  • 测评结果:TL
  • 用时:56ms
  • 内存:96264kb
  • [2023-09-20 19:14:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

namespace FastIO {
	#define iL (1 << 20)
	char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL;
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++)
	template<typename T>
	inline void read(T &a) {
		char ch;int sign = 0;
		for(ch = gc();!isdigit(ch);ch = gc())
			if(ch == '-') sign = 1;
		a = ch & 15;
		for(ch = gc();isdigit(ch);ch = gc())
			a = (a << 3) + (a << 1) + (ch & 15);
		if(sign) a = -a;
	}
	char Out[iL],*iter = Out;
	#define flush() fwrite(Out,1,iter - Out,stdout),iter = Out
	template<typename T>
	inline void write(T x,char end = '\n') {
		int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x;
		do c[++l] = x % 10,x /= 10; while(x);
		while(l) *iter++ = c[l--] + '0';
		*iter++ = end;flush();
	}
	#undef iL
	#undef gc
	#undef flush
}
using namespace FastIO;
const int N = 3e5 + 5,Lg = 20;
typedef long long ll;
int n;
int a[N],b[N],w[N];
int lg[N];

struct SA {
	SA(){}
	int v1[N],v2[N];
	int sa[N],ton[N];
	inline bool cmp(int *r,int i,int j,int k) { 
        return r[i] == r[j] && (i + k > n ? 0 : r[i + k]) == (j + k > n ? 0 : r[j + k]);
    }
	inline void Getsa(int *s,int *sa,int n,int m) {
		#define For(i,a,b) for(int i = (a);i <= (b);i++)
		#define Rof(i,a,b) for(int i = (a);i >= (b);i--)
		For(i,1,n) sa[i] = rk[i] = v1[i] = v2[i] = 0;
		int *x = v1,*y = v2,*t;
		For(i,0,m) ton[i] = 0;
		For(i,1,n) ton[x[i] = s[i]]++;
		For(i,0,m) ton[i] += ton[i - 1];
		Rof(i,n,1) sa[ton[x[i]]--] = i;
		for(int j = 1,p;j <= n;j *= 2,m = p) {
			p = 0;
			For(i,n - j + 1,n) y[++p] = i;
			For(i,1,n) if(sa[i] > j) y[++p] = sa[i] - j;
			For(i,0,m) ton[i] = 0;
			For(i,1,n) ton[x[y[i]]]++;
			For(i,1,m) ton[i] += ton[i - 1];
			Rof(i,n,1) sa[ton[x[y[i]]]--] = y[i],y[i] = 0;
			t = x;x = y;y = t;p = 1;x[sa[1]] = 1;
			For(i,2,n) 
				x[sa[i]] = cmp(y,sa[i - 1],sa[i],j) ? p : (++p);
			if(p >= n) break;
		}
		#undef For
		#undef Rof
	}
	int rk[N],height[N];
	inline void GetHeight(int *s,int n) {
		s[n + 1] = 0;
		for(int i = 1;i <= n;i++) rk[sa[i]] = i;
		for(int i = 1,j = 0;i <= n;i++) {
			if(j) --j;
			if(rk[i] != 1) 
				while(i + j <= n && sa[rk[i] - 1] + j <= n && s[i + j] == s[sa[rk[i] - 1] + j]) ++j;
			height[rk[i]] = j;
		}
	}
	int ST[Lg][N];
	inline int LCP(int l,int r) {
		if(l < 1 || r < 1 || l > n || r > n) return 0;
		l = rk[l];r = rk[r];
		if(l > r) swap(l,r);
		int res = 1e9;
		for(int i = l + 1;i <= r;i++) res = min(res,height[i]);
		return res;
	}
	inline void init(int *a,int n) {
		for(int i = 1;i <= n;i++) rk[i] = sa[i] = height[i] = 0;
		Getsa(a,sa,n,n);
		GetHeight(a,n);
		for(int i = 1;i <= n;i++) for(int j = 0;j < Lg;j++)  ST[j][i] = 0; 

		for(int i = 1;i <= n;i++) ST[0][i] = height[i];
		for(int j = 1;j < Lg;j++)
			for(int i = 1;i + (1 << j) - 1 <= n;i++)
				ST[j][i] = min(ST[j - 1][i],ST[j - 1][i + (1 << j - 1)]);
	}
};
SA A,B;

struct BCJ {
	int fa[N];
	inline void init(int n) { for(int i = 1;i <= n;i++) fa[i] = i;}
	int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]);}
	inline void Merge(int x,int y) { fa[find(x)] = find(y);}
	inline int Same(int x,int y) { return find(x) == find(y);}
}S[Lg],S0;

long long ans;
inline void Union(int x,int y,int dep) { // [x,x + 2^{dep}) , [y,y + 2^{dep})
	if(S[dep].Same(x,y)) return;
	if(dep == 0) {
		if(!S0.Same(x,y)) ans += w[y - x],S0.Merge(x,y);
		return;
	}
	S[dep].Merge(x,y);
	Union(x,y,dep - 1);
	if(y + (1 << dep - 1) <= n) Union(x + (1 << dep - 1),y + (1 << dep - 1),dep - 1);
}
int srt[N];
inline void Work() {
	read(n);
	for(int i = 1;i <= n;i++) read(a[i]),b[n - i + 1] = a[i];
	for(int i = 1;i <= n / 2;i++) read(w[i]),srt[i] = i;
	A.init(a,n);B.init(b,n);
	sort(srt + 1,srt + n / 2 + 1,[&](const int &x,const int &y) { return w[x] < w[y];});
	S0.init(n);
	for(int i = 0;i < Lg;i++) S[i].init(n);
	lg[0] = -1;
	for(int i = 1;i <= n;i++) lg[i] = lg[i >> 1] + 1;
	ans = 0;
	for(int _ = 1;_ <= n / 2;_++) {
		int len = srt[_];
		for(int i = len;i + len <= n;i += len) {
			int l = i,r = i + len;
			int L = n - (r - 1) + 1,R = n - (l - 1) + 1;
			int lcp = A.LCP(l,r);lcp = min(lcp,len);
			int lcs = B.LCP(L,R);lcs = min(lcs,len - 1);
			if(lcp + lcs >= len) {
				int tlen = lcp + lcs - len + 1;
				int cl = i - lcs,cr = cl + tlen - 1;
				cr = cr + len - 1;
				assert(cr <= n);
				int k = lg[cr - cl + 1];
				Union(cl,cl + len,k);
				Union(cr - (1 << k) + 1,cr - (1 << k) + 1 + len,k);
			}
		}
	}
	write(ans);
}
int main() {
	int T;read(T);
	while(T--) Work();
	return 0;
}
/*
1
9
1 2 2 1 2 2 2 2 2
4 1 3 4
*/

详细

Test #1:

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

input:

1
8
2 2 5 6 2 5 6 2
5 1 4 4

output:

21

result:

ok "21"

Test #2:

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

input:

3000
41
1 1 2 1 1 2 1 1 2 2 2 1 1 1 2 2 1 2 1 1 2 1 2 2 1 1 1 1 1 2 2 1 1 1 1 2 2 2 1 1 2
37 81 33 27 88 64 13 12 39 94 90 76 5 94 6 10 87 91 7 48
43
2 2 2 2 1 2 2 1 1 1 2 1 1 2 1 1 2 2 1 1 1 1 1 2 2 2 1 2 2 2 1 2 1 1 2 2 1 1 2 1 2 1 1
55 29 4 81 8 90 1 95 94 70 5 3 64 67 70 60 39 59 39 29 99
47
1 2...

output:

1169
1527
1193
1065
739
394
3245
1196
2962
1399
3334
1952
1093
2577
901
83
1559
226
3055
2631
212
2804
1769
925
2652
2040
2106
3917
1608
517
532
1447
2712
1845
2797
628
2551
2637
210
471
3293
1362
1210
2021
1096
1490
2931
3200
4073
1641
1851
4823
864
1502
1674
698
2189
2144
1878
1053
421
337
1524
26...

result:

ok 3000 tokens

Test #3:

score: 0
Accepted
time: 55ms
memory: 96264kb

input:

3000
75
1 1 1 2 2 1 1 2 1 1 2 2 2 1 1 2 1 1 2 1 1 2 1 1 2 2 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 1 2 1 1 1 2 2 2 2 1 1 1 1 2 2 1 1 2 2 1 2 2 2 2 2 2 2 2 1 2 1
69 55 48 7 94 67 75 44 45 87 7 20 74 86 33 87 98 38 59 75 15 67 48 70 89 69 30 34 92 96 91 15 63 54 67 65 9
60
2 2 1 2 2 2 2 1 2 1 2 1 1 2 1 2...

output:

2889
2671
2113
2727
840
2193
2600
2360
267
1367
4648
2062
1740
1853
2114
490
15
2887
452
3794
1031
3428
2213
2862
492
1769
854
2537
4498
3400
1177
2383
576
2621
2306
2799
333
959
2118
2370
3337
1538
605
1327
4021
1998
1038
2290
1725
1202
4298
2001
3841
361
1739
285
234
655
1738
1012
3311
715
3771
54...

result:

ok 3000 tokens

Test #4:

score: 0
Accepted
time: 55ms
memory: 95864kb

input:

3000
52
2 2 1 2 1 1 2 2 1 2 2 1 1 1 1 1 1 2 1 1 2 2 2 1 2 1 1 1 1 2 2 1 2 2 1 1 1 1 2 2 2 1 2 1 1 2 1 2 2 1 1 1
65 79 47 69 16 44 37 86 39 60 84 51 58 31 18 96 82 88 10 100 9 42 83 42 53 25
52
1 2 1 1 1 2 2 2 1 2 2 2 1 2 2 1 1 1 1 2 2 2 1 1 1 2 2 1 2 1 2 2 1 2 2 1 1 1 1 2 1 2 1 2 2 2 1 1 1 2 1 2
95 ...

output:

2573
2152
2784
2808
586
737
3491
3894
1551
3040
1324
2019
766
723
2045
534
1143
584
4691
1052
3218
798
1274
4603
1417
2285
2643
3030
432
840
597
1201
1375
1108
5191
4305
2770
490
2193
1988
2106
2283
4438
998
3188
788
2440
2095
2444
1547
376
2183
111
4627
3248
862
1110
2678
2056
51
2238
1162
1119
400...

result:

ok 3000 tokens

Test #5:

score: 0
Accepted
time: 48ms
memory: 94868kb

input:

3000
62
3 2 2 1 4 5 3 5 5 5 4 5 1 4 2 1 1 2 2 4 4 2 2 1 3 2 1 2 1 3 5 4 1 3 3 2 2 2 3 2 3 1 4 4 3 4 3 4 1 1 2 2 3 4 1 1 5 1 2 4 3 3
421365777 831616822 950787430 270300815 35365972 916821714 542965999 146553901 444779521 340715057 983926749 477210482 475811143 605420792 775843602 739625008 81179672 ...

output:

12141804409
3514131806
13169144445
2834321189
4486289916
9365861534
19576050944
5331567924
2478214236
10120189376
6234037984
4772438519
822409467
9546763284
1890883254
10063013047
9328670724
5941666882
1520030152
12085259192
2527539040
6837130908
3960468724
3734588204
16751260189
745609947
278011044...

result:

ok 3000 tokens

Test #6:

score: 0
Accepted
time: 48ms
memory: 96152kb

input:

3000
95
3 5 1 1 4 2 5 1 2 4 2 2 4 1 5 5 2 3 1 1 4 1 2 2 1 5 5 1 2 4 5 4 2 2 2 3 3 2 5 5 5 4 4 3 1 3 4 3 2 2 2 3 3 5 1 5 1 5 4 4 3 4 4 1 4 3 2 4 2 5 4 3 3 2 1 5 5 4 4 1 5 3 4 5 2 3 4 1 4 1 3 2 3 5 1
424088652 632010471 774450715 372487597 308102681 822963353 769946235 300697393 838458560 119222841 87...

output:

11641825395
423266192
8128431577
2975328961
724283784
7726787435
3958571304
5840908183
13109978662
6141304494
7475762420
5646743023
8075122865
5758155513
7431563108
5017751770
7718700502
11634959512
11745951987
5965010228
15387865647
8535897994
17809028780
23892051157
2323825336
1107917884
431613111...

result:

ok 3000 tokens

Test #7:

score: 0
Accepted
time: 54ms
memory: 96116kb

input:

3000
26
5 3 2 3 3 5 2 3 3 2 5 1 4 4 3 5 2 4 5 5 1 1 1 1 2 5
207931107 614536623 629001820 112093714 506722332 311961087 93990579 67140726 861435900 190087119 97599359 916252534 85629395
6
4 4 1 2 4 2
396521794 780484551 141958399
47
1 1 2 2 2 3 1 1 2 5 1 1 2 2 2 3 3 3 2 1 1 5 2 3 5 4 4 2 5 5 5 3 5 4...

output:

1455517749
396521794
10745876239
6630115397
10402871870
19077444882
572314658
20782278554
4352726008
24006096728
10633448335
3379046930
5272110524
4239680082
12558548995
8497545848
17559672467
16505145636
18494578194
11898911682
9351222916
4119102344
3238900654
10430517563
1686566822
3242206822
6470...

result:

ok 3000 tokens

Test #8:

score: -100
Time Limit Exceeded

input:

3
91459
1 2 2 1 2 1 2 2 1 1 2 1 1 2 1 2 1 1 2 2 1 1 1 2 2 2 1 2 1 2 1 2 1 2 1 1 2 1 1 1 1 2 1 2 2 2 2 2 2 1 2 1 1 2 2 1 1 2 1 2 2 2 2 1 1 2 1 2 1 2 1 1 1 2 2 1 1 2 2 2 1 2 1 1 1 2 2 1 1 1 2 1 2 1 2 1 1 2 2 2 1 1 1 1 1 2 2 2 2 1 2 1 1 2 1 2 1 2 2 2 2 1 1 1 2 1 2 1 2 2 2 2 2 2 1 1 2 1 2 1 1 1 2 1 2 2 ...

output:


result: