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>
using namespace std;
#define ll long long int
#define sz(x) (int) x.size()
#define pb push_back
const int mxN = 1e5+5;
const int mC = 3e7;
int n;
int v[mC];
int sum[mC];
vector<int> adj[mC];
int roots[4*mxN+1];
int cnt = 0;
int dfs(int node, int idx, string &s){
if (idx == 0) return sum[node];
for (int i : adj[node]){
if (v[i] == s[idx-1]-'A') return dfs(i,idx-1,s);
}
return 0;
}
void add(int node, int idx, string &s){
sum[node]++;
if (idx == 0) return;
//cout << node << " " << (char) (v[node] + 'A') << ": ";
//for (int i : adj[node]) cout << i << " ";
//cout << '\n';
for (int i : adj[node]){
if (v[i] == s[idx-1]-'A') return add(i,idx-1,s);
}
v[cnt] = s[idx-1] - 'A';
sum[cnt] = 0;
adj[node].pb(cnt);
cnt++;
return add(cnt-1,idx-1,s);
}
void upd(int t, string &cv, int l = 0, int r = n-1, int idx = 0){
if (l > t || r < t) return;
//cout << t << "," << cv << " " << l << "," << r << ": ";
if (roots[idx] == -1){
roots[idx] = cnt;
sum[cnt] = 0;
v[cnt] = 1;
cnt++;
}
//cout << roots[idx] << '\n';
add(roots[idx],sz(cv)-1,cv);
if (l == r) return;
int m = (l+r)/2;
upd(t,cv,l,m,2*idx+1);
upd(t,cv,m+1,r,2*idx+2);
}
int query(int tl, int tr, string &cv, int l = 0, int r = n-1, int idx = 0){
if (tl > tr) return 0;
if (l > tr || r < tl) return 0;
//cout << cv << " " << l << " " << r << " " << tl << " " << tr << '\n';
if (l >= tl && r <= tr) return dfs(roots[idx],sz(cv)-1,cv);
int m = (l+r)/2;
return query(tl,tr,cv,l,m,2*idx+1) + query(tl,tr,cv,m+1,r,2*idx+2);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m;
cin >> n >> m;
vector<string> all(n);
for (int i = 0; i < n; i++) cin >> all[i];
sort(all.begin(),all.end());
for (int i = 0; i < 4*n+1; i++) roots[i] = -1;
string temp;
for (int i = 0; i < n; i++){
//cout << all[i] << " ";
temp = all[i]+'B';
upd(i,temp);
}
//cout << '\n';
int l,r;
string a,b;
//return 0;
for (int i = 0; i < m; i++){
cin >> a >> b;
l = lower_bound(all.begin(),all.end(), a) - all.begin();
r = lower_bound(all.begin(),all.end(), a+'Z') - all.begin();
temp = b + 'B';
//cout << a << " " << b << '\n';
//cout << ("AU" < "AUGC") << '\n';
//cout << l << " " << r << ' ' << temp << '\n';
cout << query(l,r-1,temp) << '\n';
}
return 0;
}
# | 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... |