#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;
vector<int> v;
vector<int> sum;
vector<vector<int>> adj;
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.pb(0);
sum.pb(0);
adj.pb({});
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){
v.pb(0);
sum.pb(0);
adj.pb({});
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
916 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
26632 KB |
Output is correct |
2 |
Correct |
53 ms |
25096 KB |
Output is correct |
3 |
Correct |
61 ms |
25352 KB |
Output is correct |
4 |
Correct |
45 ms |
25344 KB |
Output is correct |
5 |
Correct |
56 ms |
24832 KB |
Output is correct |
6 |
Correct |
61 ms |
26364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Runtime error |
916 ms |
1048576 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |