Submission #996293

# Submission time Handle Problem Language Result Execution time Memory
996293 2024-06-10T11:48:18 Z phoenix Regions (IOI09_regions) C++17
100 / 100
3496 ms 63560 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200200;
const int R = 25025;
const int B = 1080;


int T;
int tin[N];
int tout[N];
vector<int> g[N];
int rg[N];

int m;
vector<int> heavy;
vector<int> ver[R];
vector<int> seg[R];

void precalc(int s) {
    tin[s] = ++T;
    seg[rg[s]].push_back(tin[s]);
    for (int to : g[s]) 
        precalc(to);
    tout[s] = ++T;
}


vector<vector<ll>> heavy_ans;
vector<ll> light_ans[N];

int lst[R];
int cnt[N];

bool isheavy(int r) {
    return ((int)seg[r].size() > B);
}

void dfs(int s) {
    for (int i = 0; i < m; i++) {
        int c = heavy[i];
        heavy_ans[i][rg[s]] += cnt[lst[c]];
    }

    int x = lst[rg[s]];
    cnt[s] = cnt[lst[rg[s]]] + 1;
    lst[rg[s]] = s;

    light_ans[s].resize(m);
    if (isheavy(rg[s])) {
        int c = lower_bound(heavy.begin(), heavy.end(), rg[s]) - heavy.begin();
        light_ans[s][c]++;
    }
    for (int to : g[s]) {
        dfs(to);
        for (int i = 0; i < m; i++)  
            light_ans[s][i] += light_ans[to][i];
    } 
    lst[rg[s]] = x;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, regions, q;
    cin >> n >> regions >> q;
    for (int i = 1; i <= n; i++) {
        int p;
        if (i > 1) {
            cin >> p;
            g[p].push_back(i);
        }
        cin >> rg[i];
        ver[rg[i]].push_back(i);
    }  
    
    precalc(1);

    for (int i = 1; i <= regions; i++) {
        if (isheavy(i)) {
            heavy.push_back(i);
            heavy_ans.push_back(vector<ll>(regions + 1));
            m++;
        }
        sort(seg[i].begin(), seg[i].end());
    }

    dfs(1);
    while (q--) {
        int r1, r2;
        cin >> r1 >> r2;
        int ans = 0;
        if (isheavy(r1)) {
            int c = lower_bound(heavy.begin(), heavy.end(), r1) - heavy.begin();
            ans = heavy_ans[c][r2];
        } else if (isheavy(r2)) { 
                int c = lower_bound(heavy.begin(), heavy.end(), r2) - heavy.begin();
                for (int v : ver[r1]) {
                    ans += light_ans[v][c];
                }
        } else {
            for (int v : ver[r1]) {
                int l, r;
                l = upper_bound(seg[r2].begin(), seg[r2].end(), tin[v]) - seg[r2].begin();
                r = upper_bound(seg[r2].begin(), seg[r2].end(), tout[v]) - seg[r2].begin();
                ans += max(r - l, 0);
            }
        }
        cout << ans << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 3 ms 12632 KB Output is correct
3 Correct 3 ms 12632 KB Output is correct
4 Correct 4 ms 12632 KB Output is correct
5 Correct 5 ms 12632 KB Output is correct
6 Correct 10 ms 12888 KB Output is correct
7 Correct 16 ms 12632 KB Output is correct
8 Correct 19 ms 12888 KB Output is correct
9 Correct 27 ms 13400 KB Output is correct
10 Correct 55 ms 13144 KB Output is correct
11 Correct 80 ms 13400 KB Output is correct
12 Correct 103 ms 14168 KB Output is correct
13 Correct 131 ms 13656 KB Output is correct
14 Correct 204 ms 14168 KB Output is correct
15 Correct 184 ms 18008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1192 ms 19800 KB Output is correct
2 Correct 1183 ms 22156 KB Output is correct
3 Correct 2008 ms 28284 KB Output is correct
4 Correct 195 ms 14168 KB Output is correct
5 Correct 254 ms 16472 KB Output is correct
6 Correct 1119 ms 15448 KB Output is correct
7 Correct 1411 ms 16472 KB Output is correct
8 Correct 1062 ms 23384 KB Output is correct
9 Correct 1911 ms 22096 KB Output is correct
10 Correct 3496 ms 29008 KB Output is correct
11 Correct 3391 ms 21300 KB Output is correct
12 Correct 1038 ms 28884 KB Output is correct
13 Correct 1510 ms 29568 KB Output is correct
14 Correct 1873 ms 46636 KB Output is correct
15 Correct 2577 ms 35700 KB Output is correct
16 Correct 2489 ms 44888 KB Output is correct
17 Correct 2350 ms 63560 KB Output is correct