This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 100000000000000000
#define int long long
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
#define ordered_set tree<pair<int, int>, null_type,less<pair<int, int>>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
const int N = 1e5 + 5;
int n, m;
vector<string> v;
vector<string> v1;
vector<string> mst[(int)4*N];
string reverse(string s){
string tr = "";
for(int i = s.length()-1;i>=0;i--) tr+=s[i];
return tr;
}
void build(int l ,int u, int i){
if(l==u){
mst[i].push_back(v1[l]);
return;
}
build(l, mid(l, u), lchild(i));
build(mid(l,u)+1, u, rchild(i));
merge(mst[lchild(i)].begin(), mst[lchild(i)].end(), mst[rchild(i)].begin(), mst[rchild(i)].end(), back_inserter(mst[i]));
}
int query(int l, int u, int i, int ll, int uu, const string& s, const string& s1){
if(l>=ll && u<=uu){
//cout<<"RANGE "<<l<<" "<<u<<endl;
//cout<<s1<<" "<<s<<endl;
//cout<<mst[i][(lower_bound(mst[i].begin(), mst[i].end(), s1) - mst[i].begin())]<<" "<<mst[i][(lower_bound(mst[i].begin(), mst[i].end(), s) - mst[i].begin())]<<endl;
int ans = (lower_bound(mst[i].begin(), mst[i].end(), s1) - lower_bound(mst[i].begin(), mst[i].end(), s));
return ans;
}
if(l>uu || u<ll) return 0;
return query(l, mid(l, u), lchild(i), ll, uu, s, s1) + query(mid(l, u)+1, u, rchild(i), ll, uu, s, s1);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i = 0;i<n;i++){
string s;
cin>>s;
v.push_back(s);
}
sort(v.begin(), v.end());
for(int i = 0;i<n;i++){
v1.push_back(reverse(v[i]));
}
build(0, n-1, 0);
while(m--){
string p, q;
cin>>p>>q;
q = reverse(q);
string tempp = p;
string tempq = q;
tempp[p.size()-1]++;
tempq[q.size()-1]++;
int begind = lower_bound(v.begin(), v.end(), p) - v.begin();
int endind = lower_bound(v.begin(), v.end(), tempp) - v.begin() - 1;
//cout<<begind<<" "<<endind<<endl;
if(begind <= endind) cout<<query(0, n-1, 0, begind, endind, q, tempq)<<"\n";
else cout<<0<<"\n";
}
}
/*
8 7
GCGCUACCCCAACACAAGGCAAGAUAUA
G
GGAC
GCGG
U
GCGCUACCCCAACACAAGGCAAGAUGGUC
GCCG
GCGCUGA
GCGCUACCC A
GCGCUACCCC AC
GCG C
GCGC A
G G
G C
G GGA
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |