QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#351651 | #6419. Baby's First Suffix Array Problem | crsfaa | TL | 3075ms | 13776kb | C++14 | 4.5kb | 2024-03-12 10:10:32 | 2024-03-12 10:10:32 |
Judging History
answer
#include<bits/stdc++.h>
#define Yukinoshita namespace
#define Yukino std
using Yukinoshita Yukino;
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int mxn=2e5+5;
struct SA
{
#define f(i,a,b) for(i=a;i<=b;i++)
#define e(i,a,b) for(i=a;i>=b;i--)
int sa[mxn],height[mxn];
int cnt[mxn],rk[mxn];
int preN;
void S_sort(char s[],int l,int m)
{
int i,k,ct;
f(i,1,l) cnt[height[i]=s[i]]++;
f(i,2,m) cnt[i]+=cnt[i-1];
e(i,l,1) sa[cnt[height[i]]--]=i;
for(k=1;k<=l;k*=2)
{
ct=0;
f(i,l-k+1,l) rk[++ct]=i;
f(i,1,l)
if(sa[i]>k)
rk[++ct]=sa[i]-k;
f(i,1,m) cnt[i]=0;
f(i,1,l) cnt[height[i]]++;
f(i,2,m) cnt[i]+=cnt[i-1];
e(i,l,1) sa[cnt[height[rk[i]]]--]=rk[i],rk[i]=0;
swap(height,rk);
height[sa[1]]=m=1;
f(i,2,l)
height[sa[i]]=m=m+!(rk[sa[i]]==rk[sa[i-1]]&&rk[sa[i]+k]==rk[sa[i-1]+k]);
if(m==l) return;
}
}
void build(char s[],int l,int m)
{
memset(cnt,0,preN);
memset(rk,0,preN);
memset(height,0,preN);
memset(sa,0,preN);
S_sort(s,l,m);
int i,j,k;
for(i=1;i<=l;i++)
rk[sa[i]]=i;
for(i=1,k=0;i<=l;i++)
{
if(rk[i]==1) k=0;
else
{
k-=k>0;
int j=sa[rk[i]-1];
while(i+k<=l&&j+k<=l&&s[i+k]==s[j+k])
k++;
}
height[rk[i]]=k;
}
preN=max(l,m)+1<<4;
}
};
struct Word_RMQ
{
#define w 32
#define u unsigned
#define be(x) ((x>>5)+1)
#define bm mxn/w+5
int *a;
int ST[int(log2(bm)+1)][bm];
int lg[bm];
int l[bm],r[bm];
struct node
{
u st[w+1];
int *p;
int ask(int l,int r)
{
return p[l+__builtin_ctz(st[r]>>l-1)];
}
void init(int *p_,int len)
{
p=p_;
int i,top=0;
int stack[w+1];
for(i=1;i<=len;i++)
{
st[i]=st[i-1];
while(top&&p[i]<p[stack[top]])
st[i]-=1u<<stack[top--]-1;
stack[++top]=i,st[i]+=1u<<i-1;
}
}
}k[bm];
void build(int A[],int n)
{
a=A;
int i,j,cnt=0,mn;
for(i=0;i<=n;i+=w)
l[++cnt]=i,r[cnt]=min(n,i+w-1);
l[1]=1;
for(i=1;i<=cnt;i++)
{
k[i].init(a+l[i]-1,r[i]-l[i]+1);
mn=INT_MAX;
for(j=l[i];j<=r[i];j++)
mn=min(mn,a[j]);
ST[0][i]=mn;
}
for(j=1;(1<<j)<=cnt;j++)
for(i=1;i+(1<<j)-1<=cnt;i++)
ST[j][i]=min(ST[j-1][i],ST[j-1][i+(1<<(j-1))]);
for(i=2;i<=cnt;i++)
lg[i]=lg[i>>1]+1;
}
int STask(int L,int R)
{
if(L>R) return INT_MAX;
int k=lg[R-L+1];
return min(ST[k][L],ST[k][R-(1<<k)+1]);
}
int ask(int L,int R)
{
if(L>R) return INT_MAX;
if(be(L)==be(R)) return k[be(L)].ask(L-l[be(L)]+1,R-l[be(L)]+1);
return min({STask(be(L)+1,be(R)-1),ask(L,r[be(L)]),ask(l[be(R)],R)});
}
#undef w
}ds;
SA a;
int L;
void build(char s[],int l,int m)
{
a.build(s,l,m);
ds.build(a.height,l);
}
int asklcp(int x,int y)
{
if(x==y) return L-x+1;
x=a.rk[x],y=a.rk[y];
if(x>y) swap(x,y);
return ds.ask(x+1,y);
}
char s[mxn];
int ans[mxn];
int n;
struct bit
{
#define lowbit(x) (x&-x)
int t[mxn];
void add(int x,int v)
{
for(;x<=n;x+=lowbit(x))
t[x]+=v;
}
int ask(int x)
{
int s=0;
for(;x;x-=lowbit(x))
s+=t[x];
return s;
}
}T0;
vector<pair<int,pair<int,int>>> qwq[mxn];
/*
*/
int main()
{
int T=read();
while(T--)
{
n=read();
memset(T0.t,0,n+1<<2);
int m=read(),i,j,l,r,k,p;
scanf("%s",s+1);
build(s,n,'z');
for(i=1;i<=n;i++)
qwq[i].clear();
for(i=1;i<=m;i++)
{
int sum=1,w=0;
l=read(),r=read(),k=read();
for(j=1<<20;j;j>>=1)
w+=(w+j<a.rk[k]&&ds.ask(w+j+1,a.rk[k])<=r-k)*j;
qwq[w].push_back({i,{l,k-1}});
sum+=r-k;
for(p=a.rk[k]+1;p<=n;p++)
if(a.sa[p]+ds.ask(a.rk[k]+1,p)<=r&&a.sa[p]>=k+1&&a.sa[p]<=r)
sum--;
// else sum--;
// for(p=k+1;p<=r;p++)
// if(p+asklcp(p,k)<=r)
// sum+=a.rk[p]<a.rk[k];
// else sum++;
ans[i]=sum;
}
for(i=1;i<=n;i++)
{
T0.add(a.sa[i],1);
for(auto x:qwq[i])
ans[x.first]+=T0.ask(x.second.second)-T0.ask(x.second.first-1);
}
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
}
/*
1
10 10
abaabbaabb
2 8 3
1
10 10
abaabbaabb
2 8 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12556kb
input:
2 10 4 baaabbabba 2 8 3 1 1 1 2 3 2 2 5 4 20 3 cccbccbadaacbbbcccab 14 17 16 3 20 17 17 20 18
output:
2 1 2 3 4 15 3
result:
ok 7 numbers
Test #2:
score: 0
Accepted
time: 826ms
memory: 13620kb
input:
8994 10 10 abaabbaabb 2 8 3 1 8 5 6 9 6 9 9 9 5 10 7 5 7 7 7 8 7 5 6 6 1 9 9 3 9 5 2 1 ab 1 2 1 8 6 bbabaabb 3 7 7 5 7 7 1 5 1 1 4 3 3 5 3 5 5 5 10 3 aababbaaab 3 4 4 6 9 8 4 6 6 7 3 babbaab 5 6 5 3 6 6 1 6 6 9 3 baaaabbba 2 5 2 8 9 8 1 4 4 9 2 babaababa 2 4 4 2 6 3 2 3 ba 1 2 2 1 2 1 2 2 2 10 2 aba...
output:
3 8 4 1 1 1 2 1 6 7 1 4 3 5 1 2 1 1 2 2 2 1 1 4 2 1 1 5 1 2 1 5 2 3 1 1 1 4 3 2 1 1 3 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 3 2 1 2 1 1 2 3 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 2 2 1 2 2 2 2 1 2 2 1 1 5 1 1 3 2 4 1 2 1 2 1 1 1 4 2 2 2 6 1 1 2 2 1 2 1 4 4 1 1 1 1 1 1 1 2 1 4 2 3 2 2 1 4 2 2 2 2 1 2 ...
result:
ok 50000 numbers
Test #3:
score: 0
Accepted
time: 409ms
memory: 13224kb
input:
3349 12 39 ccccaabbacac 7 11 10 2 11 2 2 6 4 8 10 9 4 7 4 6 12 8 5 10 10 10 12 10 1 8 8 8 10 8 1 3 3 8 10 8 7 7 7 9 11 10 2 9 6 1 2 1 10 12 10 5 6 5 2 11 2 7 11 7 4 4 4 3 4 3 11 12 11 1 7 4 3 12 6 6 8 7 6 12 11 1 9 6 5 11 5 4 5 4 3 8 8 2 9 3 6 10 8 1 3 3 1 11 11 6 11 6 8 11 11 6 9 6 3 9 4 20 2 accba...
output:
5 10 3 1 4 4 6 3 3 2 1 2 1 3 3 2 3 2 10 4 1 2 1 4 2 3 2 3 2 2 3 7 3 1 1 2 1 2 6 5 9 1 6 3 1 1 2 3 3 2 2 5 4 2 8 1 2 5 1 3 6 3 5 5 1 2 3 1 4 4 2 5 2 3 2 3 2 1 3 1 1 5 7 4 5 6 7 4 1 4 1 7 14 2 3 4 1 4 3 1 1 1 9 2 3 3 7 2 7 8 4 7 4 3 3 3 6 1 3 2 1 2 2 2 5 3 3 3 1 4 8 6 3 1 1 5 1 1 2 1 4 2 10 1 6 1 4 1 ...
result:
ok 50000 numbers
Test #4:
score: 0
Accepted
time: 88ms
memory: 13548kb
input:
333 130 121 cdbcacbdbcaccccdbabadddbccbcadbadbcdddcbdcdcaddcaccdbaaadcbdbadabddaddadccdbadbbbaabdadabcbadacaabcabdaddbdaacbccbdddddcaacbcdcdab 81 115 82 22 81 24 49 95 57 22 101 26 66 71 66 90 98 92 31 118 98 70 70 70 33 77 64 61 126 99 11 34 14 9 89 70 17 101 94 5 56 34 47 83 54 2 10 6 63 68 64 47 ...
output:
2 21 44 46 6 4 32 1 4 37 17 59 11 19 3 7 2 35 21 16 2 38 25 23 3 40 1 53 17 72 64 11 4 20 11 25 10 56 13 8 98 37 8 43 16 3 34 9 91 36 10 22 2 15 11 3 4 21 85 17 2 32 1 6 1 26 2 79 1 4 7 2 4 10 16 2 19 3 39 7 32 66 4 2 22 67 2 16 34 25 11 1 30 44 32 6 32 26 25 1 36 39 44 46 5 11 29 14 10 7 3 26 20 30...
result:
ok 50000 numbers
Test #5:
score: 0
Accepted
time: 348ms
memory: 13776kb
input:
32 1618 204 bdbcacdcecbeaabdcebdcedeacbaeadadbaaceebaeccacdadcabaaebeabdabdcbabaececbeccddbdeabcebebdcdcbbcccebdcecbbadbebbcacacecbbbaddebcaecadccdadccdddaaabaeacebbcaddacaabcbdccabedccbebbcebeeadcdecbbdaabecebeaddaadaeeecbcadeaaeeaedbddceabadcbdbcabbbedcabaacaddcbccdcdeebdadbeeeaebbbdeebbaebdbecadc...
output:
17 33 74 113 19 7 83 262 25 118 47 42 106 144 91 225 64 443 21 68 100 21 152 524 511 74 49 3 642 544 23 123 59 547 45 459 37 718 99 22 140 234 847 127 4 115 921 64 173 296 148 192 96 115 3 5 594 377 936 510 7 84 39 268 89 19 78 252 938 297 45 57 429 992 1160 23 3 47 526 609 30 30 1 1116 364 355 64 4...
result:
ok 50000 numbers
Test #6:
score: 0
Accepted
time: 3075ms
memory: 13764kb
input:
3 14322 3266 ahdmjrtnnkvotzitfqctsisxdlevjmdpocarqibnvfgpmlwkeuynawqzhnjwvtzxlckeawjwmaobieqaflnnlovlpxnjtafikqhvviqmkcyhiljgsohiqkxrpajshiighwhtglkrnbofakpqlxuwiruddxvntsvhgmwzitrtznsydvmyfhqltgtuoakvyxezwpkfwgurykrsekcaapjrvfmtljtxnvraisnvybuzveqvfglnhhqgrecvwiyzgerkwzeayvdhkfdmiwdgvxpyqajevefzzxn...
output:
3188 47 2495 4509 3896 564 619 1537 266 61 1746 148 804 1857 4589 7088 1449 344 937 4533 70 2727 62 5225 3477 341 93 121 5058 1706 1939 2786 98 342 314 4701 2938 1998 466 194 615 975 3044 432 2108 578 302 4645 3201 419 1233 345 907 4760 4944 1707 357 1297 1253 256 18 4371 3859 1029 308 860 3256 2444...
result:
ok 50000 numbers
Test #7:
score: -100
Time Limit Exceeded
input:
1 50000 50000 bbbbababaaaabababaaabaabaabbabbabbaaabbbbbbabbbababaaaaabbaaabbbababbbaabaabababababbbabaaaababaababbabbbabbbabaabbbbbaababaaaabaaaaaabaabbaaaabbaaababbbabbaabaababbbaabbbbaabbbbabbaabbabababaaabaababaabaabababbabbbabaaabaaabaabbaabbaabababaaaaabaabbabbbaaaabbbabbabbbaaabaaaabbbbaabbaa...