제출 #228809

#제출 시각아이디문제언어결과실행 시간메모리
228809osaaateiasavtnlRegions (IOI09_regions)C++14
65 / 100
8060 ms58180 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 5e5 + 7;
int n, R, q;
int color[N], par[N];
vector <int> tree[N], pos[N], who[N];
int l[N], r[N], ptr = 0;
void dfs(int u) {
    l[u] = ++ptr;
    pos[color[u]].app(l[u]);
    for (int v : tree[u]) {
        dfs(v);
    }   
    r[u] = ptr;
}   
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #endif
    cin >> n >> R >> q;
    cin >> color[1];
    for (int i = 2; i <= n; ++i) {
        cin >> par[i] >> color[i];
        tree[par[i]].app(i);
    }   
    for (int i = 1; i <= n; ++i)
        who[color[i]].app(i);
    dfs(1);
    while (q--) {
        int c1, c2;
        cin >> c1 >> c2;
        vector <int> &a = pos[c2];
        int ans = 0;
        for (int u : who[c1]) {
            ans += upper_bound(all(a), r[u]) - upper_bound(all(a), l[u]);
        }   
        cout << ans << endl;
        fflush(stdout);
    }   
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...