#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=1e5+5;
const int mxl=2e6+5;
int n, m;
string s[mxn];
int agcu(char c){
if(c=='A')return 0;
if(c=='G')return 1;
if(c=='C')return 2;
return 3;
}
struct trie{
int timer=0, nxt[mxl][4], cnt[mxl], euler, in[mxl], out[mxl];
void init(){
timer=0;
euler=1;
memset(nxt,0,sizeof(nxt));
memset(cnt,0,sizeof(cnt));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
}
void ins(string s){
int cur=0;
for(auto c:s){
int x=agcu(c);
if(!nxt[cur][x]){
nxt[cur][x]=++timer;
}
cur=nxt[cur][x];
debug(cur);
}
cnt[cur]++;
}
void dfs(int v){
in[v]=euler;
euler+=cnt[v];
out[v]=euler-1;
for(int i=0;i<4;i++){
if(nxt[v][i]){
dfs(nxt[v][i]);
out[v]=out[nxt[v][i]];
}
}
}
pii query(string s){
int cur=0;
for(auto c:s){
int x=agcu(c);
if(nxt[cur][x]){
cur=nxt[cur][x];
}
else{
return {-1,-1};
}
}
return {in[cur],out[cur]};
}
}tr,rt;
struct BIT{
int bit[mxn];
void init(){
memset(bit,0,sizeof(bit));
}
void modify(int p,int v){
for(;p<mxn;p+=p&-p){
bit[p]+=v;
}
}
int query(int p){
int res=0;
for(;p;p-=p&-p){
res+=bit[p];
}
return res;
}
int query(int l,int r){
return query(r)-query(l-1);
}
};
vector<tuple<int,int,int>>in[mxn],out[mxn],mod[mxn];
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
tr.init();
rt.init();
for(int i=1;i<=n;i++){
cin>>s[i];
tr.ins(s[i]);
reverse(all(s[i]));
rt.ins(s[i]);
}
tr.dfs(0);
rt.dfs(0);
for(int i=1;i<=n;i++){
pii sec=rt.query(s[i]);
reverse(all(s[i]));
pii f=tr.query(s[i]);
mod[f.F].pb({sec.F,0,0});
}
for(int i=1;i<=m;i++){
string a,b;
cin>>a>>b;
pii f=tr.query(a);
reverse(all(b));
pii sec=rt.query(b);
if(f.F<0 or sec.F<0)continue;
debug(f,sec);
in[f.F].pb({i,sec.F,sec.S});
out[f.S].pb({i,sec.F,sec.S});
}
BIT bt;
bt.init();
vector<int>ans(m+1);
for(int i=1;i<=n;i++){
for(auto [id,l,r]:in[i]){
ans[id]-=bt.query(l,r);
}
for(auto [p,l,r]:mod[i]){
debug(p);
bt.modify(p,1);
}
for(auto [id,l,r]:out[i]){
ans[id]+=bt.query(l,r);
}
}
for(int i=1;i<=m;i++){
cout<<ans[i]<<'\n';
}
}
/*
input:
*/
Compilation message
selling_rna.cpp: In function 'void setIO(std::string)':
selling_rna.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
230480 KB |
Output is correct |
2 |
Correct |
31 ms |
230568 KB |
Output is correct |
3 |
Correct |
30 ms |
230548 KB |
Output is correct |
4 |
Correct |
33 ms |
230484 KB |
Output is correct |
5 |
Correct |
30 ms |
230492 KB |
Output is correct |
6 |
Correct |
30 ms |
230484 KB |
Output is correct |
7 |
Correct |
31 ms |
230416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
232880 KB |
Output is correct |
2 |
Correct |
99 ms |
237280 KB |
Output is correct |
3 |
Correct |
104 ms |
237344 KB |
Output is correct |
4 |
Correct |
99 ms |
237136 KB |
Output is correct |
5 |
Correct |
128 ms |
234848 KB |
Output is correct |
6 |
Correct |
129 ms |
234764 KB |
Output is correct |
7 |
Correct |
69 ms |
237844 KB |
Output is correct |
8 |
Correct |
103 ms |
238888 KB |
Output is correct |
9 |
Correct |
106 ms |
238932 KB |
Output is correct |
10 |
Correct |
84 ms |
236636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
235368 KB |
Output is correct |
2 |
Correct |
44 ms |
234380 KB |
Output is correct |
3 |
Correct |
48 ms |
234876 KB |
Output is correct |
4 |
Correct |
44 ms |
234056 KB |
Output is correct |
5 |
Correct |
44 ms |
234068 KB |
Output is correct |
6 |
Correct |
51 ms |
235076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
230480 KB |
Output is correct |
2 |
Correct |
31 ms |
230568 KB |
Output is correct |
3 |
Correct |
30 ms |
230548 KB |
Output is correct |
4 |
Correct |
33 ms |
230484 KB |
Output is correct |
5 |
Correct |
30 ms |
230492 KB |
Output is correct |
6 |
Correct |
30 ms |
230484 KB |
Output is correct |
7 |
Correct |
31 ms |
230416 KB |
Output is correct |
8 |
Correct |
101 ms |
232880 KB |
Output is correct |
9 |
Correct |
99 ms |
237280 KB |
Output is correct |
10 |
Correct |
104 ms |
237344 KB |
Output is correct |
11 |
Correct |
99 ms |
237136 KB |
Output is correct |
12 |
Correct |
128 ms |
234848 KB |
Output is correct |
13 |
Correct |
129 ms |
234764 KB |
Output is correct |
14 |
Correct |
69 ms |
237844 KB |
Output is correct |
15 |
Correct |
103 ms |
238888 KB |
Output is correct |
16 |
Correct |
106 ms |
238932 KB |
Output is correct |
17 |
Correct |
84 ms |
236636 KB |
Output is correct |
18 |
Correct |
51 ms |
235368 KB |
Output is correct |
19 |
Correct |
44 ms |
234380 KB |
Output is correct |
20 |
Correct |
48 ms |
234876 KB |
Output is correct |
21 |
Correct |
44 ms |
234056 KB |
Output is correct |
22 |
Correct |
44 ms |
234068 KB |
Output is correct |
23 |
Correct |
51 ms |
235076 KB |
Output is correct |
24 |
Correct |
100 ms |
238932 KB |
Output is correct |
25 |
Correct |
108 ms |
241644 KB |
Output is correct |
26 |
Correct |
95 ms |
237904 KB |
Output is correct |
27 |
Correct |
99 ms |
238672 KB |
Output is correct |
28 |
Correct |
122 ms |
249224 KB |
Output is correct |
29 |
Correct |
87 ms |
243592 KB |
Output is correct |