QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#844619#1942. Another Substring Query ProblemyanshanjiahongAC ✓123ms131896kbC++143.1kb2025-01-06 09:08:482025-01-06 09:08:49

Judging History

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

  • [2025-01-06 09:08:49]
  • 评测
  • 测评结果:AC
  • 用时:123ms
  • 内存:131896kb
  • [2025-01-06 09:08:48]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define pii pair<int,int>
#define mp make_pair
#define fir first
#define sec second
#define ls(x) lson[x]
#define rs(x) rson[x]
#define lowbit(i) (i&-i)
using namespace std;
typedef long long ll;
const int N=2e5+5,M=2e6+5,mo=998244353,inf=(ll)1e18+7;
const double PI=acos(-1);
void read(int &a){
    int x=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    a=x*w;
}
int n,m,q[N];
vector<int>e[N];
struct seg{
    int t[N*32],lson[N*32],rson[N*32],rt[N],cntp=0;
    void pushup(int x){
        t[x]=t[ls(x)]+t[rs(x)];
    }
    void modify(int &x,int le,int ri,int p){
        if(!x)x=++cntp,t[x]=ls(x)=rs(x)=0;
        if(le==ri){
            t[x]++;
            return;
        }
        int mid=(le+ri)>>1;
        if(p<=mid)modify(ls(x),le,mid,p);
        else modify(rs(x),mid+1,ri,p);
        pushup(x);
    }
    int query(int x,int le,int ri,int v){
        if(t[x]<v)return -1;
        if(le==ri)return le;
        int mid=(le+ri)>>1;
        if(t[ls(x)]>=v)return query(ls(x),le,mid,v);
        else return query(rs(x),mid+1,ri,v-t[ls(x)]);
    }
    int merge(int p,int q,int le,int ri){
        if(!p)return q;
        if(!q)return p;
        if(le==ri){
            t[p]+=t[q];
            return p;
        }
        int mid=(le+ri)>>1;
        ls(p)=merge(ls(p),ls(q),le,mid),rs(p)=merge(rs(p),rs(q),mid+1,ri);
        pushup(p);
        return p;
    }
}T;
struct ACAM{
    int ch[N][26],fa[N],cntp;
    int insert(string s){
        int p=0;
        for(auto j:s){
            int v=j-'a';
            if(!ch[p][v])ch[p][v]=++cntp;
            p=ch[p][v];
        }
        return p;
    }
    void add(int f,int x){
        e[f].push_back(x),fa[x]=f;
    }
    void build(){
        queue<int>q;
        rep(i,0,25)
            if(ch[0][i])add(0,ch[0][i]),q.push(ch[0][i]);
        while(!q.empty()){
            int x=q.front();
            q.pop();
            rep(i,0,25){
                if(!ch[x][i]){
                    ch[x][i]=ch[fa[x]][i];
                    continue;
                }
                add(ch[fa[x]][i],ch[x][i]),q.push(ch[x][i]);
            }
        }
    }
    int nwp=0;
    void addc(int x){
        nwp=ch[nwp][x];
    }
}A;
string s,t[N];
int pos[N];
vector<int>vq[N];
int ans[N];
void dfs(int x){
    for(auto j:e[x])
        dfs(j),T.rt[x]=T.merge(T.rt[x],T.rt[j],1,n);
    for(auto j:vq[x])
        ans[j]=T.query(T.rt[x],1,n,q[j]);
}
signed main(){
    cin>>s,n=s.size();
    read(m);
    rep(i,1,m){
        cin>>t[i],read(q[i]);
        pos[i]=A.insert(t[i]),vq[pos[i]].push_back(i);
    }
    A.build();
    rep(i,1,n)
        A.addc(s[i-1]-'a'),T.modify(T.rt[A.nwp],1,n,i);
    dfs(0);
    rep(i,1,m){
        if(ans[i]==-1)puts("-1");
        else printf("%d\n",ans[i]-t[i].size()+1);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 19ms
memory: 33560kb

input:

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

output:

50997
112451
97534
13355
150562
5772
146250
10904
4343
194777
71395
96501
6809
18571
166901
114789
123587
176339
29660
82650
63509
80101
47907
66143
199971
108032
140518
49221
46212
28127
61461
149025
142818
33646
17132
196161
36671
28656
16860
141951
139429
72866
162807
21343
194803
16263
118701
20...

result:

ok 631 lines

Test #2:

score: 0
Accepted
time: 109ms
memory: 73592kb

input:

gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...

output:

-1
-1
174057
-1
141370
-1
75407
-1
112842
-1
-1
4510
100584
12223
35161
28768
-1
5258
151196
-1
25061
87769
-1
135272
-1
-1
-1
159851
-1
-1
121971
108762
60263
143662
-1
5200
65515
-1
-1
-1
-1
157064
65499
-1
84122
173745
171855
-1
193921
-1
121426
162134
137032
126244
99210
155648
95498
151755
1105...

result:

ok 54751 lines

Test #3:

score: 0
Accepted
time: 50ms
memory: 45712kb

input:

gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...

output:

-1
-1
-1
66435
197872
-1
83779
-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
-1
91584
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
21808
-1
-1
-1
-1
-1
15575
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
196132
-...

result:

ok 200000 lines

Test #4:

score: 0
Accepted
time: 123ms
memory: 75792kb

input:

qzpazeynjuulxowssvbghqhdtywsnzgwmpospvxyeoxsrwxccwucqgsyllrkburhfrivzqcpnevmixivrtbxexhrjauxayxmrdluzkvxnfvocgoceavurwlxzdfepcxzcdaoppzfhqpyhmjlhbfsntaewlfjqgefvqttczahgzgucgoohnjsdvyaxltixdvbgzoewptjuosymanxeiewbvnibhjbkhoebygmtmwldtvyijasjquxvtwkhlngbcremdsprdetxkggogyjbgsuduzmutcgblzoqopwspbzrurs...

output:

109498
154508
-1
-1
-1
-1
1028
36133
-1
-1
-1
-1
-1
191213
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
129366
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
196816
-1
-1
141983
-1
-1
137732
-1
11350
-1
-1
-1
25285
52189
-1
-1
-1
-1
80999
-1
-1
17773
5870
-1
-1
-1
-1
-1
128813
-1
1492
-1
-1
1513
-1
-1
-1
139760
199034
-1
-1
49122
...

result:

ok 50034 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 29340kb

input:

dkfjahfwbaofboahsdlvkjnalsf
10
dkfjahfwbaofboahsdlvkjnalsfd 1
adkfjahfwbaofboahsdlvkjnalsf 1
kfj 2
kfj 1
a 3
a 1
a 2
a 4
w 2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1

output:

-1
-1
-1
2
15
5
10
24
-1
-1

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 4ms
memory: 29060kb

input:

alfalfa
5
alfa 1
alfa 2
alfa 3
alfalfa 1
alfalfa 2

output:

1
4
-1
1
-1

result:

ok 5 lines

Test #7:

score: 0
Accepted
time: 2ms
memory: 29228kb

input:

x
28
a 1
b 1
c 1
d 1
e 1
f 1
g 1
h 1
i 1
j 1
k 1
l 1
m 1
n 1
o 1
p 1
q 1
r 1
s 1
t 1
u 1
v 1
w 1
x 1
y 1
z 1
abcdefghijklmnopqrstuvwxyz 1
abcdefghijklmnopqrstuvwxyz 1

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

result:

ok 28 lines

Test #8:

score: 0
Accepted
time: 57ms
memory: 37428kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...

result:

ok 200000 lines

Test #9:

score: 0
Accepted
time: 36ms
memory: 83472kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

100001
-1

result:

ok 2 lines

Test #10:

score: 0
Accepted
time: 46ms
memory: 131896kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

2

result:

ok single line: '2'

Test #11:

score: 0
Accepted
time: 46ms
memory: 131860kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1

result:

ok single line: '1'

Test #12:

score: 0
Accepted
time: 58ms
memory: 55900kb

input:

abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaab...

output:

1
1537
3073
5121
6145
7681
9217
10241
12289
13825
15361
17409
18433
20481
22017
23553
24577
26113
27649
29697
30721
32257
33793
34817
36865
38401
39937
40961
42497
44033
46081
47105
49153
50689
52225
54273
55297
56833
58369
59393
61441
62977
64513
66561
67585
69633
71169
72705
73729
75265
76801
7884...

result:

ok 195 lines