Submission #493211

# Submission time Handle Problem Language Result Execution time Memory
493211 2021-12-10T11:30:11 Z alextodoran Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
190 ms 175692 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
 
**/
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
const int N_MAX = 100000;
const int M_MAX = 100000;
const int XY_MAX = 2000000 * 3;
 
const int MOD = (int) 1e9 + 7;
 
int N, M;
 
string S[N_MAX];
string P[M_MAX], Q[M_MAX];
 
char chars[] = {'A', 'C', 'G', 'U'};
int encrypt[CHAR_MAX];
 
struct TrieNode {
    TrieNode* son[4];
    vector <pair <int, int>*> addresses;
    TrieNode () {
        for (int enc = 0; enc < 4; enc++) {
            son[enc] = NULL;
        }
        addresses.clear();
    }
};
 
void insertString (TrieNode* node, pair <int, int>* addr, string &str, string::iterator it) {
    if (it == str.end()) {
        node->addresses.push_back(addr);
        return;
    }
    int enc = encrypt[(int) *it];
    if (node->son[enc] == NULL) {
        node->son[enc] = new TrieNode;
    }
    insertString(node->son[enc], addr, str, next(it));
}
void insertString (TrieNode* root, pair <int, int>* addr, string &str) {
    insertString(root, addr, str, str.begin());
}
 
void linearize (TrieNode* node, int &curr) {
    curr++;
    int L = curr;
    for (int enc = 0; enc < 4; enc++) {
        if (node->son[enc] != NULL) {
            linearize(node->son[enc], curr);
        }
    }
    int R = curr;
    for (pair <int, int>* addr : node->addresses) {
        *addr = make_pair(L, R);
    }
}
void linearize (TrieNode* root) {
    int curr = 0;
    linearize(root, curr);
}
 
pair <int, int> SPrange[N_MAX];
pair <int, int> SQrange[N_MAX];
pair <int, int> Prange[N_MAX];
pair <int, int> Qrange[N_MAX];
 
TrieNode* rootP = new TrieNode;
TrieNode* rootQ = new TrieNode;
 
struct Point {
    int x, y;
};
bool operator < (const Point &p1, const Point &p2) {
    return p1.x < p2.x;
}
 
struct Query {
    Point p;
    int* addr;
    int sgn;
};
bool operator < (const Query &q1, const Query &q2) {
    return q1.p < q2.p;
}
 
Point points[N_MAX];
Query queries[M_MAX * 4];
 
int answer[M_MAX];
 
int Fen[XY_MAX + 2];
 
void updateFen (int y) {
    for (int i = y; i <= XY_MAX; i += i & -i) {
        Fen[i]++;
    }
}
int queryFen (int y) {
    int cnt = 0;
    for (int i = y; i >= 1; i -= i & -i) {
        cnt += Fen[i];
    }
    return cnt;
}
 
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    for (int enc = 0; enc < 4; enc++) {
        encrypt[(int) chars[enc]] = enc;
    }
 
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        cin >> S[i];
    }
    for (int i = 0; i < M; i++) {
        cin >> P[i] >> Q[i];
    }
 
    for (int i = 0; i < N; i++) {
        insertString(rootP, &SPrange[i], S[i]);
        reverse(S[i].begin(), S[i].end());
        insertString(rootQ, &SQrange[i], S[i]);
    }
    for (int i = 0; i < M; i++) {
        insertString(rootP, &Prange[i], P[i]);
        reverse(Q[i].begin(), Q[i].end());
        insertString(rootQ, &Qrange[i], Q[i]);
    }
    linearize(rootP);
    linearize(rootQ);
 
    for (int i = 0; i < N; i++) {
        points[i] = Point{SPrange[i].first, SQrange[i].first};
    }
    for (int i = 0; i < M; i++) {
        queries[i * 4 + 0] = Query{Point{Prange[i].second,     Qrange[i].second},     &answer[i], +1};
        queries[i * 4 + 1] = Query{Point{Prange[i].first - 1,  Qrange[i].second},     &answer[i], -1};
        queries[i * 4 + 2] = Query{Point{Prange[i].second,     Qrange[i].first - 1},  &answer[i], -1};
        queries[i * 4 + 3] = Query{Point{Prange[i].first - 1,  Qrange[i].first - 1},  &answer[i], +1};
    }
    sort(points, points + N);
    sort(queries, queries + M * 4);
 
    for (int i = 0, j = -1; i < M * 4; i++) {
        while (j + 1 < N && points[j + 1].x <= queries[i].p.x) {
            j++;
            updateFen(points[j].y);
        }
        *queries[i].addr += queries[i].sgn * queryFen(queries[i].p.y);
    }
 
    for (int i = 0; i < M; i++) {
        cout << answer[i] << "\n";
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 5 ms 9804 KB Output is correct
3 Correct 5 ms 9804 KB Output is correct
4 Correct 5 ms 9804 KB Output is correct
5 Correct 5 ms 9804 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 5 ms 9804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 144860 KB Output is correct
2 Correct 150 ms 143060 KB Output is correct
3 Correct 153 ms 140612 KB Output is correct
4 Correct 162 ms 134776 KB Output is correct
5 Correct 190 ms 173188 KB Output is correct
6 Correct 186 ms 175692 KB Output is correct
7 Correct 47 ms 21968 KB Output is correct
8 Correct 157 ms 115136 KB Output is correct
9 Correct 143 ms 100192 KB Output is correct
10 Correct 101 ms 92872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 18928 KB Output is correct
2 Correct 28 ms 15692 KB Output is correct
3 Correct 36 ms 17376 KB Output is correct
4 Correct 27 ms 15684 KB Output is correct
5 Correct 29 ms 15692 KB Output is correct
6 Correct 37 ms 17304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 5 ms 9804 KB Output is correct
3 Correct 5 ms 9804 KB Output is correct
4 Correct 5 ms 9804 KB Output is correct
5 Correct 5 ms 9804 KB Output is correct
6 Correct 6 ms 9804 KB Output is correct
7 Correct 5 ms 9804 KB Output is correct
8 Correct 160 ms 144860 KB Output is correct
9 Correct 150 ms 143060 KB Output is correct
10 Correct 153 ms 140612 KB Output is correct
11 Correct 162 ms 134776 KB Output is correct
12 Correct 190 ms 173188 KB Output is correct
13 Correct 186 ms 175692 KB Output is correct
14 Correct 47 ms 21968 KB Output is correct
15 Correct 157 ms 115136 KB Output is correct
16 Correct 143 ms 100192 KB Output is correct
17 Correct 101 ms 92872 KB Output is correct
18 Correct 36 ms 18928 KB Output is correct
19 Correct 28 ms 15692 KB Output is correct
20 Correct 36 ms 17376 KB Output is correct
21 Correct 27 ms 15684 KB Output is correct
22 Correct 29 ms 15692 KB Output is correct
23 Correct 37 ms 17304 KB Output is correct
24 Correct 155 ms 128540 KB Output is correct
25 Correct 167 ms 133352 KB Output is correct
26 Correct 140 ms 125796 KB Output is correct
27 Correct 144 ms 121088 KB Output is correct
28 Correct 145 ms 52416 KB Output is correct
29 Correct 84 ms 31084 KB Output is correct