/ Randle /

记录详情

Accepted

/in/foo.cc: In function 'int cmp(int, int)':
/in/foo.cc:38:2: warning: 'len' may be used uninitialized in this function [-Wmaybe-uninitialized]
  if(lena==len) return 1;
  ^~
/in/foo.cc: In function 'int main()':
/in/foo.cc:39:2: warning: 'len' may be used uninitialized in this function [-Wmaybe-uninitialized]
  if(lenb==len) return 0;
  ^~
/in/foo.cc:31:40: note: 'len' was declared here
  reg int l=1,r=std::min(lena,lenb),mid,len;
                                        ^~~
# 状态 耗时 内存占用
#1 Accepted 2ms 2.211 MiB
#2 Accepted 2ms 2.223 MiB
#3 Accepted 2ms 2.219 MiB
#4 Accepted 2ms 2.227 MiB
#5 Accepted 7ms 2.441 MiB
#6 Accepted 8ms 2.605 MiB
#7 Accepted 29ms 5.934 MiB
#8 Accepted 59ms 5.836 MiB
#9 Accepted 74ms 6.422 MiB
#10 Accepted 70ms 6.227 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 100005
#define base 131
int n,m;
char s[N];
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){
	if(a==b) return 1;
	if(s[a]^s[b]) return s[a]<s[b];
	int lena=std::min(m,n-a+1),lenb=std::min(m,n-b+1);
	reg int l=1,r=std::min(lena,lenb),mid,len;
	while(l<=r){
		mid=(l+r)>>1;
		if(hash[a+mid-1]-hash[a-1]*power[mid]==hash[b+mid-1]-hash[b-1]*power[mid]) len=mid,l=mid+1;
		else r=mid-1;
	}
	if(lena==lenb&&lena==len) return a<b;
	if(lena==len) return 1;
	if(lenb==len) 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\n",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-28 11:15:06
评测时间
2020-08-28 11:15:06
评测机
分数
100
总耗时
261ms
峰值内存
6.422 MiB