# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1280080 | Aquarius | Regions (IOI09_regions) | C++20 | 941 ms | 39272 KiB |
#include<bits/stdc++.h>
#define int long long
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e5 + 5;
vector<int> E[N];
vector<int> can_st[N], can_ed[N];
int ans[2500][2500];
int val[N], st[N], ed[N];
int n, q, r, t = 0;
void dfs(int u, int p) {
st[u] = ++t;
can_st[val[u]].push_back(st[u]);
for(int v: E[u]) {
if(v==p) continue;
dfs(v, u);
}
ed[u] = ++t;
can_ed[val[u]].push_back(ed[u]);
}
int count_parent(int u, int c) {
// st[x] <= st[u]
// ed[x] <= st[u]
int a = upper_bound(all(can_st[c]), st[u]) - can_st[c].begin();
int b = upper_bound(all(can_ed[c]), st[u]) - can_ed[c].begin();
return a - b;
}
void solve() {
cin >> n >> r >> q;
for(int i=1; i<=n; i++) {
if(i != 1) {
int p; cin >> p;
E[p].push_back(i);
E[i].push_back(p);
}
cin >> val[i];
}
dfs(1, 0);
for(int i=1; i<=r; i++) sort(all(can_ed[i]));
for(int i=1; i<=n; i++) {
for(int color = 1; color <=r; color++) {
if(color == val[i]) continue;
ans[val[i]][color] += count_parent(i, color);
}
}
while(q--) {
int r1, r2; cin >> r1 >> r2;
cout<<ans[r2][r1] << endl;
}
}
/*
1
3
2
1
*/
signed main() {
if(fopen("input.txt", "r")) freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(false);cin.tie(0);
int T = 1;
// cin >> T;
while(T--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |