#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][2000005], tout[2][2000005];
ll k, ans[2000005];
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[200005];
vector <pair<pll, pll>> event[200005];
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 |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9880 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9752 KB |
Output is correct |
5 |
Correct |
5 ms |
9808 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
292 ms |
271216 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
19388 KB |
Output is correct |
2 |
Correct |
22 ms |
16708 KB |
Output is correct |
3 |
Correct |
31 ms |
17560 KB |
Output is correct |
4 |
Correct |
19 ms |
15920 KB |
Output is correct |
5 |
Correct |
24 ms |
16160 KB |
Output is correct |
6 |
Correct |
25 ms |
17744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9880 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9752 KB |
Output is correct |
5 |
Correct |
5 ms |
9808 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Runtime error |
292 ms |
271216 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |