#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <algorithm>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const sigma = 4;
struct Trie{
std::vector<int> ids;
Trie* p[sigma];
Trie() {
for(int i = 0; i < sigma; i++)
p[i] = nullptr;
}
};
int const bigsigma = 256;
char mp[1 + bigsigma];
void _add(Trie* &root, std::string &s, int pos, int id) {
if(root == nullptr)
root = new Trie();
if(s.size() == pos)
root->ids.push_back(id);
else
_add(root->p[mp[s[pos]]], s, pos + 1, id);
}
void dfs(Trie* &root, int target, std::vector<int> &parc, std::vector<int> &start, std::vector<int> &stop) {
for(int h = 0; h < root->ids.size(); h++) {
int id = root->ids[h];
if(target < id)
start[id - 1 - target] = parc.size();
}
for(int h = 0; h < root->ids.size(); h++) {
int id = root->ids[h];
if(id <= target)
parc.push_back(id);
}
for(int h = 0; h < sigma; h++)
if(root->p[h] != nullptr)
dfs(root->p[h], target, parc, start, stop);
for(int h = 0; h < root->ids.size(); h++) {
int id = root->ids[h];
if(target < id)
stop[id - 1 - target] = parc.size() - 1;
}
}
Trie *root = nullptr, *root2 = nullptr;
class FenwickTree{
private:
int n;
std::vector<int> aib;
public:
FenwickTree(int n_) {
n = n_;
aib.resize(1 + n);
}
void update(int pos, int val) {
if(pos == 0) {
aib[0] += val;
return ;
}
for(int x = pos; x <= n; x += (x ^ (x & (x - 1))))
aib[x] += val;
}
int query(int pos) {
if(pos < 0)
return 0;
int result = 0;
for(int x = pos; 0 < x; x = (x & (x - 1)))
result += aib[x];
return result + aib[0];
}
};
int const nmax = 100000;
int sol[1 + nmax];
int main() {
mp['A'] = 0;
mp['G'] = 1;
mp['C'] = 2;
mp['U'] = 3;
int n, q;
std::cin >> n >> q;
for(int i = 1; i <= n; i++) {
std::string s;
std::cin >> s;
_add(root, s, 0, i);
std::reverse(s.begin(), s.end());
_add(root2, s, 0, i);
}
for(int i = 1;i <= q; i++) {
std::string p, q;
std::cin >> p >> q;
std::reverse(q.begin(), q.end());
_add(root, p, 0, n + i);
_add(root2, q, 0, n + i);
}
std::vector<int> parc1, start1(q), stop1(q);
std::vector<int> parc2, start2(q), stop2(q);
std::vector<int> pos1(n + 1);
dfs(root, n, parc1, start1, stop1);
dfs(root2, n, parc2, start2, stop2);
for(int i = 0; i < n; i++)
pos1[parc1[i]] = i;
std::vector<std::vector<std::pair<int,int>>> events(1 + n);
for(int i = 0; i < q; i++) {
if(start2[i] <= stop2[i]) {
events[stop2[i]].push_back({i, 1});
if(0 < start2[i])
events[start2[i] - 1].push_back({i, -1});
}
}
FenwickTree aib(1 + n);
for(int i = 0; i < n; i++) {
aib.update(pos1[parc2[i]], 1);
for(int h = 0; h < events[i].size(); h++) {
int id = events[i][h].first;
sol[id] += events[i][h].second * (aib.query(stop1[id]) - aib.query(start1[id] - 1));
}
}
for(int i = 0; i < q; i++)
std::cout << sol[i] << '\n';
return 0;
}
Compilation message
selling_rna.cpp: In function 'void _add(Trie*&, std::string&, int, int)':
selling_rna.cpp:30:15: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
30 | if(s.size() == pos)
| ~~~~~~~~~^~~~~~
selling_rna.cpp:33:27: warning: array subscript has type 'char' [-Wchar-subscripts]
33 | _add(root->p[mp[s[pos]]], s, pos + 1, id);
| ^
selling_rna.cpp:33:27: warning: array subscript has type 'char' [-Wchar-subscripts]
33 | _add(root->p[mp[s[pos]]], s, pos + 1, id);
| ~~~~~~~~~^
selling_rna.cpp: In function 'void dfs(Trie*&, int, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
selling_rna.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int h = 0; h < root->ids.size(); h++) {
| ~~^~~~~~~~~~~~~~~~~~
selling_rna.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int h = 0; h < root->ids.size(); h++) {
| ~~^~~~~~~~~~~~~~~~~~
selling_rna.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int h = 0; h < root->ids.size(); h++) {
| ~~^~~~~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:142:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for(int h = 0; h < events[i].size(); h++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
128492 KB |
Output is correct |
2 |
Correct |
279 ms |
121964 KB |
Output is correct |
3 |
Correct |
280 ms |
126828 KB |
Output is correct |
4 |
Correct |
283 ms |
120940 KB |
Output is correct |
5 |
Correct |
301 ms |
156268 KB |
Output is correct |
6 |
Correct |
304 ms |
158572 KB |
Output is correct |
7 |
Correct |
165 ms |
6636 KB |
Output is correct |
8 |
Correct |
313 ms |
96444 KB |
Output is correct |
9 |
Correct |
297 ms |
82028 KB |
Output is correct |
10 |
Correct |
202 ms |
77164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
5824 KB |
Output is correct |
2 |
Correct |
35 ms |
4204 KB |
Output is correct |
3 |
Correct |
41 ms |
5000 KB |
Output is correct |
4 |
Correct |
36 ms |
4332 KB |
Output is correct |
5 |
Correct |
35 ms |
4456 KB |
Output is correct |
6 |
Correct |
42 ms |
5352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
280 ms |
128492 KB |
Output is correct |
9 |
Correct |
279 ms |
121964 KB |
Output is correct |
10 |
Correct |
280 ms |
126828 KB |
Output is correct |
11 |
Correct |
283 ms |
120940 KB |
Output is correct |
12 |
Correct |
301 ms |
156268 KB |
Output is correct |
13 |
Correct |
304 ms |
158572 KB |
Output is correct |
14 |
Correct |
165 ms |
6636 KB |
Output is correct |
15 |
Correct |
313 ms |
96444 KB |
Output is correct |
16 |
Correct |
297 ms |
82028 KB |
Output is correct |
17 |
Correct |
202 ms |
77164 KB |
Output is correct |
18 |
Correct |
47 ms |
5824 KB |
Output is correct |
19 |
Correct |
35 ms |
4204 KB |
Output is correct |
20 |
Correct |
41 ms |
5000 KB |
Output is correct |
21 |
Correct |
36 ms |
4332 KB |
Output is correct |
22 |
Correct |
35 ms |
4456 KB |
Output is correct |
23 |
Correct |
42 ms |
5352 KB |
Output is correct |
24 |
Correct |
265 ms |
106988 KB |
Output is correct |
25 |
Correct |
286 ms |
108988 KB |
Output is correct |
26 |
Correct |
260 ms |
105204 KB |
Output is correct |
27 |
Correct |
272 ms |
105836 KB |
Output is correct |
28 |
Correct |
253 ms |
29080 KB |
Output is correct |
29 |
Correct |
164 ms |
13880 KB |
Output is correct |