Submission #944371

#TimeUsernameProblemLanguageResultExecution timeMemory
944371FucKanhRegions (IOI09_regions)C++14
0 / 100
1280 ms43244 KiB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>

using namespace std;

const int maxn = 2e5 + 2;
const int maxr = 25002;

int cnt[450][maxr], c[maxr], tin[maxn], tout[maxn], t, h[maxn], id[maxn], sz[maxr];
vector<int> a[maxn], in[maxn], out[maxn];

void dfs(int u, int pa = -1) {
    tin[u] = ++t;
    if (!c[h[u]]) c[h[u]] = u;
    for (int i = 0; i < a[u].size(); i++) {
        int v = a[u][i];
        if (v == pa) continue;
        dfs(v, u);
    }
//    cerr << "out " << u << " " << h[u] << " : " << c[h[u]] << endl;
    tout[u] = t;
    if (c[h[u]]==u) {
        in[h[u]].push_back(tin[u]);
        out[h[u]].push_back(tout[u]);
        c[h[u]] = 0;
    }
}

void dfsc(int u, int check, int pa = -1) {
    if (h[u] == check && c[check] == 0) c[check] = u;
    for (int i = 0; i < a[u].size(); i++) {
        int v = a[u][i];
        if (v == pa) continue;
        dfsc(v, check, u);
    }
    if (c[check] == u) c[check] = 0;
    if (c[check]) cnt[id[check]][h[u]]++;
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    int n,r,q; cin >> n >> r >> q;
    cin >> h[1];
    for (int i = 2; i <= n; i++) {
        int x; cin >> x >> h[i];
        a[x].push_back(i);
        sz[h[i]]++;
    }
    for (int i = 1; i <= r; i++) {
        in[i].push_back(0);
        out[i].push_back(0);
    }
    dfs(1);

//    for (int i = 1; i <= r; i++) {
//        cerr << i << ": " << endl;
//        for (int j = 1; j < in[i].size(); j++) {
//            cerr << in[i][j] << " " << out[i][j] << endl;
//        }
//    }

    int dem = 0;
    for (int i = 1; i <= r; i++) {
        if (sz[i] * sz[i] >= n) {
            id[i] = ++dem;
            c[i] = 0;
            dfsc(1, i);
//            for (int j = 1; j <= r; j++) {
//                cerr << cnt[dem][j] << " ";
//            } cerr << endl;
        }
    }

    for (int i =0; i < q; i++) {
        int r1, r2; cin >> r1 >> r2;
        if (sz[r1] * sz[r1] >= n) {
            cout << cnt[id[r1]][r2] << "\n" << flush;
        }
        else {
            int pos = 1;
            int ans = 0;
            for (int i = 1; i < in[r1].size() && pos < in[r2].size(); i++) {
                while (pos < in[r2].size() && in[r2][pos] < in[r1][i]) pos++;
                while (pos < in[r2].size() && in[r2][pos] >= in[r1][i] && out[r2][pos] <= out[r1][i]) ans++, pos++;
            }
            cout << ans << "\n" << flush;
        }
    }
    return 0;
}

Compilation message (stderr)

regions.cpp: In function 'void dfs(int, int)':
regions.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < a[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
regions.cpp: In function 'void dfsc(int, int, int)':
regions.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i = 0; i < a[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:83:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |             for (int i = 1; i < in[r1].size() && pos < in[r2].size(); i++) {
      |                             ~~^~~~~~~~~~~~~~~
regions.cpp:83:54: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |             for (int i = 1; i < in[r1].size() && pos < in[r2].size(); i++) {
      |                                                  ~~~~^~~~~~~~~~~~~~~
regions.cpp:84:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |                 while (pos < in[r2].size() && in[r2][pos] < in[r1][i]) pos++;
      |                        ~~~~^~~~~~~~~~~~~~~
regions.cpp:85:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |                 while (pos < in[r2].size() && in[r2][pos] >= in[r1][i] && out[r2][pos] <= out[r1][i]) ans++, pos++;
      |                        ~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...