Submission #607155

# Submission time Handle Problem Language Result Execution time Memory
607155 2022-07-26T12:33:21 Z jhwest2 Regions (IOI09_regions) C++14
95 / 100
8000 ms 73788 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 202020;
const int MAXR = 25252;
typedef long long ll;

int n, r, q, R[MAXN], P[MAXN], X[MAXN];
vector<int> G[MAXN];
vector<int> V[MAXR], W[MAXR];
map<pair<int, int>, ll> M;

struct PST {
    vector<int> tree, l, r;

    int new_node() {
        tree.push_back(0);
        l.push_back(0);
        r.push_back(0);
        return (int)tree.size() - 1;
    }
    int init(int lo, int hi) {
        int idx = new_node();
        if (lo != hi) {
            int ll = init(lo, lo + hi >> 1);
            int rr = init(1 + (lo + hi >> 1), hi);
            l[idx] = ll;
            r[idx] = rr;
        }
        return idx;
    }
    int update(int s, int x, int lo, int hi, int p_idx) {
        int idx = new_node();
        if (lo == hi) {
            tree[idx] = tree[p_idx] + x; return idx;
        }
        int mid = (lo + hi >> 1);
        int ll = (s <= mid) ? update(s, x, lo, mid, l[p_idx]) : l[p_idx];
        int rr = (s > mid) ? update(s, x, mid + 1, hi, r[p_idx]) : r[p_idx];
        l[idx] = ll;
        r[idx] = rr;
        return idx;
    }
    int get() { return tree.size(); }
    int query(int s, int lo, int hi, int idx) {
        if (lo == hi) return tree[idx];
        int mid = (lo + hi >> 1);
        if (s <= mid) return query(s, lo, mid, l[idx]);
        else return query(s, mid + 1, hi, r[idx]);
    }
} PST;

int p, LL[MAXN], RR[MAXN];
void dfs(int v) {
    LL[v] = ++p;
    for (int x : G[v]) dfs(x);
    RR[v] = p;
}
int main() {
    cout.tie(0); ios_base::sync_with_stdio(0);
    cin >> n >> r >> q;
    cin >> R[1]; V[R[1]].push_back(1);
    for (int i = 2; i <= n; i++) {
        cin >> P[i] >> R[i];
        G[P[i]].push_back(i);
        V[R[i]].push_back(i);
    }

    PST.init(1, r);
    for (int i = 1; i <= n; i++) {
        X[i] = PST.get();
        PST.update(R[i], 1, 1, r, X[P[i]]);
    }

    dfs(1);
    for (int i = 1; i <= n; i++) {
        W[R[i]].push_back(LL[i]);
    }
    for (int i = 1; i <= r; i++) {
        sort(W[i].begin(), W[i].end());
    }
    while (q--) {
        int a, b; cin >> a >> b;
        if (M.find({ a, b }) != M.end()) {
            cout << M[{a, b}] << endl;
            continue;
        }
        ll ans = 0;
        if (V[a].size() <= V[b].size()) {
            for (int x : V[a]) {
                ans += upper_bound(W[b].begin(), W[b].end(), RR[x]) -
                    lower_bound(W[b].begin(), W[b].end(), LL[x]);
            }
        }
        else {
            for (int x : V[b]) {
                ans += PST.query(a, 1, r, X[x]);
            }
        }
        M[{a, b}] = ans;
        cout << ans << endl;
    }
}

Compilation message

regions.cpp: In member function 'int PST::init(int, int)':
regions.cpp:25:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |             int ll = init(lo, lo + hi >> 1);
      |                               ~~~^~~~
regions.cpp:26:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |             int rr = init(1 + (lo + hi >> 1), hi);
      |                                ~~~^~~~
regions.cpp: In member function 'int PST::update(int, int, int, int, int)':
regions.cpp:37:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |         int mid = (lo + hi >> 1);
      |                    ~~~^~~~
regions.cpp: In member function 'int PST::query(int, int, int, int)':
regions.cpp:47:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |         int mid = (lo + hi >> 1);
      |                    ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6224 KB Output is correct
2 Correct 4 ms 6224 KB Output is correct
3 Correct 5 ms 6224 KB Output is correct
4 Correct 8 ms 6224 KB Output is correct
5 Correct 11 ms 6384 KB Output is correct
6 Correct 20 ms 6508 KB Output is correct
7 Correct 36 ms 6676 KB Output is correct
8 Correct 44 ms 6836 KB Output is correct
9 Correct 64 ms 7540 KB Output is correct
10 Correct 93 ms 8436 KB Output is correct
11 Correct 135 ms 9396 KB Output is correct
12 Correct 153 ms 10672 KB Output is correct
13 Correct 226 ms 11044 KB Output is correct
14 Correct 298 ms 11916 KB Output is correct
15 Correct 379 ms 15040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1114 ms 20572 KB Output is correct
2 Correct 1518 ms 20432 KB Output is correct
3 Correct 2567 ms 26188 KB Output is correct
4 Correct 428 ms 14368 KB Output is correct
5 Correct 478 ms 17408 KB Output is correct
6 Correct 618 ms 20004 KB Output is correct
7 Correct 924 ms 23924 KB Output is correct
8 Correct 1586 ms 35764 KB Output is correct
9 Correct 3491 ms 51228 KB Output is correct
10 Execution timed out 8036 ms 61272 KB Time limit exceeded
11 Correct 7981 ms 66256 KB Output is correct
12 Correct 2210 ms 57180 KB Output is correct
13 Correct 3140 ms 59160 KB Output is correct
14 Correct 3647 ms 63144 KB Output is correct
15 Correct 6543 ms 70336 KB Output is correct
16 Correct 6913 ms 73788 KB Output is correct
17 Correct 5878 ms 71316 KB Output is correct