QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#410602 | #7506. StrCartesian | grass8cow | AC ✓ | 10898ms | 326104kb | C++17 | 4.6kb | 2024-05-14 10:19:56 | 2024-05-14 10:19:56 |
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]);
}
pi t1[2],t2[2];
int cts(int ax,int bx,int ay,int by){
t1[0]={al[ax],ar[ax]},t1[1]={bl[bx],br[bx]};
t2[0]={al[ay],ar[ay]},t2[1]={bl[by],br[by]};
int x=0,y=0;
while(x<2&&y<2){
pi A=t1[x],B=t2[y];
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)t1[x]=A;else x++;
if(B.fi<=B.se)t2[y]=B;else y++;
}
if(y<2)return 1;
if(x<2)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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 28608kb
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: 3ms
memory: 28592kb
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: 0
Accepted
time: 5197ms
memory: 264224kb
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 ...
result:
ok Correct Answer
Test #4:
score: 0
Accepted
time: 10898ms
memory: 274568kb
input:
42793 43577 osjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjy...
output:
478 23079 19578 5700 32327 37245 23174 34890 8775 43551 33235 7846 40699 41435 24607 32335 10681 5081 40664 38308 4711 11678 31506 13791 13592 7796 9099 15409 35 36153 12575 30943 1418 37944 18258 11644 4315 19248 29431 9382 9938 30089 16317 36775 25241 10476 27724 29981 33560 26458 311 26256 24402 ...
result:
ok Correct Answer
Test #5:
score: 0
Accepted
time: 5010ms
memory: 268060kb
input:
21455 22492 osjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaq...
output:
1153 18391 12397 8491 9381 14158 8926 9710 19054 21286 18999 489 2337 3914 20214 18000 4041 11022 3232 2622 632 12989 4831 1494 7204 17465 7959 13995 4214 14108 14052 20985 1761 21505 3079 11058 16940 15409 4088 14365 14018 20169 19551 9309 16472 21758 13613 21356 6865 9708 11938 3822 14038 1038 370...
result:
ok Correct Answer
Test #6:
score: 0
Accepted
time: 7691ms
memory: 326104kb
input:
33056 33045 yiyrokayfnzhamwfzuadvuyndjspptokvoqlgrgqytwceezsczyljaxyicjepttyvllsbqcfmsseskryayuezexpiypfgswfvzexhocjyzpxuazgrdjngsovhcrkdoirnxfxwjypnamxblzrqbgoomgltuoxkgtdnjsanhszewdkbediutypzjcasccewjnxakdeinqczivdzoecriaabjfuipztnvlapnqrsnqoigciixjrpgjxmtacshfrkeflmxjzmuwmkgpcsgqcgtlyqgugtdyoasns...
output:
13792 9065 8306 26578 7635 30555 18164 11919 11011 8423 25829 23538 30170 28490 19726 650 19941 2741 4050 1943 25996 22793 29283 32610 4160 4087 4759 31867 6046 6176 23450 14827 21825 16147 7681 18029 4173 15086 6943 7840 2462 27663 6954 18606 17654 23682 14061 2338 30610 1881 8076 25979 18709 32844...
result:
ok Correct Answer