#include <bits/stdc++.h>
using namespace std;
const int L = 2000000 + 7;
const int N = (int) 1e5 + 7;
int aib[L];
void add(int i, int x) {
while (i < L) {
aib[i] += x;
i += i & (-i);
}
}
int get(int i) {
int sol = 0;
while (i) {
sol += aib[i];
i -= i & (-i);
}
return sol;
}
int decode(char ch) {
if (ch == 'A') return 0;
if (ch == 'G') return 1;
if (ch == 'C') return 2;
if (ch == 'U') return 3;
assert(false);
}
struct TrieNode {
TrieNode* kids[4];
int first;
int last;
vector<int> guys;
TrieNode() {
for (int i = 0; i < 4; i++) {
kids[i] = nullptr;
}
}
};
void addToTrie(TrieNode* root, string s, int index) {
TrieNode* current = root;
for (auto &ch : s) {
int decoded = decode(ch);
if (!current->kids[decoded]) {
current->kids[decoded] = new TrieNode;
}
current = current->kids[decoded];
}
current->guys.push_back(index);
}
void prepTrie(TrieNode* root, int inverse[]) {
int index = 0;
function<void(TrieNode*)> dfs = [&] (TrieNode* current) {
current->first = ++index;
for (auto &i : current->guys) {
inverse[i] = index;
}
for (int j = 0; j < 4; j++) {
if (current->kids[j]) {
dfs(current->kids[j]);
}
}
current->last = index;
};
dfs(root);
}
pair<int, int> getInterval(TrieNode* root, string s) {
TrieNode* current = root;
for (auto &ch : s) {
int decoded = decode(ch);
if (!current->kids[decoded]) {
return {1, 0};
}
current = current->kids[decoded];
}
return {current->first, current->last};
}
TrieNode* root1 = new TrieNode;
TrieNode* root2 = new TrieNode;
int x[N];
int y[N];
struct BBox {
int xmin;
int ymin;
int xmax;
int ymax;
int index;
};
struct Question {
int x;
int y;
int coef;
int index;
};
int solPrint[N];
int main() {
bool isHome;
isHome = true;
isHome = false;
if (isHome) {
freopen ("input", "r", stdin);
} else {
ios::sync_with_stdio(0); cin.tie(0);
}
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
string s, revS;
cin >> s;
revS = s;
reverse(revS.begin(), revS.end());
addToTrie(root1, s, i);
addToTrie(root2, revS, i);
}
prepTrie(root1, x);
prepTrie(root2, y);
vector<Question> questions;
for (int iq = 1; iq <= q; iq++) {
string pref, suf;
cin >> pref >> suf;
reverse(suf.begin(), suf.end());
auto xx = getInterval(root1, pref);
auto yy = getInterval(root2, suf);
if (xx.first > xx.second || yy.first > yy.second) {
continue;
}
int x1 = xx.first, x2 = xx.second;
int y1 = yy.first, y2 = yy.second;
questions.push_back({x2, y2, +1, iq});
questions.push_back({x2, y1 - 1, -1, iq});
questions.push_back({x1 - 1, y2, -1, iq});
questions.push_back({x1 - 1, y1 - 1, +1, iq});
}
/// sort(bboxes.begin(), bboxes.end());
for (auto &question : questions) {
int sol = 0;
for (int i = 1; i <= n; i++) {
sol += (x[i] <= question.x && y[i] <= question.y);
}
sol *= question.coef;
solPrint[question.index] += sol;
}
for (int iq = 1; iq <= q; iq++) {
cout << solPrint[iq] << "\n";
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:118:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen ("input", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
155408 KB |
Output is correct |
2 |
Correct |
407 ms |
147416 KB |
Output is correct |
3 |
Correct |
325 ms |
153516 KB |
Output is correct |
4 |
Correct |
432 ms |
146052 KB |
Output is correct |
5 |
Correct |
479 ms |
191692 KB |
Output is correct |
6 |
Correct |
537 ms |
194516 KB |
Output is correct |
7 |
Correct |
66 ms |
1480 KB |
Output is correct |
8 |
Correct |
417 ms |
113036 KB |
Output is correct |
9 |
Correct |
425 ms |
94868 KB |
Output is correct |
10 |
Correct |
170 ms |
92044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1571 ms |
5468 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
207 ms |
155408 KB |
Output is correct |
9 |
Correct |
407 ms |
147416 KB |
Output is correct |
10 |
Correct |
325 ms |
153516 KB |
Output is correct |
11 |
Correct |
432 ms |
146052 KB |
Output is correct |
12 |
Correct |
479 ms |
191692 KB |
Output is correct |
13 |
Correct |
537 ms |
194516 KB |
Output is correct |
14 |
Correct |
66 ms |
1480 KB |
Output is correct |
15 |
Correct |
417 ms |
113036 KB |
Output is correct |
16 |
Correct |
425 ms |
94868 KB |
Output is correct |
17 |
Correct |
170 ms |
92044 KB |
Output is correct |
18 |
Execution timed out |
1571 ms |
5468 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |