QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#410589 | #7506. StrCartesian | grass8cow | TL | 17ms | 13552kb | C++17 | 4.7kb | 2024-05-14 10:12:48 | 2024-05-14 10:12:48 |
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];
t2[i]=l-1;
l=pl[i],r=pr[i];
if(Sa::cts(i,l,X,Y)>=0){
if(Sa::cts(i,r,X,Y)>=0)t2[i]=r;
else{
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(k<=s2){for(int i=1;i<=n;i++)pr[i]=t2[i];}
else{k-=s2;for(int i=1;i<=n;i++)pl[i]=t2[i]+1;}
bool gd=1;
X=-1,Y=-1;
for(int i=1;i<=n;i++)if(pl[i]<=pr[i]){
if(pl[i]<pr[i]){gd=0;break;}
if(X!=-1&&Sa::cts(X,Y,i,pl[i])!=0){gd=0;break;}
X=i,Y=pl[i];
}
if(gd){printf("%d %d\n",X,sa[Y]);return;}
}
}
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: 4ms
memory: 12088kb
input:
2 3 a ab a aa ba 2 3 4
output:
2 1 2 1
result:
ok Correct Answer
Test #2:
score: 0
Accepted
time: 17ms
memory: 13552kb
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...
output:
11748 7566 13026 16461 9916 11268 12937 6138 20884 12212 310 19018 14665 17118 7698 15274 18832 22061 7551 10949 12970 7192 1098 9710 19691 12422 5351 11239 14696 2061 2889 12319 8909 10800 6739 1691 19504 5705 599 16293 2991 6114 20067 16922 11544 17617 7562 4195 7364 20824 19750 22736 15334 22205 ...