답안 #738486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
738486 2023-05-08T20:37:02 Z lorenzoferrari Regions (IOI09_regions) C++17
95 / 100
8000 ms 34024 KB
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
using LL = long long;

static constexpr int SQ = 500;

struct Segtree {
    int n;
    vector<int> t;
    Segtree(int n) : n(n), t(2*n) {}
    void update(int p, int v) {
        for (t[p += n] += v; p > 1; p >>= 1) {
            t[p >> 1] = t[p] + t[p ^ 1];
        }
    }
    int query(int l, int r) {
        int ans = 0;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ans += t[l++];
            if (r & 1) ans += t[--r];
        }
        return ans;
    }
};

int main() {
    int n; cin >> n;
    int r; cin >> r;
    int q; cin >> q;
    vector<int> h(n), p(n), f(r);
    vector<vector<int>> adj(n);
    cin >> h[0]; ++f[--h[0]];
    for (int i = 1; i < n; ++i) {
        cin >> p[i] >> h[i];
        --p[i], --h[i];
        adj[p[i]].push_back(i);
        ++f[h[i]];
    }
    vector<int> ff;
    vector<int> fi(r, -1);
    for (int i = 0; i < r; ++i) {
        if (f[i] >= SQ) {
            fi[i] = ff.size();
            ff.push_back(i);
        }
    }
    int nn = ff.size();

    vector<int> in(n), out(n), ord;
    { // linearize the tree
        int t = 0;
        auto dfs = [&](auto&& self, int v) -> void {
            in[v] = t++;
            ord.push_back(v);
            for (int u : adj[v]) {
                self(self, u);
            }
            out[v] = t;
        };
        dfs(dfs, 0);
    }

    vector<vector<int>> vs(r);
    for (int v : ord) {
        vs[h[v]].push_back(v);
    }

    vector<vector<LL>> sub(r, vector<LL>(nn));
    vector<vector<LL>> anc(r, vector<LL>(nn));
    {
        auto dfs = [&](auto&& self, int v, int i, int acc = 0) -> int {
            int cnt = 0;
            for (int u : adj[v]) {
                cnt += self(self, u, i, acc + (h[v] == ff[i]));
            }
            sub[h[v]][i] += cnt;
            anc[h[v]][i] += acc;
            return cnt + (h[v] == ff[i]);
        };
        for (int i = 0; i < nn; ++i) {
            dfs(dfs, 0, i);
        }
    }

    Segtree st(n);
    for (int r1, r2; q--;) {
        cin >> r1 >> r2;
        --r1, --r2;
        if (f[r2] >= SQ) {
            cout << sub[r1][fi[r2]] << endl;
        } else if (f[r1] >= SQ) {
            cout << anc[r2][fi[r1]] << endl;
        } else {
            LL ans = 0;
            for (int v : vs[r2]) {
                st.update(in[v], 1);
            }
            for (int u : vs[r1]) {
                ans += st.query(in[u]+1, out[u]);
            }
            for (int v : vs[r2]) {
                st.update(in[v], -1);
            }
            cout << ans << endl;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 2 ms 208 KB Output is correct
4 Correct 5 ms 208 KB Output is correct
5 Correct 9 ms 336 KB Output is correct
6 Correct 22 ms 336 KB Output is correct
7 Correct 35 ms 336 KB Output is correct
8 Correct 41 ms 464 KB Output is correct
9 Correct 33 ms 848 KB Output is correct
10 Correct 79 ms 1232 KB Output is correct
11 Correct 152 ms 1640 KB Output is correct
12 Correct 203 ms 2232 KB Output is correct
13 Correct 193 ms 2128 KB Output is correct
14 Correct 292 ms 2996 KB Output is correct
15 Correct 643 ms 5064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1890 ms 7420 KB Output is correct
2 Correct 1482 ms 6536 KB Output is correct
3 Correct 4086 ms 9632 KB Output is correct
4 Correct 388 ms 3272 KB Output is correct
5 Correct 569 ms 4736 KB Output is correct
6 Correct 1339 ms 7628 KB Output is correct
7 Correct 2802 ms 7184 KB Output is correct
8 Correct 3003 ms 13448 KB Output is correct
9 Correct 4659 ms 15228 KB Output is correct
10 Execution timed out 8016 ms 20104 KB Time limit exceeded
11 Correct 5899 ms 17792 KB Output is correct
12 Correct 1671 ms 19420 KB Output is correct
13 Correct 2834 ms 19528 KB Output is correct
14 Correct 3302 ms 26432 KB Output is correct
15 Correct 6424 ms 24672 KB Output is correct
16 Correct 7026 ms 29552 KB Output is correct
17 Correct 6259 ms 34024 KB Output is correct