QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356783#7036. Can You Solve the Harder Problem?atgcWA 504ms26444kbC++172.0kb2024-03-18 11:48:152024-03-18 11:48:15

Judging History

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

  • [2024-03-18 11:48:15]
  • 评测
  • 测评结果:WA
  • 用时:504ms
  • 内存:26444kb
  • [2024-03-18 11:48:15]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5+10;
int T;
void getsa(int n,int s[],int sa[],int rk[],int bn[],int tp[]) {
	int m = n;
	memset(bn,0,(m+1)*sizeof bn[0]);
	for(int i = 1;i<=n;++i) ++bn[rk[i]=s[i]];
	for(int i = 2;i<=m;++i) bn[i] += bn[i-1];
	for(int i = n;i;--i) sa[bn[rk[i]]--] = i;
	for(int j = 1;j<n;j<<=1) {
		int cnt = 0;
		for(int i = n - j + 1;i<=n;++i) tp[++cnt] = i;
		for(int i = 1;i<=n;++i) if(sa[i] > j) tp[++cnt] = sa[i] - j;
		memset(bn+1,0,m * sizeof bn[0]);
		for(int i = 1;i<=n;++i) ++bn[rk[i]];
		for(int i = 2;i<=m;++i) bn[i] += bn[i-1];
		for(int i = n;i;--i) sa[bn[rk[tp[i]]]--] = tp[i];
		memcpy(tp+1,rk+1,n * sizeof tp[0]);
		cnt = rk[sa[1]] = 1;
		for(int i = 2;i<=n;++i)
			if(tp[sa[i]] == tp[sa[i-1]] && tp[sa[i]+j] == tp[sa[i-1]+j]) rk[sa[i]] = cnt;
			else rk[sa[i]] = ++cnt;
		if((m=cnt)==n) break;
	}
}
void gethr(int n,int s[],int sa[],int rk[],int hi[]) {
	for(int i = 1,k = 0;i<=n;++i) {
		if(k) --k;
		if(rk[i] == 1) continue;
		while(s[i+k] == s[sa[rk[i]-1]+k]) ++k;
		hi[i] = k;
	}
}
int n,a[maxn],ih[maxn],sa[maxn],rk[maxn],bn[maxn],tp[maxn];
auto&hi=bn;

void sol(){
	unordered_map<int,int>id;
	cin>>n;for(int i=1,_=0;i<=n;++i)cin>>a[i],ih[i]=(id.count(a[i])?id[a[i]]:id[a[i]]=++_);
	getsa(n,ih,sa,rk,bn,tp);
	gethr(n,ih,sa,rk,hi);

	static struct STK{
		int pos,val,sufans;
	}s[maxn];
	int tp=0,res = 0;
	s[0]={.pos=n+1,.val=0,.sufans=0};
	for(int i = n;i;--i) {
		while(tp && s[tp].val <= a[i]) s[tp--]={};
		int va=(s[tp].pos-i)*a[i]+s[tp].sufans;
		s[++tp]={.pos=i,.val=a[i],.sufans=va};

		int l=0,r=tp,ind = i+hi[i];// >= ind
		while(l < r){
			int mid=(l+r+1)>>1;
			if(s[mid].pos >= ind) l=mid;
			else r=mid-1;
		}
		res += s[l].sufans + (s[l+1].sufans-s[l].sufans)/(s[l].pos-s[l+1].pos)*(s[l].pos-ind);
	}while(tp)s[tp--]={};
	cout<<res<<'\n';
}

signed main() {
	ios::sync_with_stdio(0),cin.tie(0);
	int T;cin>>T;while(T--)sol();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
3
1 2 3
3
2 3 3

output:

14
14

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 504ms
memory: 26444kb

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:

17377263880
484769420621
1469039857
473739194467
490764968536
305434410403
488123854116
494441224927
484272937511
482411215077
34438338505
225764586244
494494400870
492874976740
486779601551
483167680203
341192630235
492747353987
491280077388
9765154255
491745355442
120812820486
487737922489
4725166...

result:

wrong answer 50th lines differ - expected: '476015445949', found: '475928085348'