This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int MAX_N = 2e5 + 10;
const int MAX_R = 3e4 + 10;;
const int B = 500;
int timer;
int H[MAX_N];
int sz[MAX_N], st[MAX_N], pos[MAX_N];
vector <int> graph[MAX_N];
vector <int> region[MAX_R];
map <int, int> ans[MAX_R];
int pre[MAX_N];
int get_size(const int &u) {
timer++;
st[u] = timer;
pos[timer] = u;
sz[u] = 1;
for(auto v : graph[u]) {
sz[u] += get_size(v);
}
return sz[u];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, R, Q;
cin >> N >> R >> Q;
for(int i = 1; i <= N; i++) {
int S;
if(i != 1) {
cin >> S;
graph[S].push_back(i);
}
cin >> H[i];
}
get_size(1);
for(int i = 1; i <= N; i++) {
region[H[i]].push_back(st[i]);
}
for(int i = 1; i <= R; i++) {
sort(region[i].begin(), region[i].end());
}
for(int i = 1; i <= R; i++) {
if(region[i].size() < B) {
continue;
}
fill(pre, pre + N + 1, 0);
for(auto v : region[i]) {
pre[v]++;
pre[v + sz[pos[v]]]--;
}
for(int j = 1; j <= N; j++) {
pre[j] += pre[j - 1];
ans[H[pos[j]]][i] += pre[j];
}
}
for(int i = 1; i <= Q; i++) {
int r1, r2;
cin >> r1 >> r2;
if(region[r1].size() >= B) {
cout << ans[r2][r1] << endl;
continue;
}
int res = 0;
for(auto v : region[r1]) {
res += upper_bound(region[r2].begin(), region[r2].end(), v + sz[pos[v]] - 1) - lower_bound(region[r2].begin(), region[r2].end(), v);
}
cout << res << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |