QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#724283 | #8079. Range Periodicity Query | juan_123 | RE | 0ms | 0kb | C++14 | 3.7kb | 2024-11-08 11:40:14 | 2024-11-08 11:40:14 |
answer
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3)
#define double long double
#define lowbit(x) (x&(-x))
int n,m,q;
int sa[1000005],tp[1000005],rk[1000005],a[1000005],h[1000005];
char s[500005],t[500005];
int ll,rr;
int l[500005],r[500005];
int st[500005][20];
int lg[500005],ppos[500005];
int p[500005];
vector<int>pos[500005],qq[500005],del[500005];
int ql[500005],qr[500005],qk[500005],ans[500005];
struct node{
int l,r,mn;
void add(int x){mn=x;}
}tree[2000005];
void build(int l,int r,int k){
tree[k].l = l,tree[k].r = r;tree[k].mn = n+5;
if(l+1 == r)return;
build(l,l+r>>1,k<<1),build(l+r>>1,r,k<<1|1);
}
void update(int pos,int add,int k){
int l = tree[k].l,r = tree[k].r;
if(l+1 == r){return tree[k].add(add);}
int mid = l+r>>1;
if(pos<mid)update(pos,add,k<<1);else update(pos,add,k<<1|1);
tree[k].mn = min(tree[k<<1].mn,tree[k<<1|1].mn);
}
int query(int l,int r,int k){
int ll = tree[k].l,rr = tree[k].r;
if(ll>=r or l>=rr)return n+5;
if(l<=ll and rr<=r)return tree[k].mn;
return min(query(l,r,k<<1),query(l,r,k<<1|1));
}
void qsort(){
for(int i = 1;i<=max(n,256);i++)a[i] = 0;
for(int i = 1;i<=n;i++)a[rk[i]]++;
for(int i = 1;i<=max(n,256);i++)a[i]+=a[i-1];
for(int i = n;i>=1;i--)sa[a[rk[tp[i]]]--] = tp[i];
}
void SA(){
for(int k = 1;k<=n;k<<=1){
int tot = 0;
for(int i = n-k+1;i<=n;i++)tp[++tot] = i;
for(int i = 1;i<=n;i++)if(sa[i]>k)tp[++tot] = sa[i]-k;
qsort();swap(rk,tp);
rk[sa[1]] = 1;
for(int i = 2;i<=n;i++){
rk[sa[i]] = rk[sa[i-1]]+1-(tp[sa[i]] == tp[sa[i-1]] and tp[sa[i]+k] == tp[sa[i-1]+k]);
}
}
}
void get_height(){
for(int i = 1;i<=n;i++)rk[sa[i]] = i;
int l = 0;
for(int i = 1;i<=n;i++){
if(l)--l;
if(rk[i] == 1)continue;
int j = sa[rk[i]-1];
while(j+l<=n and i+l<=n and s[i+l] == s[j+l])++l;
h[rk[i]] = l;
}
for(int i = 2;i<=n;i++)st[i][0] = h[i];
for(int j = 1;j<20;j++){
for(int i = 1;i+(1<<j)-1<=n;i++){
st[i][j] = min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
}
}
int lcp(int x,int y){
x = rk[x],y = rk[y];
if(x>y)swap(x,y);
int k = lg[y-x];
return min(st[x+1][k],st[y-(1<<k)+1][k]);
}
bool check(int l,int r,int p){
if(r-l+1 == p)return 1;
return (lcp(l,l+p)>=(r-(l+p)+1));
}
signed main(){
// system("fc period.out ex_period2.out");
freopen("period.in","r",stdin);
freopen("period.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int tt =0 ;
cin >> n;
lg[1] = 0;for(int i = 2;i<=n;i++)lg[i] = lg[i>>1]+1;
for(int i = 1;i<=n;i++)cin >> t[i];
cin >> m;
for(int i = 1;i<=m;i++)cin >> p[i];
cin >> q;//q=20;
for(int i = 1;i<=q;i++)cin >> qk[i] >> ql[i] >> qr[i];
for(int i = 1;i<=n;i++)if('A'<=t[i] and t[i]<='Z')++tt;
ll = tt+1,rr = tt;
for(int i = 1;i<=n;i++){
if('A'<=t[i] and t[i]<='Z'){
s[--ll] = t[i]-'A'+'a';
}else{
s[++rr] = t[i];
}
l[i] = ll,r[i] = rr;
}
for(int i = 1;i<=n;i++)rk[i] = s[i],tp[i] = i ;
qsort();SA();get_height();
for(int i = 1;i<=m;i++)pos[p[i]].push_back(i);
for(int i = 1;i<=m;i++)qq[qk[i]].push_back(i);
build(1,m+1,1);
for(int i = 1;i<=n;i++){
int ll = i,rr = n;
while(ll<rr){
int mid = ll+rr+1>>1;
if(check(l[mid],r[mid],i))ll = mid;
else rr = mid-1;
}
ppos[i] = ll;
del[ll+1].push_back(i);
}
for(int i = 1;i<=n;i++){
for(auto x:pos[i]){update(x,i,1);}
for(auto l:del[i])for(auto x:pos[l])update(x,n+5,1);
for(auto id:qq[i]){
int x = query(ql[id],qr[id]+1,1);
if(x == n+5)ans[id] = -1;
else ans[id] = x;
}
}
for(int i = 1;i<=q;i++){
cout << ans[i] << '\n';
}
return 0;
}/*
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
*/
詳細信息
Test #1:
score: 0
Dangerous Syscalls
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