# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
74291 | Bruteforceman | Regions (IOI09_regions) | C++11 | 6595 ms | 75512 KiB |
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"
using namespace std;
int N, R, Q;
int a[200010], p[200010];
const int magic = 200;
const int chunk = 5 + (200000 / magic);
int ans[chunk][25001];
vector <int> g[200010];
int idx[25001];
int current;
int tot[25001];
void dfs(int x, int cnt) {
cnt += (current == a[x]);
if(a[x] != current) {
ans[idx[current]][a[x]] += cnt;
}
for(auto i : g[x]) {
dfs(i, cnt);
}
}
int in[200010];
int out[200010];
vector <int> v[25001];
vector <int> nodes[25001];
int cur;
void go(int x) {
in[x] = ++cur;
v[a[x]].emplace_back(in[x]);
for(auto i : g[x]) {
go(i);
}
out[x] = cur;
}
int count(int l, int r, int val) {
return upper_bound(v[val].begin(), v[val].end(), r) - lower_bound(v[val].begin(), v[val].end(), l);
}
int main(int argc, char const *argv[])
{
scanf("%d %d %d", &N, &R, &Q);
for(int i = 1; i <= N; i++) {
if(i == 1) scanf("%d", &a[i]);
else scanf("%d %d", &p[i], &a[i]);
}
for(int i = 2; i <= N; i++) {
g[p[i]].emplace_back(i);
}
for(int i = 1; i <= N; i++) {
tot[a[i]] += 1;
nodes[a[i]].emplace_back(i);
}
int now = 0;
for(int i = 1; i <= R; i++) {
if(tot[i] > magic) {
current = i;
idx[i] = now++;
dfs(1, 0);
}
}
go(1);
while(Q--) {
int p, q;
scanf("%d %d", &p, &q);
if(tot[p] > magic) {
printf("%d\n", ans[idx[p]][q]);
} else {
int res = 0;
for(int i : nodes[p]) {
res += count(in[i], out[i], q);
}
printf("%d\n", res);
}
fflush(stdout);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |