QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#187899 | #7506. StrCartesian | bulijiojiodibuliduo# | AC ✓ | 784ms | 225460kb | C++17 | 6.1kb | 2023-09-25 05:12:59 | 2023-09-25 05:12:59 |
Judging History
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: 100
Accepted
time: 0ms
memory: 30520kb
input:
2 3 a ab a aa ba 2 3 4
output:
1 3 1 3
result:
ok Correct Answer
Test #2:
score: 0
Accepted
time: 8ms
memory: 42832kb
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: 646ms
memory: 221084kb
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: 784ms
memory: 222920kb
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: 514ms
memory: 222220kb
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: 546ms
memory: 225460kb
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