# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
53647 | baactree | Regions (IOI09_regions) | C++17 | 7202 ms | 131072 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;
vector<int> adj[200005];
int arr[200005];
const int sz = 1200;
int cache[25005][sz];
pair<int, int> cnt[25005];
int inv[25005];
vector<int> lv[25005], rv[25005], vec[25005];
int vn;
void dfs(int now, int par) {
vec[arr[now]].push_back(vn);
lv[arr[now]].push_back(vn++);
for (auto &there : adj[now]) {
if (there == par)continue;
dfs(there, now);
}
rv[arr[now]].push_back(vn++);
}
int main() {
scanf("%d%d%d", &n, &r, &q);
scanf("%d", &arr[1]);
cnt[arr[1]].first++;
for (int i = 2; i <= n; i++) {
int a, b;
scanf("%d%d", &a, &b);
adj[a].push_back(i);
arr[i] = b;
cnt[arr[i]].first++;
}
for (int i = 0; i < 25005; i++)
cnt[i].second = i;
sort(cnt, cnt + 25005, [&](pair<int, int> &a, pair<int, int> &b) {
return a > b;
});
for (int i = 0; i < 25005; i++)
inv[cnt[i].second] = i;
dfs(1, 0);
memset(cache, -1, sizeof(cache));
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
if (inv[b] < sz) {
int &ans = cache[a][inv[b]];
if (ans == -1) {
ans = 0;
for (auto x : vec[b])
ans += (lower_bound(lv[a].begin(), lv[a].end(), x) - lv[a].begin())
- (lower_bound(rv[a].begin(), rv[a].end(), x) - rv[a].begin());
}
printf("%d\n", ans);
fflush(stdout);
}
else {
int ans = 0;
for (auto x : vec[b])
ans += (lower_bound(lv[a].begin(), lv[a].end(), x) - lv[a].begin())
- (lower_bound(rv[a].begin(), rv[a].end(), x) - rv[a].begin());
printf("%d\n", ans);
fflush(stdout);
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |