QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#443308 | #8079. Range Periodicity Query | wangzhifang | WA | 661ms | 118548kb | C++14 | 5.7kb | 2024-06-15 15:06:46 | 2024-06-15 15:06:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
namespace sais{
template<int v,typename T>void arrset(T*const arr,const int len){
std::memset(arr,v,len*sizeof(T));
}
template<typename T>void arrcpy(T*const arr1,const T*const arr2,const int len){
std::memcpy(arr1,arr2,len*sizeof(T));
}
constexpr int max_n=1000000,max_char=256;
int num[max_n+1],cur[max_n+1];
void sais(int*a,const int n,const int k,int*sa){
#define pus(pos) sa[cur[a[pos]]--]=pos
#define pul(pos) sa[++cur[a[pos]]]=pos
#define induce_sort(lmspos)\
arrset<-1>(sa+1,n),arrcpy(cur,sum,k+1);\
for(const int*i=lmspos+m; i>lmspos; --i)\
pus(*i);\
arrcpy(cur+1,sum,k);\
for(const int*i=sa; ++i<=fin; *i>1&&!tp[*i-1]&&(pul(*i-1)));\
arrcpy(cur,sum,k+1);\
for(const int*i=sa+n; i>sa; --i)\
if(*i>1&&tp[*i-1])\
pus(*i-1);
bool*const tp=(new bool[n])-1;
tp[n]=1;
for(int i=n; --i; tp[i]=(a[i]==a[i+1])?tp[i+1]:(a[i]<a[i+1]));
int*const lms=new int[(n+4)>>1],m=0;
num[1]=-1;
for(int i=2; i<=n; ++i)
num[i]=(tp[i]&&!tp[i-1])?(lms[++m]=i,m):-1;
int*const sum=new int[k+1];
arrset<0>(sum,k+1);
for(const int*i=a+n; i>a; --i)
++sum[*i];
for(int*i=sum,*const ed=sum+k,now=*sum; ++i<=ed; now=*i+=now);
const int*const fin=sa+n;
induce_sort(lms);
lms[m+1]=n;
int*const a1=new int[m+1],k1=0;
for(int i=1,x,y; i<=n; ++i)
if(~(x=num[sa[i]])){
if(!k1||lms[x+1]-lms[x]!=lms[y+1]-lms[y])
a1[y=x]=++k1;
else{
for(int p1=lms[x],p2=lms[y],ed=lms[x+1]; p1<ed; ++p1,++p2)
if(a[p1]!=a[p2]||tp[p1]!=tp[p2]){
++k1;
break;
}
a1[y=x]=k1;
}
}
if(k1==m)
for(int i=1; i<=m; ++i)
sa[a1[i]]=i;
else
sais(a1,m,k1,sa);
for(int i=1; i<=m; ++i)
a1[i]=lms[sa[i]];
induce_sort(a1);
delete[] (tp+1),
delete[] lms,
delete[] sum,
delete[] a1;
#undef pus
#undef pul
#undef induce_sort
}
char s[max_n+2];
int a[max_n+2];
int sa[max_n+2];
int ton[max_char+1];
template <typename T,typename vT> inline void maxi(T&x,vT val){
(val>x)&&(x=val);
}
template <typename T,typename vT> inline void mini(T&x,vT val){
(val<x)&&(x=val);
}
int rk[max_n+2];
int height[max_n+2];
template <typename T> void write(T x){
x>9&&(write(x/10),0);
putchar(x%10^'0');
}
int st[max_n+2][20];
void main(int n){
int minc=max_char,maxc=0;
memset(ton,0,sizeof(ton));
for(int i=1; i<=n; ++i)
ton[int(s[i])]=1,maxi(maxc,s[i]),mini(minc,s[i]);
++n;
ton[minc-1]=1;
int*edd=ton+maxc;
for(int*i=ton+minc; i<=edd; ++i)
*i+=*(i-1);
for(int i=1; i<n; ++i)
a[i]=ton[int(s[i])];
a[n]=1;
sais(a,n,ton[maxc],sa);
for(int i=1; i<=n; ++i)
rk[sa[i]]=i;
int x=0;
for(int i=1,j; i<=n; ++i){
for(j=sa[rk[i]-1],x&&--x; a[i+x]==a[j+x]; ++x);
height[rk[i]-1]=x;
}
for(int i=n; --i; )
for(int now=st[i][0]=height[i],j=0,d=1; j+d*2<=n; st[i][++j]=now,d<<=1)
mini(now,st[i+d][j]);
}
int rmq(int l,int r){
const int d=31^__builtin_clz(r-l);
return min(st[l][d],st[r-(1<<d)][d]);
}
int lcp(int a,int b){
// fprintf(stderr,"%d %d\n",a,b);
a=rk[a],b=rk[b];
const int res=a<b?rmq(a,b):rmq(b,a);
// fprintf(stderr,"%d\n",res);
return res;
}
}
using namespace std;
typedef pair<int,int> pii;
constexpr int max_n=500000,max_m=500000,max_q=500000;
pii pos[max_n+1];
vector<int>dque[max_n+1];
int tr[max_m<<1];
int p[max_m+1];
void build(const int now,const int lft,const int rgt){
if(lft==rgt-1){
tr[now]=p[lft];
return;
}
const int mid=(lft+rgt)>>1;
const int lsn=mid<<1;
const int rsn=lsn|1;
build(lsn,lft,mid);
build(rsn,mid,rgt);
tr[now]=min(tr[lsn],tr[rsn]);
}
void update(const int now,const int lft,const int rgt,const int p){
if(lft==rgt-1){
tr[now]=max_n;
return;
}
const int mid=(lft+rgt)>>1;
const int lsn=mid<<1;
const int rsn=lsn|1;
if(p<mid)
update(lsn,lft,mid,p);
else
update(rsn,mid,rgt,p);
tr[now]=min(tr[lsn],tr[rsn]);
}
int query(const int now,const int lft,const int rgt,const int l,const int r){
// fprintf(stderr,"%d %d %d %d\n",lft,rgt,l,r);
if(l<=lft&&rgt<=r)
return tr[now];
const int mid=(lft+rgt)>>1;
const int lsn=mid<<1;
const int rsn=lsn|1;
if(r<=mid)
return query(lsn,lft,mid,l,r);
if(l>=mid)
return query(rsn,mid,rgt,l,r);
return min(query(lsn,lft,mid,l,r),query(rsn,mid,rgt,l,r));
}
int ans[max_q+1];
vector<pair<pii,int> >que[max_n+1];
char opstr[max_n+1];
int main(){
int n,m;
scanf("%d\n%s",&n,opstr);
// fprintf(stderr,"n: %d\n",n);
scanf("%d",&m);
for(int i=0; i<m; ++i)
scanf("%d",p+i);
int q;
scanf("%d",&q);
for(int i=0,k,l,r; i<q; ++i){
scanf("%d%d%d",&k,&l,&r);
que[k].emplace_back(pii(l-1,r),i);
}
int l=1;
for(int i=0; i<n; ++i)
l+=bool(islower(opstr[i]));
int r=l-1;
for(int i=0; i<n; ++i){
if(isupper(opstr[i]))
sais::s[++r]=tolower(opstr[i]);
else
sais::s[--l]=opstr[i];
pos[i+1]=pii(l,r);
// fprintf(stderr,"%d: %d %d\n",i,l,r);
}
// for(int i=1; i<=n; ++i)
// fprintf(stderr,"%c",sais::s[i]);
// fprintf(stderr,"\n");
sais::main(n);
for(int i=0; i<m; ++i){
const int dif=p[i];
int l=dif,r=n+1;
while(l<r-1){
const int mid=(l+r)/2;
if(sais::lcp(pos[mid].first,pos[mid].first+dif)>=mid-dif)
l=mid;
else
r=mid;
}
// fprintf(stderr,"%d: %d %d\n",i,p[i],l);
dque[l].emplace_back(i);
}
build(1,0,m);
for(int i=1; i<=n; ++i){
for(const pair<pii,int>&quer:que[i]){
const int l=quer.first.first,r=quer.first.second;
ans[quer.second]=query(1,0,m,l,r);
if(ans[quer.second]>i)
ans[quer.second]=-1;
}
for(const int&p:dque[i])
update(1,0,m,p);
}
for(int i=0; i<q; ++i)
printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 48308kb
input:
7 AABAAba 9 4 3 2 1 7 5 3 6 1 6 1 4 4 2 1 4 2 1 3 3 3 5 5 4 7 7 8 9
output:
1 1 2 -1 3 6
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 205ms
memory: 82368kb
input:
200000 BAbBbBabBBbbABbbaBbaaabaBBAbBbBAAAAABBaBaAAabBAAbABaaBABAabAAAbabbAaBABAbabbAAAbbbbabBBAbbBaabBAAAbBBBbBbbAbbbBabbBABaBAaAAAbBbaABabBAbAAbBbbAbAbBaabAbBBbaaaaBaBbbABBBaaabBaBABAbBabBbbAABBbaBAbaBAbAAABABAbaabbaAAaBAbAbAbBBbaaaAaBaaABBbBAAaAAAaaABbbaAbAaBbaAaaababbaBbaAAAAAAabbBaAabbbaBBAAaABb...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 61006 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 500000 lines
Test #3:
score: 0
Accepted
time: 298ms
memory: 58864kb
input:
10 baaAaAAaAA 500000 6 8 2 3 1 8 7 3 9 4 1 6 9 4 10 10 4 3 1 7 4 3 9 7 1 2 9 3 3 1 10 8 1 6 4 1 6 10 1 5 1 8 9 9 7 3 6 3 9 1 7 6 7 7 9 10 3 2 4 10 7 3 7 1 5 3 5 1 10 1 3 2 2 4 2 3 4 10 5 2 7 10 5 6 8 9 10 6 9 7 5 4 5 4 4 2 5 8 1 9 1 2 10 8 2 5 6 6 6 4 3 1 2 2 3 5 7 4 5 7 5 8 1 8 9 7 6 3 10 7 5 4 8 8...
output:
5 4 4 2 6 3 3 4 6 3 1 6 4 5 3 5 2 5 4 4 2 5 3 5 5 1 2 5 3 5 4 3 5 6 4 5 4 6 4 6 4 1 5 4 4 3 5 3 3 4 5 5 5 4 1 6 5 5 4 4 2 4 3 5 4 5 1 5 1 1 4 4 5 4 4 3 3 4 2 4 4 4 2 4 6 5 2 5 4 3 4 2 4 5 5 4 6 1 2 6 4 5 6 1 1 3 2 3 1 4 4 3 4 4 4 3 6 5 4 5 5 4 1 2 3 4 5 4 4 4 3 6 4 4 4 2 3 3 3 3 3 6 3 5 3 4 6 3 4 5 ...
result:
ok 500000 lines
Test #4:
score: 0
Accepted
time: 336ms
memory: 59940kb
input:
500 ababbBbBabaaBAbabBbbBBAAABabBbBAAABbaBbBAAbabaBaAAaabAaABBBabababAAbaaAbbAAabAAbBbaabbBbaAAABaAaBbbBbabBAABBaabbAabbBabbbAbABaBAABaBbAaaBABBbBAAbbbBabbABABAaAaAAAbaAabBbBaaaaAAAAAabaBBAAABAbbabAaBAbAaaBBbABbBBbaaAaAaBBbaBbabBbBABbaaBbAaabBABaBBbAAaaBABBAaaABAbbaaAaBaAAbAbbbbbaabBabaBbaabaAbaBaaa...
output:
386 327 309 141 424 175 186 273 45 498 99 262 478 149 424 444 49 267 233 388 359 310 203 81 498 12 97 295 400 351 352 407 310 471 291 479 448 203 267 60 223 458 421 391 5 470 212 253 99 281 167 451 154 86 299 434 370 255 383 207 258 310 487 380 6 368 235 137 334 141 50 128 29 478 448 223 466 345 407...
result:
ok 500000 lines
Test #5:
score: 0
Accepted
time: 392ms
memory: 61192kb
input:
10000 BaBbAAaaaaBAbbbbbaBbaaAbaaaabAaaaAAbabBAbaaBABaaabaAbBBaBBABAbabBAbaaAAaAABABbbbABBaBBaABbbAAbBabaAbaBBaAbabaaAAAabAbAABAabBbBaBaAaAbbBAAABbbabAaABABaBbaAABBbbBAABbbbAaABaAaaABAbbbABAabbaAaaBbbbBaBBbAaaabbaBbaaAbabBabaBaAAAbBAabbBAbabAAbbBBBbBAAaBBbBBAbaaaAbBaaBAAbbaAbbbBAbaAaaAbBBAaBabBaaaBab...
output:
-1 5219 4322 2614 7302 1876 -1 5584 2861 3586 4821 6579 6706 1605 7878 886 9218 293 167 7298 5146 6860 2921 8263 4330 9578 7472 6086 5537 4890 8285 58 9733 -1 3157 262 9533 6943 8285 2837 451 6494 7918 8912 2187 9832 4487 2077 871 210 951 1761 6892 4304 6634 9572 9544 5744 4015 7418 7804 5928 3611 8...
result:
ok 500000 lines
Test #6:
score: 0
Accepted
time: 480ms
memory: 74952kb
input:
100000 aabAbBbaBAaabbbbbaAAABaaabbBaBAAaBabbBAbBbbBbbbaaaABaaBaBbBABBBbabBAABbabbAaaaBBaAAbABaBABAABbBAbBAAAbaBaabbAAABaBAaaaBBbBbaBabAbBBaaabaaaaBbBaAaAbAbbBaABaabBbBaAAaAaaBbbAbbaaBBbbbaAaAabaBaAaaBaAAbbBabBaBAbAaabAbbbAbaAbBbaABABAaBBABAaABBBBABAaBAbbbaBbaAABBaAabaAbaAaabAAAbbbaBBbBaaaaAaaAABbBaa...
output:
35335 42708 80231 -1 52892 27828 25395 21105 26112 55093 16568 16170 -1 73256 -1 82801 58592 52120 48659 -1 -1 -1 92581 -1 67746 9463 50384 69443 71368 -1 62536 83524 71293 88216 83685 45630 5450 969 3140 19286 79236 80564 33058 44088 24142 -1 40385 68116 -1 20399 78247 52636 37514 -1 54565 44272 75...
result:
ok 500000 lines
Test #7:
score: 0
Accepted
time: 474ms
memory: 116252kb
input:
500000 AaAAaaAaaaAaaaaaAaAAAaaaaAAaAAAaaAAAaAaaaaAaAaaaAaAaAAaAAaAaaAaaAaAAAAAAAAAAAAaAaAAAaAaAAAAAaaaAaAAAaAaaaAaaAaaaaaaAaaaaAaAaaAAaAAaaAAAaAaaaaaaaAaAaAaAaaAAaaaAAaAaaAAAaaaaaaaAAaAAaAaaaaaAAaAaAaaAAaaaAAaaaAaAAaaaAaAaaAAAaaAAAaAaaaaaaaaaAaAaAaAAAaaAAAAaAaAAAAAAaAAAaAaaaaaaAAaAaaAAaAAAaaaAaAAaAA...
output:
3 13 3 4 3 3 6 3 6 131 3 3 6 4 33 5 9 3 195 105 77 4 3 3 3 3 3 4 3 3 4 3 4 3 3 3 3 4 3 3 4 4 4 4 4 3 9 3 3 23 33 3 4 3 3 3 3 4 4 3 4 4 4 3 5 1 3 5 3 74 3 23 5 3 3 4 3 3 3 3 3 6 4 3 4 3 4 4 3 4 3 3 4 7 4 3 3 4 3 13 3 4 1 6 3 5 3 3 4 4 20 4 532 4 3 3 3 6 97 4 6 3 3 4 3 4 6 3 3 3 3 3 3 4 7 3 6 4 4 4 3 ...
result:
ok 500000 lines
Test #8:
score: -100
Wrong Answer
time: 661ms
memory: 118548kb
input:
500000 BbBabaaAABbABbaAABaaAabBBABbBBBAbaAbbABAaBbbAAabAaBaabBbaABAbaAbBabbaaaaaaaaBBbbBabaaAAbaAABaAAAaAaAbbbaaAaaAaaABAAAAAbbbABaBBbBAAaAAaBbABbBaaBabaAAaBAABaAaaBBbaBaBaBaaAbBAbAaABbBaaAAAAAabBAABaaAbbBaBAAbBBaBaabBaBBAbAbaaaaAbBbaAbbaAaABBaaAbAaaBABABBaAbaBbAAbaAAbaBAbAAaabBbAaabABAaBBBAbBbbBABa...
output:
-1 125970 -1 -1 -1 435323 -1 425031 252960 236797 -1 -1 -1 334816 -1 -1 319448 234360 344601 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 38745 -1 379427 -1 325294 -1 -1 -1 365248 387079 -1 492283 346128 -1 -1 -1 356064 -1 -1 321398 -1 -1 13515 -1 338767 461122 -1 436442 -1 309126 -1 207537 -1 -1 -1 -1 381656 1079...
result:
wrong answer 357341st lines differ - expected: '-1', found: '500000'