QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276720 | #6361. 字符串 | SoyTony | 100 ✓ | 1246ms | 37788kb | C++14 | 3.8kb | 2023-12-06 10:03:33 | 2023-12-06 10:03:35 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn=1e5+10;
const int maxm=2e6+10;
const int mod=20101009;
const ll base=1e8;
const ull base1=10007;
const ull base2=19260817;
const int maxxn=0x3f3f3f3f;
const int minxn=-0x3f3f3f3f;
const db inf=1e13;
const db eps=1e-9;
inline ll read(){
ll x=0,w=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
return x*w;
}
/*
二分这个最大前缀的长度x
如果长度合法的话,一定在[a,b-x+1]中有一个后缀和c的LCP长大于等于x
于是可以二分找到后缀排序后大于x的区间[l,r]
最后需要判断的是,在后缀数组的[l,r]上有没有对应字符串的[a,b-x+1],可以用主席树维护
*/
int n,m;
char s[maxn];
int sa[maxn],rk[maxn],oldrk[maxn<<1],id[maxn],cnt[maxn],tmp[maxn],height[maxn];
inline bool cmp(int x,int y,int l){
return oldrk[x]==oldrk[y]&&oldrk[x+l]==oldrk[y+l];
}
inline void get_sa(){
int w=300;
for(int i=1;i<=n;++i) ++cnt[rk[i]=s[i]];
for(int i=1;i<=w;++i) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;--i) sa[cnt[rk[i]]--]=i;
for(int l=1,k=0;;l<<=1){
k=0;
for(int i=n;i+l>n;--i) id[++k]=i;
for(int i=1;i<=n;++i) if(sa[i]>l) id[++k]=sa[i]-l;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;++i) ++cnt[tmp[i]=rk[id[i]]];
for(int i=1;i<=w;++i) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;--i) sa[cnt[tmp[i]]--]=id[i];
k=0;
memcpy(oldrk,rk,sizeof(rk));
for(int i=1;i<=n;++i) rk[sa[i]]=cmp(sa[i],sa[i-1],l)?k:++k;
if(k==n){
for(int i=1;i<=n;++i) sa[rk[i]]=i;
break;
}
w=k;
}
for(int i=1,k=0;i<=n;++i){
if(k) --k;
while(s[i+k]==s[sa[rk[i]-1]+k]) ++k;
height[rk[i]]=k;
}
}
struct ST{
int f[maxn][20];
inline void build(){
for(int i=1;i<=n;++i) f[i][0]=height[i];
for(int k=1;k<=18;++k){
for(int i=1;i+(1<<k)-1<=n;++i){
f[i][k]=min(f[i][k-1],f[i+(1<<(k-1))][k-1]);
}
}
}
inline int query(int l,int r){
if(l==r) return 1e5+10;
if(l>r) swap(l,r);
++l;
int k=log2(r-l+1);
return min(f[l][k],f[r-(1<<k)+1][k]);
}
}S;
int rt[maxn];
struct SegmentTree{
#define mid ((l+r)>>1)
int ch[maxm][2],val[maxm],tot;
inline void new_node(int &pos){
ch[++tot][0]=ch[pos][0],ch[tot][1]=ch[pos][1],val[tot]=val[pos];
pos=tot;
}
inline void update(int l,int r,int p,int &pos){
new_node(pos);
++val[pos];
if(l==r) return;
if(p<=mid) update(l,mid,p,ch[pos][0]);
else update(mid+1,r,p,ch[pos][1]);
}
inline int query(int l,int r,int pl,int pr,int posl,int posr){
if(pl<=l&&r<=pr) return val[posr]-val[posl];
int res=0;
if(pl<=mid) res+=query(l,mid,pl,pr,ch[posl][0],ch[posr][0]);
if(pr>mid) res+=query(mid+1,r,pl,pr,ch[posl][1],ch[posr][1]);
return res;
}
#undef mid
}T;
inline bool check(int a,int b,int c,int d,int x){
int lpos,rpos;
int l=1,r=rk[c];
while(l<=r){
int mid=(l+r)>>1;
if(S.query(mid,rk[c])>=x) lpos=mid,r=mid-1;
else l=mid+1;
}
l=rk[c],r=n;
while(l<=r){
int mid=(l+r)>>1;
if(S.query(rk[c],mid)>=x) rpos=mid,l=mid+1;
else r=mid-1;
}
return T.query(1,n,a,b-x+1,rt[lpos-1],rt[rpos]);
}
int main(){
n=read(),m=read();
scanf("%s",s+1);
get_sa();
S.build();
for(int i=1;i<=n;++i){
rt[i]=rt[i-1];
T.update(1,n,sa[i],rt[i]);
}
while(m--){
int a=read(),b=read(),c=read(),d=read();
int l=0,r=min(b-a+1,d-c+1),ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(a,b,c,d,mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 0ms
memory: 10136kb
input:
300 300 bababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababbaaaaaaabbaaaaaaaaabaaaaaaabaaaaaaaaaaaaaaaaaaaabaaababaaaaaaaaaaaa...
output:
167 195 126 180 165 180 169 134 128 200 182 222 130 213 186 136 206 155 201 175 164 138 141 202 160 147 192 169 164 207 177 168 203 213 158 196 156 201 194 179 155 129 181 183 170 204 177 176 192 132 185 185 191 212 199 183 221 178 201 212 139 156 147 150 179 140 143 181 213 138 178 126 205 202 169 ...
result:
ok 300 numbers
Test #2:
score: 10
Accepted
time: 16ms
memory: 10524kb
input:
3000 3000 bababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa...
output:
2192 2174 2152 2232 2235 2223 2242 2183 2192 2224 2215 2236 2222 2171 2210 2178 2176 2194 2157 2206 2247 2190 2233 2158 2235 2156 2236 2172 2157 2167 2186 2221 2243 2218 2243 2233 2187 2191 2188 2245 2180 2209 2207 2208 2205 2154 2236 2181 2176 2151 2194 2208 2237 2174 2172 2231 2151 2193 2221 2194 ...
result:
ok 3000 numbers
Test #3:
score: 10
Accepted
time: 8ms
memory: 10412kb
input:
3000 3000 bababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa...
output:
2181 2200 2196 2226 2241 2159 2199 2186 2170 2209 2197 2167 2241 2152 2175 2215 2164 2167 2155 2154 2194 2160 2212 2183 2235 2160 2179 2214 2202 2151 2169 2233 2228 2177 2188 2161 2197 2183 2158 2161 2157 2245 2246 2209 2163 2200 2156 2161 2172 2193 2161 2222 2241 2183 2223 2239 2215 2159 2153 2190 ...
result:
ok 3000 numbers
Test #4:
score: 10
Accepted
time: 16ms
memory: 10464kb
input:
3000 3000 bababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa...
output:
2225 2226 2240 2220 2247 2236 2211 2244 2203 2194 2179 2196 2205 2176 2238 2209 2152 2238 2153 2157 2239 2173 2246 2208 2235 2164 2165 2201 2204 2178 2152 2245 2213 2191 2188 2187 2152 2175 2226 2175 2232 2226 2187 2167 2219 2246 2217 2196 2223 2235 2226 2181 2245 2192 2176 2247 2181 2180 2183 2241 ...
result:
ok 3000 numbers
Test #5:
score: 10
Accepted
time: 1234ms
memory: 37668kb
input:
100000 100000 aaaaaaawaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaajbaaaaaaaaaaxaaaaasaaaaaaaaaabaaaaaaaaaaaaaaaacaaaaaaaaaaaaaaaaaaaaaaaaaaqaaaaaaaaaaaaaaaabtaaaaabaaaabaaaaaaaaaaaabaaaaabaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoaaaaaaaaaaaaaaaaaaaaaaaaabkaaaaaaaaaabbaaaaaaaaaaaxaaaaaa...
output:
27 2 30 4 29 22 21 31 7 12 25 40 27 33 3 16 38 13 12 38 25 25 31 10 3 32 28 15 29 14 21 21 38 18 53 38 10 17 12 10 15 30 25 28 31 18 9 15 26 26 44 6 27 53 6 37 17 16 28 19 15 4 19 35 30 25 16 35 34 14 4 14 24 30 22 48 14 51 27 16 10 40 36 33 32 29 13 28 56 19 27 34 21 19 16 11 5 22 41 23 21 42 8 41 ...
result:
ok 100000 numbers
Test #6:
score: 10
Accepted
time: 1246ms
memory: 36992kb
input:
100000 100000 aaaaaapaaaaaaaaaaaaapaaaaaaaaaybaaaaaaaaaaaaabaaaaaaaaaaaaaaapavaaaaggaaabababaubaabaaaaaaaaalaaaaaaaaajaaaaaaaaaaaaaaaabaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaabbaaaaaaaaaabaaaaaaaaaaaaaaaaaazaaaaoaeaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaa...
output:
37 42 22 17 47 12 55 10 23 34 36 57 27 42 19 38 21 56 21 13 40 10 5 14 51 4 22 21 33 19 20 14 26 23 15 27 32 46 17 26 24 15 28 11 14 10 12 26 23 44 27 6 12 55 16 21 10 21 23 36 26 21 8 14 11 10 31 17 29 18 19 65 28 21 25 56 41 24 31 37 26 22 12 22 48 16 22 15 30 22 37 17 17 6 32 17 43 13 19 22 11 35...
result:
ok 100000 numbers
Test #7:
score: 10
Accepted
time: 1203ms
memory: 37220kb
input:
100000 100000 aaaaaaaaaaaaaaaaaaaaaaacaaaaaaaaaaaaaaabaabaaaaaabaaaaaaaaaaaaaaaaaaaaeaaaabaaaaaaaaaaqabaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaapaaaaaaaafraaaaaaabaaaaaaaaaaabaaaaaaaaaaaqaaabaaaaaaaaaaabaaaaaaaaacaaaaaaaaaaaaaaaaaaaaaaaaqbaaaaaaaaaaaaaaaaa...
output:
5 12 9 36 29 21 28 22 46 5 26 35 9 60 28 11 30 11 20 22 37 54 13 22 26 11 10 26 14 11 30 35 23 20 13 31 19 19 43 19 36 15 36 34 12 16 50 38 15 15 8 24 18 20 14 26 62 22 19 14 41 20 3 25 37 23 23 10 15 8 25 21 20 28 12 30 28 9 31 21 46 31 24 28 19 27 9 41 50 39 16 10 25 18 17 30 44 17 40 8 66 7 23 44...
result:
ok 100000 numbers
Test #8:
score: 10
Accepted
time: 1093ms
memory: 37788kb
input:
100000 100000 aaavaaaaaaaaaaaaaabaaaaaaaaaaaataabbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaaxaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadaaaaaaaaaaaaaaaaaaaaaavaaabaaaaabaaaaaaaaaaaaatabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalaaaaaaaabaaaaanawaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaab...
output:
18 37 25 26 16 14 12 18 19 19 39 38 22 22 39 7 23 21 16 24 16 30 36 26 28 27 33 30 15 19 19 29 34 34 30 28 23 27 26 40 35 8 27 10 25 25 22 22 30 8 12 14 7 31 16 30 20 20 21 24 15 19 9 36 45 6 24 32 29 20 16 11 28 26 14 26 12 6 51 25 51 27 27 27 41 29 22 8 9 19 17 13 26 15 26 21 22 26 26 16 31 28 40 ...
result:
ok 100000 numbers
Test #9:
score: 10
Accepted
time: 1070ms
memory: 37148kb
input:
100000 100000 aaaaaaaaaaaaaaabeaaaaaaaaaaaaaaaaabaaaaaaabaaaaaaalaaaaaaaaaaaaaaaaaaaavaaaaaaaakabaaaaafawbaaaaaaaaaataaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaabaaaaaaaaaaaaaaaaawaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaabaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaoaaaaaaaiaaaaaaaaaaaaaaaaa...
output:
47 21 21 21 48 61 7 35 25 16 17 21 30 21 13 16 22 17 7 25 25 24 34 25 11 31 30 18 8 45 34 52 8 22 50 24 44 45 43 29 59 40 12 14 30 26 35 34 50 29 21 11 40 15 30 51 35 45 22 5 20 16 18 20 34 56 50 45 15 41 5 50 16 19 20 14 20 33 10 32 31 15 15 31 36 23 3 30 47 43 43 21 16 14 31 22 8 63 50 13 35 26 24...
result:
ok 100000 numbers
Test #10:
score: 10
Accepted
time: 1075ms
memory: 37512kb
input:
100000 100000 aaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafaaaaaaaaaaaaaaaaababaaaaaaaaaaaaaaaaadbaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaalraaaakaaaaaaaaawaaaaaaaaaaaaaaaaaaaaaaajaaaaaaaaaaaaaaanbaaaaaaaaaaaadaaaaaataabaaaaaaabaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaa...
output:
9 23 37 20 33 25 32 55 36 16 35 23 22 15 11 21 18 33 15 29 18 21 26 49 12 25 3 16 44 31 13 16 11 23 15 10 53 28 9 22 18 17 23 38 39 29 54 37 22 11 16 26 21 24 12 31 21 33 20 21 35 25 5 4 23 54 19 20 20 16 21 15 50 22 29 29 9 30 5 27 32 29 32 24 25 14 7 21 22 7 11 35 11 24 18 33 9 23 36 20 35 9 13 27...
result:
ok 100000 numbers