QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#787170#8079. Range Periodicity QueryharlemTL 682ms53180kbC++204.3kb2024-11-27 10:13:052024-11-27 10:13:08

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 10:13:08]
  • 评测
  • 测评结果:TL
  • 用时:682ms
  • 内存:53180kb
  • [2024-11-27 10:13:05]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef double db;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
template <typename Type>
using vec=vector<Type>;
template <typename Type>
using grheap=priority_queue<Type>;
template <typename Type>
using lrheap=priority_queue<Type,vector<Type>,greater<Type> >;
#define fir first
#define sec second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)

template<typename type>
inline void read(type &x)
{
    x=0;static bool flag(0);char ch=getchar();
    while(!isdigit(ch)) flag=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    flag?x=-x:0;
}

template<typename type>
inline void write(type x,bool mode=1)
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10; while(x);
    while(top) putchar(Stack[top--]|48);
    mode?putchar('\n'):putchar(' ');
}

const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7/*998244353*/;
const ll bas=131;
const ull bas2=13331;

const int N=5e5+5;

int n,m,q;
char c[N];
string s;
int ls[N],rs[N];
int p[N],ts[N],tp[N];
vec<int> plc[N];
int arr[N];
struct query{
    int k,l,r,id;
};
vec<query> ned[N];
int ans[N];

ll hsh[N],cf[N];
ull hsh2[N],cf2[N];
inline ll gethash(int l,int r){
    return (hsh[r]-hsh[l-1]*cf[r-l+1]%mod+mod)%mod;
}
inline ll gethash2(int l,int r){
    return hsh2[r]-hsh2[l-1]*cf2[r-l+1];
}

struct segment{
    int dat[N<<2];
    void update(int x){
        dat[x]=min(dat[x<<1],dat[x<<1|1]);
    }
    void build(int x=1,int l=1,int r=m){
        dat[x]=inf;
        if(l==r)return;
        int m=l+r>>1;
        build(x<<1,l,m);
        build(x<<1|1,m+1,r);
        update(x);
    }
    void chg(int k,int v,int x=1,int l=1,int r=m){
        if(l==r){
            dat[x]=v;
            return;
        }
        int m=l+r>>1;
        if(k<=m)chg(k,v,x<<1,l,m);
        else chg(k,v,x<<1|1,m+1,r);
        update(x);
    }
    int query(int lq,int rq,int x=1,int l=1,int r=m){
        if(lq<=l&&r<=rq){
            return dat[x];
        }
        int m=l+r>>1,res=inf;
        if(lq<=m)chmin(res,query(lq,rq,x<<1,l,m));
        if(m<rq)chmin(res,query(lq,rq,x<<1|1,m+1,r));
        return res;
    }
}seg;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    read(n);
    int fst;
    rep(i,1,n){
        c[i]=getchar();
        if(i==1){
            fst=1;
            if(c[i]<'a')s=c[i]-'A'+'a';
            else s=c[i];
        }else{
            if('a'<=c[i]){
                s=c[i]+s;
                fst++;
            }else s=s+char(c[i]-'A'+'a');
        }
    }
    ls[1]=rs[1]=fst;
    rep(i,2,n){
        ls[i]=ls[i-1];rs[i]=rs[i-1];
        if('a'<=c[i])ls[i]--;
        else rs[i]++;
    }
    s=' '+s;
    cf[0]=cf2[0]=1;
    rep(i,1,n){
        hsh[i]=(hsh[i-1]*bas%mod+s[i])%mod;
        cf[i]=cf[i-1]*bas%mod;
        hsh2[i]=hsh2[i-1]*bas2;
        cf2[i]=cf2[i-1]*bas2;
    }
    rep(i,1,n){
        arr[i]=i;
        int l=i,r=n;
        while(l<r){
            int m=l+r+1>>1;
            int len=m-i;
            if(gethash(ls[m],ls[m]+len-1)==gethash(rs[m]-len+1,rs[m])&&
            gethash2(ls[m],ls[m]+len-1)==gethash2(rs[m]-len+1,rs[m]))l=m;
            else r=m-1;
        }
        ts[i]=l;
    }
    sort(arr+1,arr+1+n,[](int a,int b){return ts[a]<ts[b];});
    read(m);
    rep(i,1,m){
        read(p[i]);
        plc[p[i]].pub(i);
    }
    read(q);
    query tq;
    rep(i,1,q){
        read(tq.k);read(tq.l);read(tq.r);
        tq.id=i;
        ned[tq.k].pub(tq);
    }
    seg.build();
    int j=n;
    per(i,n,1){
        while(j>=1&&ts[arr[j]]>=i){
            for(auto p:plc[arr[j]])seg.chg(p,arr[j]);
            j--;
        }
        for(auto q:ned[i]){
            int res=seg.query(q.l,q.r);
            if(res>i)res=-1;
            ans[q.id]=res;
        }
    }
    rep(i,1,q)write(ans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 22024kb

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: 682ms
memory: 51708kb

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: 190ms
memory: 37500kb

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: 206ms
memory: 39736kb

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: 235ms
memory: 42020kb

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: 473ms
memory: 53180kb

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: -100
Time Limit Exceeded

input:

500000
AaAAaaAaaaAaaaaaAaAAAaaaaAAaAAAaaAAAaAaaaaAaAaaaAaAaAAaAAaAaaAaaAaAAAAAAAAAAAAaAaAAAaAaAAAAAaaaAaAAAaAaaaAaaAaaaaaaAaaaaAaAaaAAaAAaaAAAaAaaaaaaaAaAaAaAaaAAaaaAAaAaaAAAaaaaaaaAAaAAaAaaaaaAAaAaAaaAAaaaAAaaaAaAAaaaAaAaaAAAaaAAAaAaaaaaaaaaAaAaAaAAAaaAAAAaAaAAAAAAaAAAaAaaaaaaAAaAaaAAaAAAaaaAaAAaAA...

output:


result: