Submission #1158787

#TimeUsernameProblemLanguageResultExecution timeMemory
1158787CodeLakVNRegions (IOI09_regions)C++20
0 / 100
74 ms31496 KiB
#include <bits/stdc++.h>
using namespace std;

#define task "REGIONS"
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)

const int N = (int)2e5 + 5;
const int BLOCK = 450;

int numNode, numQuery, numRegions;
int region[N];
vector<int> adj[N];

int timer = 0;
int in[N], out[N];

void dfs(int u, int p) {
    in[u] = ++timer;
    for (int v : adj[u]) if (v != p) 
        dfs(v, u);
    out[u] = timer;
}

vector<pair<int, int>> employee[N];
int counter[N], id[N];
int ans[BLOCK][25005];

void solve(int r1, int r2) {
    int res = 0;
    for (auto [t, u] : employee[r1]) {
        if (u == 0) break;
        res += upper_bound(employee[r2].begin(), employee[r2].end(), make_pair(out[u], N)) - upper_bound(employee[r2].begin(), employee[r2].end(), make_pair(in[u], N)); 
    }
    cout << res << "\n";
} 

int main() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> numNode >> numRegions >> numQuery;
    cin >> region[1];
    FOR(i, 2, numNode) {
        int p;
        cin >> p >> region[i];
        adj[p].push_back(i);
    }

    dfs(1, -1);
    FOR(i, 1, numNode) {
        employee[region[i]].push_back({in[i], i});
    }
    int cnt = 0;
    FOR(r, 1, numRegions) {
        sort(employee[r].begin(), employee[r].end());
        int m = employee[r].size();
        if (m < BLOCK) continue;
        id[r] = ++cnt;
        FOR(i, 1, numNode) counter[i] = 0;
        for (auto [t, u] : employee[r]) {
            counter[in[u]]++;
            counter[out[u] + 1]--;
        }
        FOR(i, 1, numNode) counter[i] += counter[i - 1];
        FOR(other, 1, numRegions) if (other != r) {
            for (auto [t, u] : employee[other]) ans[cnt][other] += counter[t];
        }
    }
    FOR(r, 1, numRegions) employee[r].push_back({(int)1e9, 0});

    while (numQuery--) {
        int r1, r2;
        cin >> r1 >> r2;
        if ((int)employee[r1].size() >= BLOCK) cout << ans[id[r1]][r2] << "\n";
        else solve(r1, r2);
    }


    return 0;
}

Compilation message (stderr)

regions.cpp: In function 'int main()':
regions.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...