#include<bits/stdc++.h>
#define S second
#define F first
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
int inf = 1e9;
const int maxn = 1e5+20;
vector<string> vec;
vector<string> tmp;
vector<string> seg[maxn*4];
string nxt(string s){
int n = s.size();
string t=s;
t[n-1]++;
return t;
}
void RVR(string&s){
int n = s.size();
for(int i=0,j=n-1;i < j;i++,j--){
swap(s[i],s[j]);
}
}
void build(int s,int e,int v){
if(e-s<2){
seg[v].push_back(tmp[s]);
return;
}
int mid = (s+e)/2;
build(s,mid,2*v);
build(mid,e,2*v+1);
int l=0,r=0;
while(l<seg[2*v].size() || r<seg[2*v+1].size()){
if(l>=seg[2*v].size() || (r<seg[2*v+1].size() && seg[2*v+1][r]<=seg[2*v][l])){
seg[v].push_back(seg[2*v+1][r]);
r++;
}else{
seg[v].push_back(seg[2*v][l]);
l++;
}
}
}
int ans(int s,int e,int v,int l,int r,string ll,string rr){
if(l<=s && e<=r){
return lower_bound(seg[v].begin(),seg[v].end(),rr)-lower_bound(seg[v].begin(),seg[v].end(),ll);
}
if(l>=e || s>=r){
return 0;
}
int mid = (s+e)/2;
return ans(s,mid,2*v,l,r,ll,rr)+ans(mid,e,2*v+1,l,r,ll,rr);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
string s;
cin>>s;
vec.push_back(s);
}
sort(vec.begin(),vec.end());
for(string s:vec){
RVR(s);
tmp.push_back(s);
}
int sz = tmp.size();
build(0,sz,1);
for(int i=0;i<m;i++){
string p,q;
cin>>p>>q;
int l = lower_bound(vec.begin(),vec.end(),p)-vec.begin();
int r = lower_bound(vec.begin(),vec.end(),nxt(p))-vec.begin();
RVR(q);
//cout<<l<<" "<<r<<"\n";
cout<<ans(0,sz,1,l,r,q,nxt(q))<<"\n";
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'void build(int, int, int)':
selling_rna.cpp:33:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | while(l<seg[2*v].size() || r<seg[2*v+1].size()){
| ~^~~~~~~~~~~~~~~~
selling_rna.cpp:33:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | while(l<seg[2*v].size() || r<seg[2*v+1].size()){
| ~^~~~~~~~~~~~~~~~~~
selling_rna.cpp:34:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if(l>=seg[2*v].size() || (r<seg[2*v+1].size() && seg[2*v+1][r]<=seg[2*v][l])){
| ~^~~~~~~~~~~~~~~~~
selling_rna.cpp:34:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if(l>=seg[2*v].size() || (r<seg[2*v+1].size() && seg[2*v+1][r]<=seg[2*v][l])){
| ~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
38508 KB |
Output is correct |
2 |
Correct |
99 ms |
44392 KB |
Output is correct |
3 |
Correct |
126 ms |
41580 KB |
Output is correct |
4 |
Correct |
123 ms |
44648 KB |
Output is correct |
5 |
Correct |
73 ms |
33000 KB |
Output is correct |
6 |
Correct |
73 ms |
33384 KB |
Output is correct |
7 |
Correct |
80 ms |
31596 KB |
Output is correct |
8 |
Correct |
87 ms |
44420 KB |
Output is correct |
9 |
Correct |
105 ms |
44392 KB |
Output is correct |
10 |
Correct |
105 ms |
44392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
47460 KB |
Output is correct |
2 |
Correct |
191 ms |
30052 KB |
Output is correct |
3 |
Correct |
246 ms |
42340 KB |
Output is correct |
4 |
Correct |
167 ms |
36196 KB |
Output is correct |
5 |
Correct |
152 ms |
29796 KB |
Output is correct |
6 |
Correct |
214 ms |
42340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
51 ms |
38508 KB |
Output is correct |
9 |
Correct |
99 ms |
44392 KB |
Output is correct |
10 |
Correct |
126 ms |
41580 KB |
Output is correct |
11 |
Correct |
123 ms |
44648 KB |
Output is correct |
12 |
Correct |
73 ms |
33000 KB |
Output is correct |
13 |
Correct |
73 ms |
33384 KB |
Output is correct |
14 |
Correct |
80 ms |
31596 KB |
Output is correct |
15 |
Correct |
87 ms |
44420 KB |
Output is correct |
16 |
Correct |
105 ms |
44392 KB |
Output is correct |
17 |
Correct |
105 ms |
44392 KB |
Output is correct |
18 |
Correct |
212 ms |
47460 KB |
Output is correct |
19 |
Correct |
191 ms |
30052 KB |
Output is correct |
20 |
Correct |
246 ms |
42340 KB |
Output is correct |
21 |
Correct |
167 ms |
36196 KB |
Output is correct |
22 |
Correct |
152 ms |
29796 KB |
Output is correct |
23 |
Correct |
214 ms |
42340 KB |
Output is correct |
24 |
Correct |
231 ms |
51172 KB |
Output is correct |
25 |
Correct |
376 ms |
51060 KB |
Output is correct |
26 |
Correct |
207 ms |
51044 KB |
Output is correct |
27 |
Correct |
286 ms |
51044 KB |
Output is correct |
28 |
Correct |
1199 ms |
132584 KB |
Output is correct |
29 |
Correct |
616 ms |
88260 KB |
Output is correct |