#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define lb(s, a) lower_bound(all(s), (a)) - s.begin()
#define ii pair <int, int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int ar = 2e6+5;
const ll mod = 1e9+7;
const int oo = 1e9;
int n, m, maxx = 0, maxy = 0;
int cs(char a) {
if(a == 'C') return 0;
if(a == 'U') return 1;
if(a == 'G') return 2;
return 3;
}
struct Trie {
struct Node{
int child[5];
} nodes[ar];
int cur;
Trie() : cur(0) {
memset(nodes[0].child, -1, sizeof(nodes[cur].child));
};
int new_node() {
cur++;
memset(nodes[cur].child, -1, sizeof(nodes[cur].child));
return cur;
}
void add(string s) {
int pos = 0;
for (auto f : s) {
int c = cs(f);
if (nodes[pos].child[c] == -1) {
nodes[pos].child[c] = new_node();
}
pos = nodes[pos].child[c];
}
}
int trace(string s) {
int pos = 0;
for (auto f : s) {
int c = cs(f);
pos = nodes[pos].child[c];
}
return in[pos];
}
int in[ar], out[ar], timeDfs = 0;
void dfs(int u) {
in[u] = ++timeDfs;
for(int i = 0; i < 4; ++i) if(nodes[u].child[i] != -1)
dfs(nodes[u].child[i]);
out[u] = timeDfs;
}
ii get(string s) {
int pos = 0;
for (auto f : s) {
int c = cs(f);
if (nodes[pos].child[c] == -1)
return {-1, -1};
pos = nodes[pos].child[c];
}
return {in[pos], out[pos]};
}
} t1, t2;
int bit[ar], res[ar], L[ar];
vector <int> x[ar];
struct Query {
int l, r, i;
bool t;
};
vector <Query> query[ar];
string s[100005];
void update(int i, int val) {
for(; i <= maxy; i += i & -i)
bit[i] += val;
}
int get(int i) {
if(i == 0) return 0;
int res = 0;
for(; i > 0; i -= i & -i)
res += bit[i];
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "b"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
cin >> s[i];
t1.add(s[i]);
string tmp = s[i];
reverse(all(tmp));
t2.add(tmp);
}
t1.dfs(0);
t2.dfs(0);
for(int i = 1; i <= n; ++i) {
string tmp = s[i];
reverse(all(tmp));
int tmp1 = t1.trace(s[i]), tmp2 = t2.trace(tmp);
x[tmp1].push_back(tmp2);
// cout << i << ' ' << tmp1 << ' ' << tmp2 << '\n';
}
string p, q;
for(int i = 1; i <= m; ++i) {
cin >> p >> q;
ii p1 = t1.get(p);
if(p1.fi == -1) continue;
reverse(all(q));
ii p2 = t2.get(q);
if(p2.fi == -1) continue;
// cout << i << ' ' << p1.fi << ' ' << p1.se << ' ' << p2.fi << ' ' << p2.se << '\n';
// if(p1.fi > p2.fi) swap(p1.fi, p2.fi);
// if(p1.se > p2.se) swap(p2.se, p1.se);
query[p1.fi - 1].push_back({p2.fi, p2.se, i, 0});
query[p1.se].push_back({p2.fi, p2.se, i, 1});
maxx = max({maxx, p1.se});
maxy = max({maxy, p2.se});
}
for(int i = 1; i <= maxx; ++i) {
for(auto y : x[i])
update(y, 1);
for(auto [l, r, j, t] : query[i]) {
int tmp = get(r) - get(l - 1);
if(t == 0) L[j] = tmp;
else res[j] = tmp - L[j];
}
}
for(int i = 1; i <= m; ++i)
cout << res[i] << '\n';
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
113500 KB |
Output is correct |
2 |
Correct |
14 ms |
113464 KB |
Output is correct |
3 |
Correct |
16 ms |
113500 KB |
Output is correct |
4 |
Correct |
14 ms |
113444 KB |
Output is correct |
5 |
Correct |
14 ms |
113500 KB |
Output is correct |
6 |
Correct |
14 ms |
113500 KB |
Output is correct |
7 |
Correct |
15 ms |
113500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
170324 KB |
Output is correct |
2 |
Correct |
81 ms |
169812 KB |
Output is correct |
3 |
Correct |
80 ms |
164432 KB |
Output is correct |
4 |
Correct |
78 ms |
161876 KB |
Output is correct |
5 |
Correct |
105 ms |
179280 KB |
Output is correct |
6 |
Correct |
111 ms |
180304 KB |
Output is correct |
7 |
Correct |
48 ms |
120656 KB |
Output is correct |
8 |
Correct |
79 ms |
153928 KB |
Output is correct |
9 |
Correct |
70 ms |
147016 KB |
Output is correct |
10 |
Correct |
59 ms |
143440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
116428 KB |
Output is correct |
2 |
Correct |
24 ms |
115696 KB |
Output is correct |
3 |
Correct |
31 ms |
117956 KB |
Output is correct |
4 |
Correct |
23 ms |
115412 KB |
Output is correct |
5 |
Correct |
23 ms |
115400 KB |
Output is correct |
6 |
Correct |
26 ms |
115792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
113500 KB |
Output is correct |
2 |
Correct |
14 ms |
113464 KB |
Output is correct |
3 |
Correct |
16 ms |
113500 KB |
Output is correct |
4 |
Correct |
14 ms |
113444 KB |
Output is correct |
5 |
Correct |
14 ms |
113500 KB |
Output is correct |
6 |
Correct |
14 ms |
113500 KB |
Output is correct |
7 |
Correct |
15 ms |
113500 KB |
Output is correct |
8 |
Correct |
81 ms |
170324 KB |
Output is correct |
9 |
Correct |
81 ms |
169812 KB |
Output is correct |
10 |
Correct |
80 ms |
164432 KB |
Output is correct |
11 |
Correct |
78 ms |
161876 KB |
Output is correct |
12 |
Correct |
105 ms |
179280 KB |
Output is correct |
13 |
Correct |
111 ms |
180304 KB |
Output is correct |
14 |
Correct |
48 ms |
120656 KB |
Output is correct |
15 |
Correct |
79 ms |
153928 KB |
Output is correct |
16 |
Correct |
70 ms |
147016 KB |
Output is correct |
17 |
Correct |
59 ms |
143440 KB |
Output is correct |
18 |
Correct |
31 ms |
116428 KB |
Output is correct |
19 |
Correct |
24 ms |
115696 KB |
Output is correct |
20 |
Correct |
31 ms |
117956 KB |
Output is correct |
21 |
Correct |
23 ms |
115412 KB |
Output is correct |
22 |
Correct |
23 ms |
115400 KB |
Output is correct |
23 |
Correct |
26 ms |
115792 KB |
Output is correct |
24 |
Correct |
74 ms |
163664 KB |
Output is correct |
25 |
Correct |
83 ms |
165492 KB |
Output is correct |
26 |
Correct |
71 ms |
162388 KB |
Output is correct |
27 |
Correct |
76 ms |
155472 KB |
Output is correct |
28 |
Correct |
84 ms |
131408 KB |
Output is correct |
29 |
Correct |
55 ms |
121680 KB |
Output is correct |