# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
155478 | karma | Regions (IOI09_regions) | C++14 | 144 ms | 24232 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>
#define Task "test"
#define pb emplace_back
#define ll long long
using namespace std;
const int N = int(2e5) + 7;
const int C = 25001;
const int Irene = 500;
int in[N], out[N], r[N], cur, Time;
vector<int> col[C], rin[C], a[N];
int n, R, Q, p, e1, e2;
ll res = 0, cnt[C];
bool vis[C];
vector<ll> ans[C];
void DFS(int u) {
in[u] = ++Time;
rin[r[u]].pb(Time);
for(int v: a[u]) DFS(v);
out[u] = Time;
}
void GetAns(int u, int e) {
if(r[u] == e) ++cur;
else cnt[r[u]] += cur;
for(int v: a[u]) GetAns(v, e);
if(r[u] == e) --cur;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> R >> Q >> r[1];
col[r[1]].pb(1);
for(int i = 2; i <= n; ++i) {
cin >> p >> r[i];
a[p].pb(i); col[r[i]].pb(i);
}
DFS(1);
while(Q --) {
cin >> e1 >> e2;
if(col[e1].size() <= Irene) {
res = 0;
for(int v: col[e1]) {
res +=
upper_bound(rin[e2].begin(), rin[e2].end(), out[v]) -
lower_bound(rin[e2].begin(), rin[e2].end(), in[v]);
}
cout << res << '\n';
} else {
if(!vis[e1]) {
fill(cnt, cnt + R + 1, 0);
vis[e1] = 1; cur = 0;
GetAns(1, e1);
for(int i = 1; i <= R; ++i) ans[e1].pb(cnt[i]);
}
cout << ans[e1][e2 - 1] << '\n';
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |