QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142102#5368. 异世界的文章分割者DaiRuiChen0070 3ms12076kbC++142.4kb2023-08-18 14:43:242023-08-18 14:43:27

Judging History

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

  • [2023-08-18 14:43:27]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:12076kb
  • [2023-08-18 14:43:24]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=5e4+5;
const ll inf=1e18;
struct Node {
	unordered_map <char,int> ch;
	int link,len;
}	tr[MAXN<<1];
int lst,tot;
inline void init() {
	for(int i=1;i<=tot;++i) {
		tr[i].link=tr[i].len=0;
		tr[i].ch.clear();
	}
	lst=tot=1;
}
inline int insert(char c) {
	int cur=++tot,p=lst;
	tr[cur].len=tr[lst].len+1;
	while(p&&!tr[p].ch.count(c)) tr[p].ch[c]=cur,p=tr[p].link;
	if(!p) tr[cur].link=1;
	else {
		int q=tr[p].ch[c];
		if(tr[q].len==tr[p].len+1) tr[cur].link=q;
		else {
			int r=++tot;
			tr[r]=tr[q],tr[r].len=tr[p].len+1;
			tr[cur].link=tr[q].link=r;
			while(p&&tr[p].ch[c]==q) tr[p].ch[c]=r,p=tr[p].link;
		}
	}
	return lst=cur;
} //SAM
char str[MAXN];
int q[MAXN],w[MAXN];
ll st[MAXN<<1],ed[MAXN<<1]; //ans[i]=qi+w
int buc[MAXN],rnk[MAXN<<1];
map <array<int,2>,int> Q;
inline ll eval(int l,int r) { //get val(S[l,r])
	if(Q.count({l,r})) return Q[{l,r}];
	fill(st+1,st+tot+1,inf),fill(ed+1,ed+tot+1,-inf);
	init();
	for(int i=l;i<=r;++i) {
		int p=insert(str[i]);
		st[p]=ed[p]=i;
	}
	fill(buc,buc+r-l+2,0);
	for(int i=2;i<=tot;++i) ++buc[tr[i].len];
	for(int i=1;i<=r-l+1;++i) buc[i]+=buc[i-1];
	for(int i=2;i<=tot;++i) rnk[buc[tr[i].len]--]=i;
	for(int i=tot-1;i;--i) {
		int p=rnk[i];
		st[tr[p].link]=min(st[tr[p].link],st[p]);
		ed[tr[p].link]=max(ed[tr[p].link],ed[p]);
	}
	fill(q+l,q+r+1,0),fill(w+l,w+r+1,0);
	for(int i=2;i<=tot;++i) {
		auto add=[&](int x,int y,ll nq,ll nw) {
			if(x>y) return ;
			q[x]+=nq,w[x]+=nw;
			if(y<r) q[y+1]-=nq,w[y+1]-=nw;
		};
		int mx=tr[i].len,mn=tr[tr[i].link].len;
		add(st[i]+1,ed[i]-mx,0,mx-mn);
		add(max(st[i]+1,ed[i]-mx+1),ed[i]-mn,-1,ed[i]-mn+1);
	}
	ll ans=0;
	for(int i=l+1;i<=r;++i) {
		q[i]+=q[i-1],w[i]+=w[i-1];
		ans+=(q[i]*i+w[i])*(q[i]*i+w[i]);
	}
	return Q[{l,r}]=ans;
//	return ans;
}
int n,k;
inline bool check(ll x) {
	int st=1,tot=0;
	while(st<=n) {
		if((++tot)>k) return false;
		auto judge=[&](int len) {
			if(st+len-1<=n) {
				return eval(st,st+len-1)<=x;
			}
			return false;
		};
		int k=1,len=0;
		while(judge(1<<k)) ++k;
		len=1<<(k-1);
		for(int d=k-2;~d;--d) if(judge(len+(1<<d))) len+=1<<d;
		st+=len;
	}
	return true;
}
signed main() {
	scanf("%d%d%s",&n,&k,str+1);
	ll l=0,r=inf,res=0;
	while(l<=r) {
		ll mid=(l+r)>>1;
		if(check(mid)) res=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%lld\n",res);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 1ms
memory: 11540kb

input:

10 3
aaaaaaaaaa

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 11708kb

input:

10 1
abbbaabbba

output:

289

result:

ok single line: '289'

Test #3:

score: 0
Accepted
time: 3ms
memory: 11388kb

input:

10 2
cacabbcbca

output:

11

result:

ok single line: '11'

Test #4:

score: 0
Accepted
time: 3ms
memory: 10884kb

input:

10 4
aabbccddaa

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 1ms
memory: 12076kb

input:

10 4
ababbbabab

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 1ms
memory: 11344kb

input:

10 2
ababbaaaba

output:

12

result:

ok single line: '12'

Test #7:

score: -10
Wrong Answer
time: 3ms
memory: 11708kb

input:

10 1
baabaababa

output:

233

result:

wrong answer 1st lines differ - expected: '156', found: '233'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%