#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
int n,m;
string aa[100001];
vector<string> tree[400001];
void build(int no,int l,int r){
if(l==r){
tree[no].pb("");
for(int i=(int)(aa[l].size())-1;i>=0;i--){
tree[no][0]+=aa[l][i];
}
//tree[no].pb(aa[l]);
}
else{
int mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
int ind=0;
int ind2=0;
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
if(tree[no*2+1][ind]<tree[no*2+2][ind2]){
tree[no].pb(tree[no*2+1][ind]);
ind+=1;
}
else{
tree[no].pb(tree[no*2+2][ind2]);
ind2+=1;
}
}
while(ind<tree[no*2+1].size()){
tree[no].pb(tree[no*2+1][ind]);
ind+=1;
}
while(ind2<tree[no*2+2].size()){
tree[no].pb(tree[no*2+2][ind2]);
ind2+=1;
}
}
}
string cur;
string cur2;
int query(int no,int l,int r,int aa,int bb){
if(r<aa or l>bb or aa>bb){
return 0;
}
if(aa<=l and r<=bb){
int x=lower_bound(tree[no].begin(),tree[no].end(),cur2)-tree[no].begin()-1;
x-=lower_bound(tree[no].begin(),tree[no].end(),cur)-tree[no].begin();
return x+1;
}
else{
int mid=(l+r)/2;
return query(no*2+1,l,mid,aa,bb)+query(no*2+2,mid+1,r,aa,bb);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>aa[i];
}
sort(aa,aa+n);
build(0,0,n-1);
while(m--){
string s,tt;
cin>>s>>tt;
int x=lower_bound(aa,aa+n,s)-aa;
string ss;
ss=s;
ss[ss.size()-1]++;
int y=lower_bound(aa,aa+n,ss)-aa-1;
string t="";
for(int i=(int)(tt.size())-1;i>=0;i--){
t+=tt[i];
}
cur=t;
cur2=t;
cur2[cur2.size()-1]++;
// cout<<x<<','<<y<<endl;
cout<<query(0,0,n-1,x,y)<<'\n';
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'void build(int, int, int)':
selling_rna.cpp:26:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
~~~^~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:26:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
~~~~^~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:36:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size()){
~~~^~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:40:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind2<tree[no*2+2].size()){
~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12928 KB |
Output is correct |
2 |
Correct |
12 ms |
12928 KB |
Output is correct |
3 |
Correct |
12 ms |
12928 KB |
Output is correct |
4 |
Correct |
12 ms |
12928 KB |
Output is correct |
5 |
Correct |
12 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
12928 KB |
Output is correct |
7 |
Correct |
12 ms |
12928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
44408 KB |
Output is correct |
2 |
Correct |
111 ms |
49908 KB |
Output is correct |
3 |
Correct |
100 ms |
46968 KB |
Output is correct |
4 |
Correct |
112 ms |
49784 KB |
Output is correct |
5 |
Correct |
73 ms |
37492 KB |
Output is correct |
6 |
Correct |
79 ms |
37736 KB |
Output is correct |
7 |
Correct |
89 ms |
38904 KB |
Output is correct |
8 |
Correct |
98 ms |
51572 KB |
Output is correct |
9 |
Correct |
99 ms |
51444 KB |
Output is correct |
10 |
Correct |
88 ms |
49012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
46956 KB |
Output is correct |
2 |
Correct |
143 ms |
30880 KB |
Output is correct |
3 |
Correct |
187 ms |
42880 KB |
Output is correct |
4 |
Correct |
118 ms |
37232 KB |
Output is correct |
5 |
Correct |
118 ms |
30704 KB |
Output is correct |
6 |
Correct |
151 ms |
42860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12928 KB |
Output is correct |
2 |
Correct |
12 ms |
12928 KB |
Output is correct |
3 |
Correct |
12 ms |
12928 KB |
Output is correct |
4 |
Correct |
12 ms |
12928 KB |
Output is correct |
5 |
Correct |
12 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
12928 KB |
Output is correct |
7 |
Correct |
12 ms |
12928 KB |
Output is correct |
8 |
Correct |
68 ms |
44408 KB |
Output is correct |
9 |
Correct |
111 ms |
49908 KB |
Output is correct |
10 |
Correct |
100 ms |
46968 KB |
Output is correct |
11 |
Correct |
112 ms |
49784 KB |
Output is correct |
12 |
Correct |
73 ms |
37492 KB |
Output is correct |
13 |
Correct |
79 ms |
37736 KB |
Output is correct |
14 |
Correct |
89 ms |
38904 KB |
Output is correct |
15 |
Correct |
98 ms |
51572 KB |
Output is correct |
16 |
Correct |
99 ms |
51444 KB |
Output is correct |
17 |
Correct |
88 ms |
49012 KB |
Output is correct |
18 |
Correct |
162 ms |
46956 KB |
Output is correct |
19 |
Correct |
143 ms |
30880 KB |
Output is correct |
20 |
Correct |
187 ms |
42880 KB |
Output is correct |
21 |
Correct |
118 ms |
37232 KB |
Output is correct |
22 |
Correct |
118 ms |
30704 KB |
Output is correct |
23 |
Correct |
151 ms |
42860 KB |
Output is correct |
24 |
Correct |
209 ms |
56176 KB |
Output is correct |
25 |
Correct |
344 ms |
56432 KB |
Output is correct |
26 |
Correct |
149 ms |
56052 KB |
Output is correct |
27 |
Correct |
208 ms |
56304 KB |
Output is correct |
28 |
Correct |
997 ms |
130788 KB |
Output is correct |
29 |
Correct |
434 ms |
86500 KB |
Output is correct |