QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#690#442244#8079. Range Periodicity QueryN_z_rotcar07Failed.2024-06-15 12:48:022024-06-15 12:48:02

详细

Extra Test:

Accepted
time: 271ms
memory: 107420kb

input:

500000
wvzreexvwcrybuduegmiqtwqauzbvmjbtqpxkxpskqpfkqodgazczukgvedmwbemrasdzgfywdhdxrfrpbxpyxqxkpdynwfdjuqevavgjgbqbqpdnnljqbritzumsbtemiedgzeovpmwaxsisjdidoafklwuzcomelzkxngtmqjdlwvxurscpewseeckmnmxdyvnjbufcousmwvzzppeoahoibexwkrfcvhsgoezbtwheimrhvggbimuiszczqkeqljibqrqsnxqcsxmlfcqzahxgwdnsagwtkjng...

output:

1
2
3
4
5
6
7
8
8
10
11
12
13
14
15
16
17
18
19
20
21
22
22
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
60
62
63
64
65
66
67
68
69
70
71
72
72
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
93
95
96
97
98
99
100
101
102
...

result:

ok 500000 lines

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#442244#8079. Range Periodicity Queryrotcar07WA 831ms94196kbC++142.3kb2024-06-15 10:30:492024-06-15 15:03:21

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=5e5+5;
int n,m,q,L[maxn],R[maxn],ans[maxn];
vector<int> Q[maxn],P[maxn],E[maxn];
constexpr int inf=1e9;
int t[maxn<<2];
#define ls p<<1
#define rs p<<1|1
inline void pushup(int p){t[p]=min(t[ls],t[rs]);}
void build(int p,int l,int r){
    t[p]=inf;
    if(l==r) return;
    int mid=l+r>>1;
    build(ls,l,mid),build(rs,mid+1,r);
}
void modify(int p,int l,int r,int k,int x){
    if(l==r) return t[p]=x,void();
    int mid=l+r>>1;
    if(k<=mid) modify(ls,l,mid,k,x);
    else modify(rs,mid+1,r,k,x);
    pushup(p);
}
int query(int p,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr) return t[p];
    int mid=l+r>>1,res=inf;
    if(ql<=mid) res=query(ls,l,mid,ql,qr);
    if(qr>mid) res=min(res,query(rs,mid+1,r,ql,qr));
    return res;
}
typedef unsigned long long ull;
constexpr ull mod=998244853;
const ull bas=random_device{}()%10000+200;
ull bs[maxn],hsh[maxn];
inline ull gethsh(int l,int r){
    return (hsh[r]+mod-hsh[l-1]*bs[r-l+1]%mod)%mod;
}
string s,r,rv;int dt[maxn];
int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // string name="suffix";freopen((name+".in").c_str(),"r",stdin);freopen((name+".out").c_str(),"w",stdout);
    cin>>n>>s>>m;for(int i=bs[0]=1;i<=n;i++) bs[i]=bs[i-1]*bas%mod;
    for(int i=1,x;i<=m;i++) cin>>x,P[x].push_back(i);cin>>q;
    for(int i=1,x;i<=q;i++) cin>>x>>L[i]>>R[i],Q[x].push_back(i);
    for(int i=1;i<n;i++) if(s[i]>='a'&&s[i]<='z') dt[1]++;
    for(int i=1;i<n;i++) dt[i+1]=dt[i]-(s[i]>='a'&&s[i]<='z');
    for(auto x:s) if(x>='a'&&x<='z') rv+=x;else r+=x+'a'-'A';
    reverse(rv.begin(),rv.end());r=rv+r;
    for(int i=0;i<n;i++) hsh[i+1]=(hsh[i]*bas+r[i])%mod;
    for(int i=1;i<=n;i++){
        int l=i,r=n;
        while(l<r){
            int mid=l+r+1>>1,d=dt[mid],L=mid-i;
            if(gethsh(d+1,d+L)==gethsh(d+mid-L+1,d+mid)) l=mid;
            else r=mid-1;
        }E[l+1].push_back(i);
        // cerr<<i<<" "<<l<<'\n';
    }build(1,1,m);
    for(int i=1;i<=n;i++){
        for(auto x:P[i]) modify(1,1,m,x,i);
        for(auto x:E[i])for(auto y:P[x]) modify(1,1,m,y,inf);
        for(auto x:Q[i]) ans[x]=query(1,1,m,L[x],R[x]);
    }
    for(int i=1;i<=q;i++) cout<<(ans[i]==inf?-1:ans[i])<<'\n';
}