QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#657556#7036. Can You Solve the Harder Problem?veg#WA 1860ms70020kbC++172.5kb2024-10-19 14:58:552024-10-19 14:58:58

Judging History

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

  • [2024-10-19 14:58:58]
  • 评测
  • 测评结果:WA
  • 用时:1860ms
  • 内存:70020kb
  • [2024-10-19 14:58:55]
  • 提交

answer

#include <bits/stdc++.h>
#define rint int
using namespace std;
const int maxn = 1000005;
int LOG[maxn];
int sa[maxn];
int bar[maxn];
int fir[maxn];
int sec[maxn<<1];
int rk[maxn];
int hei[maxn];
int s[maxn];
int pre[maxn];
int lsh[maxn];
int nxt[maxn];
int sta[maxn];
int n,m, sss[maxn];

int top, stk[maxn];
long long sum[maxn];
int st[maxn][21];

void qsort()
{
	memset(bar,0,m+1<<2);
	for(rint i=1;i<=n;i++) bar[fir[i]]++;
	for(rint i=1;i<=m;i++) bar[i]+=bar[i-1];
	for(rint i=n;i>=1;i--) sa[bar[fir[sec[i]]]--]=sec[i];
}

void get_sa()
{
	for(rint i=1;i<=n;i++) fir[i]=s[i],sec[i]=i;
	qsort();
	for(rint k=1;;k<<=1)
	{
		rint num=0;
		for(rint i=1;i<=k;i++) sec[++num]=n-k+i;
		for(rint i=1;i<=n;i++) if(sa[i]>k) sec[++num]=sa[i]-k;
		qsort();
		memcpy(sec,fir,sizeof(fir));
		fir[sa[1]]=m=1;
		for(rint i=2;i<=n;i++)
			fir[sa[i]]=sec[sa[i]]==sec[sa[i-1]]&&sec[sa[i]+k]==sec[sa[i-1]+k]?m:++m;
		if(m==n) return;
	}
}

void get_hei()
{
	for(rint i=1;i<=n;i++) rk[sa[i]]=i;
	for(rint i=1,k=0;i<=n;i++)
	{
		if(k) k--;
		rint j=sa[rk[i]-1];
		while(s[i+k]==s[j+k]) k++;
		hei[rk[i]]=k;
	}
	
	for (int i = 1; i <= n; ++i)
		st[i][0] = hei[i];
	for (int j = 1; j <= 20; ++j)
		for (int i = 1; i <= n; ++i)
			st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}

int get_lcp(int x, int y) {
	int len = LOG[y - x + 1];
	return min(st[x][len], st[y - (1 << len) + 1][len]);
}

int main() {
	LOG[0] = -1;
	for (int i = 1; i <= 1000000; ++i) LOG[i] = LOG[i >> 1] + 1;
	int T;
	cin >> T;
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i) {
			scanf("%d", &s[i]);
			lsh[i]=s[i];
			sss[i] = s[i];
		}
		sort(lsh+1,lsh+n+1);
		m=unique(lsh+1,lsh+n+1)-lsh-1;
		for(rint i=1;i<=n;i++)
			s[i]=lower_bound(lsh+1,lsh+m+1,s[i])-lsh;
		get_sa(),get_hei();
		for (int i = 1; i <= n; ++i) s[i] = sss[i];
		int top = 0;
		stk[0] = n + 1;
		for (int i = n; i >= 1; --i) {
			while (top && s[i] >= s[stk[top]]) --top;
			stk[++top] = i;
			sum[top] = sum[top - 1] + 1ll * s[i] * (stk[top - 1] - i);
		}
		
		set<int> ss;
		long long ans = 0;
		for (int i = n; i >= 1; --i) {
			int lcp = 0;
			auto up = ss.upper_bound(rk[i]);
			if (up != ss.end()) lcp = max(lcp, get_lcp(rk[i] + 1, *up));
			if (up != ss.begin()) --up, lcp = max(lcp, get_lcp(*up + 1, rk[i]));
			ss.insert(rk[i]);
			
			int idx = lower_bound(stk + 1, stk + top + 1, i + lcp, greater<int>()) - stk;
			ans += sum[idx - 1] + 1ll * s[i] * (stk[idx - 1] - i - lcp);
		}
		cout << ans << endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
3
1 2 3
3
2 3 3

output:

14
14

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 1860ms
memory: 70020kb

input:

1000
407
205460 200518 200561 199235 198814 198136 198802 206763 198795 200705 205862 209366 204017 209481 206300 198499 197364 200897 208928 196983 205605 206396 205140 201050 199886 207314 196947 207905 204999 203288 200298 198594 198286 197714 206506 203962 197628 201127 206380 199090 200711 2063...

output:

17066168538
421603842003
1342334851
450439150919
322892925003
280429547260
252441702569
339129204955
273276404990
297831188731
32407195573
190590857234
310828014018
276533179425
270636313780
385144671535
340136148813
383547368924
271942019303
9417164178
373155976458
116180742495
366871615669
2463258...

result:

wrong answer 1st lines differ - expected: '17377263880', found: '17066168538'