Submission #763764

# Submission time Handle Problem Language Result Execution time Memory
763764 2023-06-22T20:27:46 Z izanbf Regions (IOI09_regions) C++14
90 / 100
8000 ms 88428 KB
#include <bits/stdc++.h>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;

const int BIG = 500;
vi r, tin, tout, big_id;
vvi adj, tins, touts, by_r;
vector<vector<long long>> dp;
int timer;

void euler(int u, int p) {
    tin[u] = timer++;
    tins[r[u]].push_back(tin[u]);
    for (int v : adj[u]) {
        if (v != p) euler(v, u);
    }
    tout[u] = timer++;
    touts[r[u]].push_back(tout[u]);
}

int count_in_range(const vi& v, int l, int r) {
    if (v.empty()) return 0;
    int i = upper_bound(v.begin(), v.end(), r) - v.begin();
    int j = upper_bound(v.begin(), v.end(), l) - v.begin();
    // cerr << "$$" << 1+rg << " " << l << " " << r << " " << j << " " << i << " " << i - j << endl;
    return i - j;
}

void precompute(int rg, int p, int u) {
    // cerr << "%" << 1+rg << " " << 1+u << endl;
    int rid = big_id[rg];
    cerr << 1+rg << " RID " << rid << " " << u << endl;
    dp[rid][u] = (r[u] == rg);
    for (int v : adj[u]) {
        if (v != p) {
            precompute(rg, u, v);
            dp[rid][u] += dp[rid][v];
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int N, R, Q;
    cin >> N >> R >> Q;

    r = tin = tout = vi(N);
    big_id = vi(R);
    adj = vvi(N);
    tins = touts = by_r = vvi(R);

    vi p(N, -1);

    cin >> r[0]; --r[0];
    by_r[r[0]].push_back(0);

    for (int i = 1; i < N; ++i) {
        cin >> p[i] >> r[i]; --p[i], --r[i];
        by_r[r[i]].push_back(i);
        adj[i].push_back(p[i]);
        adj[p[i]].push_back(i);
    }

    int bigs = 0;
    for (int rg = 0; rg < R; ++rg) {
        if (by_r[rg].size() >= BIG) big_id[rg] = bigs++;
    }

    dp = vector<vector<long long>>(bigs, vector<long long>(N));
    for (int rg = 0; rg < R; ++rg) {
        if (by_r[rg].size() >= BIG) precompute(rg, 0, 0);
    }

    timer = 1;
    euler(0, 0);

    // for (int x : tins[3-1]) cerr << x << " "; cerr << endl;

    // for (int i = 0; i < N; ++i) {
    //     cerr << 1+i << " " << 1+r[i] << "  " << tin[i] << " " << tout[i] << endl;
    // }

    // for (int i = 1; i < N; ++i) {
    //     cerr << 1+i << "(" << 1+r[i] << ")" 
    //     << " " << 1+p[i] << "(" << 1+r[p[i]] << ")" << endl;
    // }
    // cerr << endl << endl;

    map<pair<int,int>,long long> mem;

    while (Q--) {
        int r1, r2;
        cin >> r1 >> r2; --r1, --r2;
        // cerr << 1+r1 << " " << 1+r2 << endl;
        if (by_r[r1].size() < BIG and by_r[r2].size() >= BIG) { // r1 small r2 big
            long long cnt = 0;
            int rid = big_id[r2];
            for (int u : by_r[r1]) {
                cnt += dp[rid][u];
            }
            cout << cnt << endl;
        }
        else {
            if (not mem.count({r1,r2})) {
                long long cnt = 0;
                for (int u : by_r[r2]) {
                    cnt += count_in_range(tins[r1], 0, tin[u])
                         - count_in_range(touts[r1], 0, tin[u]);
                }
                mem[{r1,r2}] = cnt;
            }
            cout << mem[{r1,r2}] << endl;
        }
    }
}




# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 2 ms 208 KB Output is correct
4 Correct 4 ms 336 KB Output is correct
5 Correct 6 ms 392 KB Output is correct
6 Correct 12 ms 484 KB Output is correct
7 Correct 24 ms 528 KB Output is correct
8 Correct 28 ms 712 KB Output is correct
9 Correct 27 ms 1356 KB Output is correct
10 Correct 83 ms 1844 KB Output is correct
11 Correct 124 ms 2200 KB Output is correct
12 Correct 149 ms 3032 KB Output is correct
13 Correct 155 ms 3404 KB Output is correct
14 Correct 245 ms 3780 KB Output is correct
15 Correct 279 ms 7532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6359 ms 30576 KB Output is correct
2 Correct 7364 ms 33600 KB Output is correct
3 Correct 7252 ms 35716 KB Output is correct
4 Correct 283 ms 5168 KB Output is correct
5 Correct 351 ms 7716 KB Output is correct
6 Correct 6309 ms 32724 KB Output is correct
7 Correct 681 ms 9308 KB Output is correct
8 Correct 2989 ms 27364 KB Output is correct
9 Correct 2578 ms 25212 KB Output is correct
10 Correct 3909 ms 34124 KB Output is correct
11 Correct 4552 ms 33332 KB Output is correct
12 Correct 3400 ms 32244 KB Output is correct
13 Correct 4257 ms 35420 KB Output is correct
14 Execution timed out 8096 ms 75748 KB Time limit exceeded
15 Correct 5656 ms 46436 KB Output is correct
16 Correct 5390 ms 55248 KB Output is correct
17 Execution timed out 8096 ms 88428 KB Time limit exceeded