QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#306751#7503. Too Many EdgesvavkaRE 0ms0kbC++236.1kb2024-01-17 08:28:042024-01-17 08:28:04

Judging History

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

  • [2024-01-17 08:28:04]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-01-17 08:28:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937_64 mrand(2); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int M=2010,N=50100,L=1010000;
string S;
vector<pair<string,int>> str[M],sb;
VI len;
PII va[N],vb[N];
int n,m,pid[L];
int ts[L*2];
string a[N],b[N];
char s[L];

int q;
ll l[M],r[M],md[M];

struct SuffixArray {

static const int N=2000100;
static const int LOGN=22;

int n;
int sa[N],rk[N],ht[N],so[N];
bool t[N<<1];
int hv[LOGN][N];

bool islms(const int i,const bool *t) {
	return i>0&&t[i]&&!t[i - 1];
}

template<class T>
inline void sort(T s,int *sa,const int len,const int sz,const int sigma,
					bool *t,int *b,int *cb,int *p) {
	memset(b,0,sizeof(int)*sigma);
	memset(sa,-1,sizeof(int)*len);
	rep(i,0,len) b[(int)s[i]]++;
	cb[0]=b[0];
	rep(i,1,sigma) cb[i]=cb[i-1]+b[i];
	per(i,0,sz) sa[--cb[(int)s[p[i]]]]=p[i];
	rep(i,1,sigma) cb[i]=cb[i-1]+b[i-1];
	rep(i,0,len) if (sa[i]>0&&!t[sa[i]-1]) sa[cb[(int)s[sa[i]-1]]++]=sa[i]-1;
	cb[0]=b[0];
	rep(i,1,sigma) cb[i]=cb[i-1]+b[i];
	per(i,0,len) if (sa[i]>0&&t[sa[i]-1]) sa[--cb[(int)s[sa[i]-1]]]=sa[i]-1;
}

template<class T>
inline void sais(T s,int *sa,const int len,bool *t,int *b,int *b1,
				const int sigma) {
	int p=-1,*cb=b+sigma;
	t[len-1]=1;
	per(i,0,len-1) t[i]=s[i]<s[i+1]||(s[i]==s[i+1]&&t[i+1]);
	int sz=0,cnt=0;
	rep(i,1,len) if (t[i]&&!t[i-1]) b1[sz++]=i;
	sort(s,sa,len,sz,sigma,t,b,cb,b1);
	sz=0;
	rep(i,0,len) if (islms(sa[i],t)) sa[sz++]=sa[i];
	rep(i,sz,len) sa[i]=-1;
	rep(i,0,sz) {
		int x=sa[i];
		rep(j,0,len) {
			if (p==-1||s[x+j]!=s[p+j]||t[x+j]!=t[p+j]) {
				cnt++; p=x;
				break;
			} else if (j>0&&(islms(x+j,t)||islms(p+j,t))) {
				break;
			}
		}
		sa[sz+(x>>=1)]=cnt-1;
	}
	for (int i=len-1,j=len-1;i>=sz;i--) if (sa[i]>=0) sa[j--]=sa[i];
	int *s1=sa+len-sz,*b2=b1+sz;
	if (cnt<sz) sais(s1,sa,sz,t+len,b,b1+sz,cnt);
	else rep(i,0,sz) sa[s1[i]]=i;
	rep(i,0,sz) b2[i]=b1[sa[i]];
	sort(s,sa,len,sz,sigma,t,b,cb,b2);
}

template<class T>
inline void getHeight(T s,int n) {
	rep(i,1,n+1) {
		//printf("sa %d ",sa[i]); puts("");
		rk[sa[i]]=i;
	}
	int j=0,k=0;
	for (int i=0;i<n;ht[rk[i++]]=k)
		for (k?k--:0,j=sa[rk[i]-1];s[i+k]==s[j+k];k++);
}

template<class T>
inline void init(T s,const int len,const int sigma) {
	sais(s,sa,len,t,rk,ht,sigma);
}

inline void solve(int *s,int len,int sigma=28) { //    0-based, s[len]=0
	s[len]=0;
	n=len;
	init(s,len+1,28);
	rep(i,0,len) so[i]=s[i];
	getHeight(s,len);
	rk[len]=0;
	rep(i,1,len+1) hv[0][i]=ht[i];
	rep(j,1,LOGN) for (int i=1;i+(1<<j)-1<=len;i++)
		hv[j][i]=min(hv[j-1][i],hv[j-1][i+(1<<(j-1))]);
}
int lcp(int p,int q) {    // 1-based
	if (q>n) return 0;
	if (p==q) return n-p+1;
	p=rk[p-1]; q=rk[q-1];
	if (p>q) swap(p,q);
	int w=31-__builtin_clz(q-p);
	return min(hv[w][p+1],hv[w][q-(1<<w)+1]);
}

int cmp(int p,int q,int len) { // 1-based p<=q
	int d=lcp(p,q);
	if (d>=len) return 0;
	assert(so[p+d-1]!=so[q+d-1]);
	return so[p+d-1]<so[q+d-1]?-1:1;
}

}sa;


int cmp(PII a,PII b) { // a<=b
	static PII ta[2],tb[2];
	ta[0]=va[a.fi]; ta[1]=vb[a.se];
	tb[0]=va[b.fi]; tb[1]=vb[b.se];
	int p1=0,p2=0;
	while (p1<2&&p2<2) {
		int l1=ta[p1].se-ta[p1].fi+1;
		int l2=tb[p2].se-tb[p2].fi+1;
		if (l1==l2) {
			int w=sa.cmp(ta[p1].fi,tb[p2].fi,l1);
			if (w!=0) return w;
			p1++; p2++;
		} else if (l1<l2) {
			int w=sa.cmp(ta[p1].fi,tb[p2].fi,l1);
			if (w!=0) return w;
			p1++;
			tb[p2].fi+=l1;
		} else {
			int w=sa.cmp(ta[p1].fi,tb[p2].fi,l2);
			if (w!=0) return w;
			p2++;
			ta[p1].fi+=l2;
		}
	}
	if (p1==2&&p2==2) return 0;
	if (p1==2) return -1;
	else return 1;
}


PII getpair(int lm,ll x) {	
	int p1=x/m,p2=x%m;
	return mp(str[lm][p1].se,sb[p2].se);
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,0,n) {
		scanf("%s",s);
		a[i]=s;
		va[i]=mp(SZ(S)+1,SZ(S)+SZ(a[i]));
		S+=a[i];
		len.pb(SZ(a[i]));

	}
	sort(all(len));
	len.erase(unique(all(len)),len.end());
	int lm=SZ(len);
	rep(i,0,lm) pid[len[i]]=i;
	rep(i,0,n) str[pid[SZ(a[i])]].pb(mp(a[i],i));
	rep(i,0,lm) sort(all(str[i]));
	rep(i,0,m) {
		scanf("%s",s);
		b[i]=s;
		vb[i]=mp(SZ(S)+1,SZ(S)+SZ(b[i]));
		S+=b[i];
		sb.pb(mp(b[i],i));
	}
	sort(all(sb));
	rep(i,0,SZ(S)) ts[i]=S[i]-'a'+1;
	sa.solve(ts,SZ(S));
	/*rep(i1,0,n*m) rep(i2,0,n*m) {
		string s1=a[i1/m]+b[i1%m];
		string s2=a[i2/m]+b[i2%m];
		int d=s1==s2?0:((s1<s2)?-1:1);
		printf("%d %d %d %d\n",i1/m,i1%m,i2/m,i2%m);
		assert(d==cmp(mp(i1/m,i1%m),mp(i2/m,i2%m)));
	}
	return 0;*/
	scanf("%d",&q);
	rep(i,0,q) {
		ll rk;
		scanf("%lld",&rk);
		rep(i,0,lm) l[i]=0,r[i]=(ll)SZ(str[i])*m-1;
		while (1) {
			ll val=0;
			rep(i,0,lm) if (l[i]<=r[i]) val+=r[i]-l[i]+1;
			ll pivot=mrand()%val;
			PII mid(-1,-1);
			rep(i,0,lm) if (l[i]<=r[i]) {
				ll rs=r[i]-l[i]+1;
				if (pivot>=rs) pivot-=rs;
				else {
					mid=getpair(i,l[i]+pivot);
					break;
				}
			}
			assert(mid!=mp(-1,-1));
			int ls=0;
			rep(i,0,lm) if (l[i]<=r[i]) {
				ll L=l[i]-1,R=r[i]+1;
				while (L+1<R) {
					ll M=(L+R)/2;
					PII w=getpair(i,M);
					if (cmp(w,mid)>=0) R=M; else L=M;
				}
				md[i]=R;
				ls+=md[i]-l[i];
			}
			if (ls<rk) {
				rk-=ls;
				rep(i,0,lm) if (l[i]<=r[i]) l[i]=md[i];
			} else {
				rep(i,0,lm) if (l[i]<=r[i]) r[i]=md[i]-1;
			}
			PII pr(-1,-1);
			rep(i,0,lm) if (l[i]<=r[i]) { pr=getpair(i,l[i]); break; }
			bool as=1;
			rep(i,0,lm) {
				if (l[i]<r[i]) {
					as=0;
				}
				if (l[i]==r[i]) {
					as&=cmp(pr,getpair(i,l[i]))==0;
				}
				if (!as) break;
			}
			if (as) {
				printf("%d %d\n",pr.fi+1,pr.se+1);
				break;
			}
		}
	}
}

详细

Test #1:

score: 0
Runtime Error

input:

5 5
1 2
1 3
2 5
3 4
4 5

output:


result: