#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
map <char, ll> Map;
ll tin[2][200005], tout[2][200005];
ll k, ans[200005];
struct Node
{
vector <ll> crr;
ll child[4];
Node ()
{
fill(begin(child), end(child), -1);
}
};
struct Trie
{
ll Time=0;
vector <Node> t;
Trie ()
{
t.emplace_back();
}
void addstring(string s, ll idx)
{
ll p=0;
for (ll i=0; i<s.size(); i++)
{
ll c=Map[s[i]];
if (t[p].child[c]==-1)
{
t[p].child[c]=t.size();
t.emplace_back();
}
p=t[p].child[c];
}
t[p].crr.pb(idx);
}
void setup(ll p, ll num)
{
Time++;
for (ll i:t[p].crr)
tin[num][i]=Time;
for (ll i=0; i<4; i++)
if (t[p].child[i]!=-1)
setup(t[p].child[i], num);
for (ll i:t[p].crr)
tout[num][i]=Time;
k=max(k, Time);
}
};
vector <ll> num[2000005];
vector <pair<pll, pll>> event[2000005];
struct BIT
{
ll n;
vector <ll> bit;
BIT (ll n): n(n)
{
bit.assign(n+5, 0);
}
void update(ll i, ll val)
{
for (i; i<=n; i+=i&(-i))
bit[i]+=val;
}
ll query(ll l, ll r)
{
ll ans=0; l--;
for (r; r>0; r-=r&(-r))
ans+=bit[r];
for (l; l>0; l-=l&(-l))
ans-=bit[l];
return ans;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
Map['A']=0; Map['G']=1; Map['C']=2; Map['U']=3;
ll n, m; cin >> n >> m;
Trie A1, A2;
for (ll i=1; i<=n; i++)
{
string s; cin >> s;
A1.addstring(s, i);
reverse(s.begin(), s.end());
A2.addstring(s, i);
}
for (ll i=1; i<=m; i++)
{
string p, s; cin >> p >> s;
reverse(s.begin(), s.end());
A1.addstring(p, i+n);
A2.addstring(s, i+n);
}
A1.setup(0, 0); A2.setup(0, 1);
for (ll i=1; i<=m; i++)
{
event[tin[0][i+n]-1].pb({{tin[1][i+n], tout[1][i+n]}, {i, -1}});
event[tout[0][i+n]].pb({{tin[1][i+n], tout[1][i+n]}, {i, 1}});
}
for (ll i=1; i<=n; i++)
num[tin[0][i]].pb(tin[1][i]);
BIT A(k);
for (ll i=0; i<=k; i++)
{
for (ll j:num[i])
A.update(j, 1);
for (pair<pll, pll> j:event[i])
{
ll l=j.fi.fi, r=j.fi.se, idx=j.se.fi, mul=j.se.se;
ans[idx]+=A.query(l, r)*mul;
}
}
for (ll i=1; i<=m; i++)
cout << ans[i] << " ";
}
Compilation message
selling_rna.cpp: In member function 'void Trie::addstring(std::string, long long int)':
selling_rna.cpp:39:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (ll i=0; i<s.size(); i++)
| ~^~~~~~~~~
selling_rna.cpp: In member function 'void BIT::update(long long int, long long int)':
selling_rna.cpp:81:14: warning: statement has no effect [-Wunused-value]
81 | for (i; i<=n; i+=i&(-i))
| ^
selling_rna.cpp: In member function 'long long int BIT::query(long long int, long long int)':
selling_rna.cpp:88:14: warning: statement has no effect [-Wunused-value]
88 | for (r; r>0; r-=r&(-r))
| ^
selling_rna.cpp:90:14: warning: statement has no effect [-Wunused-value]
90 | for (l; l>0; l-=l&(-l))
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
94284 KB |
Output is correct |
2 |
Correct |
42 ms |
94284 KB |
Output is correct |
3 |
Correct |
41 ms |
94292 KB |
Output is correct |
4 |
Correct |
41 ms |
94272 KB |
Output is correct |
5 |
Correct |
41 ms |
94316 KB |
Output is correct |
6 |
Correct |
41 ms |
94284 KB |
Output is correct |
7 |
Correct |
47 ms |
94284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
218268 KB |
Output is correct |
2 |
Correct |
208 ms |
212364 KB |
Output is correct |
3 |
Correct |
214 ms |
217060 KB |
Output is correct |
4 |
Correct |
209 ms |
211276 KB |
Output is correct |
5 |
Correct |
298 ms |
267036 KB |
Output is correct |
6 |
Correct |
303 ms |
266872 KB |
Output is correct |
7 |
Correct |
78 ms |
95460 KB |
Output is correct |
8 |
Correct |
209 ms |
181204 KB |
Output is correct |
9 |
Correct |
207 ms |
181448 KB |
Output is correct |
10 |
Correct |
159 ms |
181476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
103852 KB |
Output is correct |
2 |
Correct |
59 ms |
101340 KB |
Output is correct |
3 |
Correct |
67 ms |
102120 KB |
Output is correct |
4 |
Correct |
60 ms |
100404 KB |
Output is correct |
5 |
Correct |
58 ms |
100772 KB |
Output is correct |
6 |
Correct |
61 ms |
102248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
94284 KB |
Output is correct |
2 |
Correct |
42 ms |
94284 KB |
Output is correct |
3 |
Correct |
41 ms |
94292 KB |
Output is correct |
4 |
Correct |
41 ms |
94272 KB |
Output is correct |
5 |
Correct |
41 ms |
94316 KB |
Output is correct |
6 |
Correct |
41 ms |
94284 KB |
Output is correct |
7 |
Correct |
47 ms |
94284 KB |
Output is correct |
8 |
Correct |
222 ms |
218268 KB |
Output is correct |
9 |
Correct |
208 ms |
212364 KB |
Output is correct |
10 |
Correct |
214 ms |
217060 KB |
Output is correct |
11 |
Correct |
209 ms |
211276 KB |
Output is correct |
12 |
Correct |
298 ms |
267036 KB |
Output is correct |
13 |
Correct |
303 ms |
266872 KB |
Output is correct |
14 |
Correct |
78 ms |
95460 KB |
Output is correct |
15 |
Correct |
209 ms |
181204 KB |
Output is correct |
16 |
Correct |
207 ms |
181448 KB |
Output is correct |
17 |
Correct |
159 ms |
181476 KB |
Output is correct |
18 |
Correct |
65 ms |
103852 KB |
Output is correct |
19 |
Correct |
59 ms |
101340 KB |
Output is correct |
20 |
Correct |
67 ms |
102120 KB |
Output is correct |
21 |
Correct |
60 ms |
100404 KB |
Output is correct |
22 |
Correct |
58 ms |
100772 KB |
Output is correct |
23 |
Correct |
61 ms |
102248 KB |
Output is correct |
24 |
Correct |
203 ms |
209840 KB |
Output is correct |
25 |
Correct |
221 ms |
209852 KB |
Output is correct |
26 |
Correct |
198 ms |
209824 KB |
Output is correct |
27 |
Correct |
197 ms |
209996 KB |
Output is correct |
28 |
Correct |
156 ms |
129324 KB |
Output is correct |
29 |
Correct |
96 ms |
113608 KB |
Output is correct |