답안 #1071892

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1071892 2024-08-23T12:10:40 Z ArthuroWich Regions (IOI09_regions) C++17
85 / 100
8000 ms 30052 KB
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300005], reg[25005], regst[25005], calc[25005];
int st[300005], en[300005], timer = 0;
void dfs(int i, int p) {
    st[i] = timer;
    timer++;
    for (int j : adj[i]) {
        if (j == p) {
            continue;
        }
        dfs(j, i);
    }
    en[i] = timer;
}
void solve() {
    int n, r, q;
    cin >> n >> r >> q;
    for (int i = 1; i <= n; i++) {
        int s, h;
        if (i == 1) {
            cin >> h;
            reg[h].push_back(i);
        } else {
            cin >> s >> h;
            reg[h].push_back(i);
            adj[s].push_back(i);
            adj[i].push_back(s);
        }
    }
    dfs(1, -1);
    for (int i = 1; i <= r; i++) {
        for (int j : reg[i]) {
            regst[i].push_back(st[j]);
        }
        sort(regst[i].begin(), regst[i].end());
    }
    for (int a = 1; a <= r; a++) {
        if (reg[a].size() > 450) {
            calc[a].resize(r+1, 0);
            for (int b = 1; b <= r; b++) {
                if (a == b) {
                    continue;   
                }
                for (int i : reg[a]) {
                    int l, r;
                    l = upper_bound(regst[b].begin(), regst[b].end(), st[i]) - regst[b].begin();
                    r = lower_bound(regst[b].begin(), regst[b].end(), en[i]) - regst[b].begin();
                    calc[a][b] += r-l;
                }
            }
        }
    }
    while(q--) {
        int a, b, ans = 0;
        cin >> a >> b;
        if (reg[a].size() <= 450) {
            for (int i : reg[a]) {
                int l, r;
                l = upper_bound(regst[b].begin(), regst[b].end(), st[i]) - regst[b].begin();
                r = lower_bound(regst[b].begin(), regst[b].end(), en[i]) - regst[b].begin();
                ans += r-l;
            }
        } else {
            ans += calc[a][b];
        }
        cout << ans << endl;
    }
}
int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    t = 1;
    while(t--) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11352 KB Output is correct
2 Correct 2 ms 11352 KB Output is correct
3 Correct 3 ms 11352 KB Output is correct
4 Correct 4 ms 11440 KB Output is correct
5 Correct 7 ms 11352 KB Output is correct
6 Correct 10 ms 11352 KB Output is correct
7 Correct 16 ms 11352 KB Output is correct
8 Correct 16 ms 11352 KB Output is correct
9 Correct 27 ms 11864 KB Output is correct
10 Correct 45 ms 11864 KB Output is correct
11 Correct 82 ms 12120 KB Output is correct
12 Correct 102 ms 12372 KB Output is correct
13 Correct 126 ms 12376 KB Output is correct
14 Correct 222 ms 12888 KB Output is correct
15 Correct 190 ms 15216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1768 ms 15704 KB Output is correct
2 Correct 2053 ms 14936 KB Output is correct
3 Correct 2710 ms 16680 KB Output is correct
4 Correct 184 ms 12888 KB Output is correct
5 Correct 273 ms 14400 KB Output is correct
6 Correct 2737 ms 15840 KB Output is correct
7 Correct 2000 ms 15696 KB Output is correct
8 Correct 4318 ms 23284 KB Output is correct
9 Correct 1896 ms 19460 KB Output is correct
10 Correct 6712 ms 29064 KB Output is correct
11 Correct 3479 ms 21324 KB Output is correct
12 Correct 5812 ms 20188 KB Output is correct
13 Correct 5860 ms 20956 KB Output is correct
14 Execution timed out 8007 ms 21672 KB Time limit exceeded
15 Execution timed out 8082 ms 24304 KB Time limit exceeded
16 Correct 7396 ms 29808 KB Output is correct
17 Execution timed out 8007 ms 30052 KB Time limit exceeded