QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#410373 | #7506. StrCartesian | grass8cow | TL | 64ms | 14328kb | C++17 | 4.5kb | 2024-05-13 22:28:40 | 2024-05-13 22:28:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pi pair<int,int>
#define fi first
#define se second
#define pb push_back
const int M=5e4+10,N=2e6+10;
int al[M],ar[M],bl[M],br[M];
char c[N];
int lg[N];
namespace Sa{
int sa[N<<2],t[N<<2],rk[N<<2],tr[N<<2];
int f[N][22],m;
void build(int n){
m=128;
for(int i=1;i<=n;i++)t[rk[i]=c[i]]++;
for(int i=1;i<=m;i++)t[i]+=t[i-1];
for(int i=n;i;i--)sa[t[rk[i]]--]=i;
for(int k=1;;k<<=1){
int p=0;
for(int i=n-k+1;i<=n;i++)tr[++p]=i;
for(int i=1;i<=n;i++)if(sa[i]>k)tr[++p]=sa[i]-k;
for(int i=1;i<=m;i++)t[i]=0;
for(int i=1;i<=n;i++)t[rk[i]]++;
for(int i=1;i<=m;i++)t[i]+=t[i-1];
for(int i=n;i;i--)sa[t[rk[tr[i]]]--]=tr[i];
for(int i=1;i<=n;i++)tr[i]=rk[i];
rk[sa[1]]=p=1;
for(int i=2;i<=n;i++)
rk[sa[i]]=(tr[sa[i-1]]==tr[sa[i]]&&tr[sa[i-1]+k]==tr[sa[i]+k])?p:(++p);
if(p==n)break;m=p;
}
for(int i=1,k=0;i<=n;i++){
if(k)k--;
int j=sa[rk[i]-1];
while(i+k<=n&&j+k<=n&&c[i+k]==c[j+k])k++;
f[rk[i]][0]=k;
}
for(int j=1;j<=20;j++)for(int i=1;i+(1<<j)-1<=n;i++)f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
int lcp(int x,int y){
x=rk[x],y=rk[y];if(x>y)swap(x,y);
if(x==y)return N;
x++;int k=lg[y-x+1];
return min(f[x][k],f[y-(1<<k)+1][k]);
}
int cts(int ax,int bx,int ay,int by){
vector<pi>x,y;
x.pb({bl[bx],br[bx]});
x.pb({al[ax],ar[ax]});
y.pb({bl[by],br[by]});
y.pb({al[ay],ar[ay]});
while(!x.empty()&&!y.empty()){
pi A=x.back(),B=y.back();x.pop_back(),y.pop_back();
int lc=lcp(A.fi,B.fi),mo=min(A.se-A.fi+1,B.se-B.fi+1);
if(lc<mo)return (c[A.fi+lc]<c[B.fi+lc])?1:-1;
A.fi+=mo,B.fi+=mo;
if(A.fi<=A.se)x.pb(A);
if(B.fi<=B.se)y.pb(B);
}
if(!y.empty())return 1;
if(!x.empty())return -1;
return 0;
}
}
int m,sa[M];
namespace Trie{
int ch[N][26],cn,e,wh[N],bl_[M],br_[M];
void dfs(int x){
if(wh[x])sa[++e]=wh[x];
for(int i=0;i<26;i++)if(ch[x][i])dfs(ch[x][i]);
}
void get(){
cn=1;
for(int i=1;i<=m;i++){
int u=1;
for(int j=bl[i];j<=br[i];j++){
int z=c[j]-'a';
if(!ch[u][z])ch[u][z]=++cn;
u=ch[u][z];
}
wh[u]=i;
}
dfs(1);assert(e==m);
for(int i=1;i<=m;i++)bl_[i]=bl[sa[i]],br_[i]=br[sa[i]];
for(int i=1;i<=m;i++)bl[i]=bl_[i],br[i]=br_[i];
}
}
#define ll long long
mt19937 rnd(time(0));
int n,pl[50100],pr[50100],t1[50100],t2[50100];
void Sol(ll k){
for(int i=1;i<=n;i++)pl[i]=1,pr[i]=m;
while(1){
ll ns=0;
for(int i=1;i<=n;i++)if(pl[i]<=pr[i])ns+=pr[i]-pl[i]+1;
assert(ns);
ll wh=(rnd()*rnd()%ns+ns)%ns;
int X=-1,Y=-1;
for(int i=1;i<=n;i++)if(pl[i]<=pr[i]){
if(wh<pr[i]-pl[i]+1){X=i,Y=pl[i]+wh;break;}
else wh-=pr[i]-pl[i]+1;
}
ll s1=0,s2=0;
for(int i=1;i<=n;i++)if(pl[i]<=pr[i]){
int l=pl[i],r=pr[i];
t1[i]=t2[i]=l-1;
while(l<=r){
int mi=(l+r)>>1;
if(Sa::cts(i,mi,X,Y)==1)t1[i]=mi,l=mi+1;
else r=mi-1;
}
s1+=t1[i]-pl[i]+1;
l=pl[i],r=pr[i];
while(l<=r){
int mi=(l+r)>>1;
if(Sa::cts(i,mi,X,Y)>=0)t2[i]=mi,l=mi+1;
else r=mi-1;
}
s2+=t2[i]-pl[i]+1;
}
if(s1<k&&k<=s2){printf("%d %d\n",X,sa[Y]);return;}
assert(s2);
if(k<=s1){for(int i=1;i<=n;i++)if(pl[i]<=pr[i])pr[i]=t1[i];}
else{k-=s2;for(int i=1;i<=n;i++)if(pl[i]<=pr[i])pl[i]=t2[i]+1;}
}
}
int ns;
int main(){
lg[0]=-1;
for(int i=1;i<=2000000;i++)lg[i]=lg[i>>1]+1;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",c+ns+1);int L=strlen(c+ns+1);
al[i]=ns+1,ar[i]=ns+L,ns+=L;
pl[i]=1,pr[i]=m;
}
for(int i=1;i<=m;i++){
scanf("%s",c+ns+1);int L=strlen(c+ns+1);
bl[i]=ns+1,br[i]=ns+L,ns+=L;
}
Trie::get();Sa::build(ns);
int q;scanf("%d",&q);
ll K;
while(q--)scanf("%lld",&K),Sol(K);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 14328kb
input:
2 3 a ab a aa ba 2 3 4
output:
1 3 2 1
result:
ok Correct Answer
Test #2:
score: 0
Accepted
time: 64ms
memory: 13464kb
input:
27 28 ptdtljwzjbyctrjetnossaiogcfce kmuwnsxjenpjybcwrecoqddbltzvnkzzrxvne lsbqkpbgihqahpotzrnggqrompohf ixonxrpixmurqgdtedhavpawmddfw ruhqnxhvucv fvarlrf uyxxjzjf tlzrxaykw kldatopsv fywmszxti yraazxvbfebdhekphdrqhvpmferaghezebm dovtqdvdd waohxsasgl biakvxhqx aeyzaqum repmbqtrvwd dvqojkduc sjxapwnyz...
output:
23 1 2 27 4 11 2 11 21 9 11 13 7 6 27 1 5 26 17 3 21 23 1 10 6 25 13 5 17 20 22 22 1 5 23 21 10 23 3 18 10 14 16 14 18 5 22 26 14 6 12 22 19 17 14 17 23 28 10 20 26 15 27 6 19 28 16 27 21 25 16 1 6 8 10 20 12 19 1 10 17 10 14 14 18 19 15 27 10 9 9 27 4 19 8 1 17 7 21 1 17 26 26 13 22 13 27 7 2 28 15...
result:
ok Correct Answer
Test #3:
score: -100
Time Limit Exceeded
input:
21673 22789 osjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwn...