답안 #490604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490604 2021-11-28T09:45:56 Z dooompy Regions (IOI09_regions) C++17
35 / 100
3699 ms 131076 KB
#include "bits/stdc++.h"

using namespace std;

using ll = long long;

const int BLOCK = 450;

int region[200005];
vector<int> adj[200005];
int tin[200005], tout[200005];
vector<int> sorted[25005];
int ind[25005];
vector<int> largeRegions;
vector<vector<int>> par(BLOCK, vector<int>(25005));
vector<vector<int>> child(BLOCK, vector<int>(25005));
int timer = 1;

void dfs(int i) {
    tin[i] = timer++;
    sorted[region[i]].push_back(i);

    for (auto a : adj[i]) {
        dfs(a);
    }

    tout[i] = timer - 1;
}

void genPar(int cur, int original, int cnt) {
    par[ind[original]][region[cur]] += cnt;
    cnt += (region[cur] == original);
    for (auto a : par[cur]) {
        genPar(a, original, cnt);
    }
}

int genChild(int cur, int original) {
    int current = (region[cur] == original);

    for (auto a : par[cur]) {
        current += genChild(a, original);
    }

    child[ind[original]][region[cur]] += current;
    return current;
}

bool isAnc(int a, int b) {
    return tin[a] <= tin[b] && tout[b] <= tout[a];
}

int getAns(int reg1, int reg2) {
    int l = 0, r = 0;
    int ans = 0;
    stack<int> st;

    while (l < sorted[reg1].size() || r < sorted[reg2].size()) {
        if ((l < sorted[reg1].size()) && (r == sorted[reg2].size() || tin[sorted[reg1][l]] < tin[sorted[reg2][r]])) {
            while (st.size() && !isAnc(st.top(), sorted[reg1][l])) {
                st.pop();
            }

            st.push(sorted[reg1][l]);
            l++;
        } else {
            while (st.size() && !isAnc(st.top(), sorted[reg2][r])) {
                st.pop();
            }

            ans += st.size();
            r++;
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, r, q; cin >> n >> r >> q;
    cin >> region[1];

    for (int i = 2; i <= n; i++) {
        int s; cin >> s >> region[i];
        adj[s].push_back(i);
    }

    dfs(1);

    int idx = 0;

    for (int i = 1; i <= r; i++) {
        if (sorted[i].size() > BLOCK) {
            largeRegions.push_back(i);
            ind[i] = idx++;
        }
    }

    for (auto a : largeRegions) {
        genPar(1, a, 0);
        genChild(1, a);
    }

    for (int i = 0; i < q; i++) {
        int a, b; cin >> a >> b;

        if (sorted[a].size() <= BLOCK && sorted[b].size() <= BLOCK) {
            cout << getAns(a, b) << endl;
            continue;
        }

        if (sorted[a].size() > BLOCK) {
            cout << par[ind[a]][b] << endl;
        } else {
            cout << child[ind[b]][a] << endl;
        }
    }
}

Compilation message

regions.cpp: In function 'int getAns(int, int)':
regions.cpp:58:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     while (l < sorted[reg1].size() || r < sorted[reg2].size()) {
      |            ~~^~~~~~~~~~~~~~~~~~~~~
regions.cpp:58:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     while (l < sorted[reg1].size() || r < sorted[reg2].size()) {
      |                                       ~~^~~~~~~~~~~~~~~~~~~~~
regions.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         if ((l < sorted[reg1].size()) && (r == sorted[reg2].size() || tin[sorted[reg1][l]] < tin[sorted[reg2][r]])) {
      |              ~~^~~~~~~~~~~~~~~~~~~~~
regions.cpp:59:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         if ((l < sorted[reg1].size()) && (r == sorted[reg2].size() || tin[sorted[reg1][l]] < tin[sorted[reg2][r]])) {
      |                                           ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 93708 KB Output is correct
2 Correct 46 ms 93792 KB Output is correct
3 Correct 42 ms 93772 KB Output is correct
4 Correct 45 ms 93692 KB Output is correct
5 Correct 47 ms 93792 KB Output is correct
6 Correct 57 ms 93860 KB Output is correct
7 Correct 69 ms 93760 KB Output is correct
8 Correct 67 ms 93892 KB Output is correct
9 Correct 80 ms 94284 KB Output is correct
10 Correct 124 ms 94184 KB Output is correct
11 Correct 155 ms 94468 KB Output is correct
12 Correct 174 ms 94904 KB Output is correct
13 Correct 193 ms 94564 KB Output is correct
14 Correct 260 ms 95172 KB Output is correct
15 Correct 315 ms 97684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 90 ms 131076 KB Execution killed with signal 9
2 Runtime error 91 ms 131076 KB Execution killed with signal 9
3 Runtime error 83 ms 131076 KB Execution killed with signal 9
4 Correct 288 ms 95228 KB Output is correct
5 Correct 326 ms 96796 KB Output is correct
6 Runtime error 78 ms 131076 KB Execution killed with signal 9
7 Runtime error 78 ms 131076 KB Execution killed with signal 9
8 Runtime error 84 ms 131076 KB Execution killed with signal 9
9 Correct 2362 ms 101920 KB Output is correct
10 Runtime error 105 ms 131076 KB Execution killed with signal 9
11 Correct 3699 ms 101512 KB Output is correct
12 Runtime error 128 ms 131076 KB Execution killed with signal 9
13 Runtime error 127 ms 131076 KB Execution killed with signal 9
14 Runtime error 122 ms 131076 KB Execution killed with signal 9
15 Runtime error 113 ms 131076 KB Execution killed with signal 9
16 Runtime error 145 ms 131076 KB Execution killed with signal 9
17 Runtime error 113 ms 131076 KB Execution killed with signal 9