이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#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, K = 400;
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;
}
vector <int> cl[N], cr[N];
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);
for (int i = 1; i <= n; ++i) {
cl[color[i]].app(l[i]);
cr[color[i]].app(r[i]);
}
for (int i = 1; i <= R; ++i) {
sort(all(cl[i]));
sort(all(cr[i]));
}
map <ii, int> mem;
while (q--) {
int c1, c2;
cin >> c1 >> c2;
if (mem.find(mp(c1, c2)) != mem.end()) {
cout << mem[mp(c1, c2)] << endl;
continue;
}
int ans = 0;
if (who[c2].size() > K) {
vector <int> &a = pos[c2];
for (int u : who[c1]) {
ans += upper_bound(all(a), r[u]) - upper_bound(all(a), l[u]);
}
}
else {
for (int u : who[c2]) {
ans += (lower_bound(all(cl[c1]), l[u]) - cl[c1].begin()) - (lower_bound(all(cr[c1]), l[u]) - cr[c1].begin());
}
}
mem[mp(c1, c2)] = ans;
cout << ans << endl;
fflush(stdout);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |