/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define ll long long
#define pii pair<int,int>
#define piiii pair<pii,pii>
#define fi first
#define se second
using namespace std;
/*Constants*/
const int N = 2e5+10;
const int INF = 1e9+7;
const long long LLINF = 1e18+3;
/*TestCases*/
int test=1;
void solve();
void TestCases(bool v){
if (v) cin >> test;
while(test--) solve();
}
int Getidx(char& c){
if (c=='A') return 0;
if (c=='C') return 1;
if (c=='G') return 2;
if (c=='U') return 3;
return 4;
}
//Rawr
struct Point{
int x,y;
Point(){
x = y = 0;
}
};
/*Global Variables*/
string a[N],b[N],c[N],d[N];
Point pts[N];
pii qx[N], qy[N];
int ans[N];
int n,m;
//BIT
struct BIT{
int n;
int pre[N];
BIT(int n_): n(n_){
for (int i=1; i<=n; i++){
pre[i] = 0;
}
};
void Add(int pos){
while (pos<=n){
++pre[pos];
pos += (pos&(-pos));
}
}
int GetPre(int pos){
int res = 0;
while (pos>0){
res+=pre[pos];
pos-=(pos&(-pos));
}
return res;
}
int Get(int l, int r){
if (l==0 || r==0) return 0;
return GetPre(r)-GetPre(l-1);
}
};
//Trie
struct Trie{
struct Node{
Node* child[4];
vector<int> query,lst;
int cnt;
Node(){
for (int idx=0; idx<4; idx++){
child[idx] = NULL;
cnt = 0;
}
}
};
Node* Root;
Trie(){
Root = new Node();
}
void AddString(string s, int x){
Node* cur = Root;
for (auto c:s){
int idx = Getidx(c);
if (cur->child[idx]==NULL) cur->child[idx] = new Node();
cur = cur->child[idx];
++cur->cnt;
}
cur->lst.push_back(x);
}
void AddQuery(string s, int x){
Node* cur = Root;
for (auto c:s){
int idx = Getidx(c);
if (cur->child[idx]==NULL) cur->child[idx] = new Node();
cur = cur->child[idx];
}
cur->query.push_back(x);
}
void Traverse(Node* cur, int& rawr, pii p[], bool xaxis){
if (cur->lst.size()!=0 || cur->query.size()!=0) ++rawr;
for (auto i:cur->lst){
if (xaxis) pts[i].x = rawr;
else pts[i].y = rawr;
}
for (auto i:cur->query){
p[i].fi = rawr;
}
// cout << rawr << endl;
for (int idx=0; idx<4; idx++){
if (cur->child[idx]!=NULL){
// cout << idx << " ";
Traverse(cur->child[idx],rawr,p,xaxis);
}
}
// cout << l << " " << r << endl;
for (auto i:cur->query){
p[i].se = rawr;
}
}
};
Trie pre,suf;
/*Solution*/
void solve(){
cin >> n >> m;
for (int i=1; i<=n; i++){
cin >> a[i];
pre.AddString(a[i],i);
b[i] = a[i];
reverse(b[i].begin(),b[i].end());
suf.AddString(b[i],i);
}
for (int i=1; i<=m; i++){
cin >> c[i];
pre.AddQuery(c[i],i);
cin >> d[i];
reverse(d[i].begin(),d[i].end());
suf.AddQuery(d[i],i);
}
int limx = 0;
pre.Traverse(pre.Root,limx,qx,true);
int limy = 0;
suf.Traverse(suf.Root,limy,qy,false);
BIT fw(limy);
vector<piiii> v,query;
for (int i=1; i<=n; i++){
v.push_back({{pts[i].x,i},{pts[i].y,pts[i].y}});
}
for (int i=1; i<=m; i++){
query.push_back({{qx[i].fi-1,-i},{qy[i].fi,qy[i].se}});
query.push_back({{qx[i].se,i},{qy[i].fi,qy[i].se}});
}
// for (int i=1; i<=n; i++){
// cout << pts[i].x << " " << pts[i].y << endl;
// }
// for (int i=1; i<=m; i++){
// cout << qx[i].fi << " " << qx[i].se << " " << qy[i].fi << " " << qy[i].se << endl;
// }
sort(v.begin(),v.end());
sort(query.begin(),query.end());
int i = 0;
int j = 0;
for (int idx=0; idx<=limx; idx++){
while (i!=(int)v.size() && v[i].fi.fi==idx){
fw.Add(v[i].se.fi);
++i;
}
while (j!=(int)query.size() && query[j].fi.fi==idx){
if (query[j].fi.se<0){
ans[-query[j].fi.se] -= fw.Get(query[j].se.fi,query[j].se.se);
}
else ans[query[j].fi.se] += fw.Get(query[j].se.fi,query[j].se.se);
++j;
}
}
// cout << i << " " << j << endl;
for (int i=1; i<=m; i++){
cout << ans[i] << endl;
}
}
/*Driver Code*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
TestCases(0);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
27736 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
11 ms |
27740 KB |
Output is correct |
4 |
Correct |
12 ms |
27740 KB |
Output is correct |
5 |
Correct |
11 ms |
27796 KB |
Output is correct |
6 |
Correct |
11 ms |
27740 KB |
Output is correct |
7 |
Correct |
11 ms |
27740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
223612 KB |
Output is correct |
2 |
Correct |
169 ms |
213956 KB |
Output is correct |
3 |
Correct |
161 ms |
221264 KB |
Output is correct |
4 |
Correct |
146 ms |
212348 KB |
Output is correct |
5 |
Correct |
212 ms |
263936 KB |
Output is correct |
6 |
Correct |
211 ms |
267228 KB |
Output is correct |
7 |
Correct |
68 ms |
41568 KB |
Output is correct |
8 |
Correct |
169 ms |
177004 KB |
Output is correct |
9 |
Correct |
143 ms |
155240 KB |
Output is correct |
10 |
Correct |
108 ms |
146844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
32908 KB |
Output is correct |
2 |
Correct |
30 ms |
31600 KB |
Output is correct |
3 |
Correct |
37 ms |
32664 KB |
Output is correct |
4 |
Correct |
39 ms |
32160 KB |
Output is correct |
5 |
Correct |
32 ms |
31452 KB |
Output is correct |
6 |
Correct |
36 ms |
32632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
27736 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
11 ms |
27740 KB |
Output is correct |
4 |
Correct |
12 ms |
27740 KB |
Output is correct |
5 |
Correct |
11 ms |
27796 KB |
Output is correct |
6 |
Correct |
11 ms |
27740 KB |
Output is correct |
7 |
Correct |
11 ms |
27740 KB |
Output is correct |
8 |
Correct |
165 ms |
223612 KB |
Output is correct |
9 |
Correct |
169 ms |
213956 KB |
Output is correct |
10 |
Correct |
161 ms |
221264 KB |
Output is correct |
11 |
Correct |
146 ms |
212348 KB |
Output is correct |
12 |
Correct |
212 ms |
263936 KB |
Output is correct |
13 |
Correct |
211 ms |
267228 KB |
Output is correct |
14 |
Correct |
68 ms |
41568 KB |
Output is correct |
15 |
Correct |
169 ms |
177004 KB |
Output is correct |
16 |
Correct |
143 ms |
155240 KB |
Output is correct |
17 |
Correct |
108 ms |
146844 KB |
Output is correct |
18 |
Correct |
38 ms |
32908 KB |
Output is correct |
19 |
Correct |
30 ms |
31600 KB |
Output is correct |
20 |
Correct |
37 ms |
32664 KB |
Output is correct |
21 |
Correct |
39 ms |
32160 KB |
Output is correct |
22 |
Correct |
32 ms |
31452 KB |
Output is correct |
23 |
Correct |
36 ms |
32632 KB |
Output is correct |
24 |
Correct |
146 ms |
191448 KB |
Output is correct |
25 |
Correct |
167 ms |
193620 KB |
Output is correct |
26 |
Correct |
141 ms |
188748 KB |
Output is correct |
27 |
Correct |
143 ms |
189520 KB |
Output is correct |
28 |
Correct |
132 ms |
69764 KB |
Output is correct |
29 |
Correct |
85 ms |
40272 KB |
Output is correct |