/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 1ms 224.0 KiB
#2 Accepted 2ms 2.215 MiB
#3 Accepted 1ms 228.0 KiB
#4 Accepted 2ms 2.215 MiB
#5 Wrong Answer 7ms 2.461 MiB
#6 Wrong Answer 6ms 2.539 MiB
#7 Accepted 27ms 3.551 MiB
#8 Accepted 69ms 3.555 MiB
#9 Wrong Answer 59ms 3.551 MiB
#10 Wrong Answer 69ms 3.535 MiB

代码

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
	register int x=0;register int y=1;
	register char c=std::getchar();
	while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
	while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
	return y?x:-x;
}
#define N 50005
#define base 822
int n,m;
char s[50006];
int a[N];
unsigned long long hash[N],power[N];
inline void work_hash(){
	for(reg int i=1;i<=n;i++) hash[i]=hash[i-1]*base+s[i];
}
inline int cmp(int a,int b){
	int lena=std::min(m,n-a+1),lenb=std::min(m,n-b+1);
	reg int l=0,r=std::min(lena,lenb),mid,len=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(hash[a+mid-1]-hash[a-1]*power[mid]+(unsigned long long)(-1)
		==hash[b+mid-1]-hash[b-1]*power[mid]+(unsigned long long)(-1)) len=mid,l=mid+1;
		else r=mid-1;
	}
	if(len==lena&&len==lenb) return a<b;
	if(len==lena) return 1;
	if(len==lenb) return 0;
	return s[a+len]<s[b+len];
}
struct tr{
	tr *ls,*rs;
	int x;
}dizhi[N*2],*root=&dizhi[0];
int tot;
inline void build(tr *tree,int l,int r){
	if(l==r) return;
	int mid=(l+r)>>1;
	tree->ls=&dizhi[++tot];tree->rs=&dizhi[++tot];
	build(tree->ls,l,mid);build(tree->rs,mid+1,r);
}
inline void change(tr *tree,int l,int r,int pos){
	if(l==r) return tree->x++,void();
	int mid=(l+r)>>1;
	pos<=mid?change(tree->ls,l,mid,pos):change(tree->rs,mid+1,r,pos);
	tree->x++;
}
inline int ask(tr *tree,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return tree->x;
	int mid=(l+r)>>1;
	if(qr<=mid) return ask(tree->ls,l,mid,ql,qr);
	if(ql>mid) return ask(tree->rs,mid+1,r,ql,qr);
	return ask(tree->rs,mid+1,r,ql,qr)+ask(tree->ls,l,mid,ql,qr);
}
int main(){
//		std::freopen("sort.in","r",stdin);
//		std::freopen("sort.out","w",stdout);
	n=read();m=read();
	scanf("%s",s+1);
	work_hash();
	power[0]=1;
	for(reg int i=1;i<=n;i++){
		power[i]=power[i-1]*base;a[i]=i;
	}
	std::sort(a+1,a+1+n,cmp);
//		for(reg int i=1;i<=n;i++) printf("%d ",a[i]);EN;
	build(root,1,n);
	change(root,1,n,a[1]);
	long long ans=0;
	for(reg int i=2;i<=n;i++){
		if(a[i]^n) ans+=ask(root,1,n,a[i]+1,n);
		change(root,1,n,a[i]);
	}
	printf("%lld\n",ans);
	return 0;
}

信息

递交者
类型
递交
题目
后缀数组
题目数据
下载
语言
C++
递交时间
2020-08-27 17:28:50
评测时间
2020-08-27 17:28:50
评测机
分数
60
总耗时
247ms
峰值内存
3.555 MiB