This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200008];
int color[200008];
vector<int> occ[25008], occ2[25008];
int tin[200008], tout[200008], euler[200008];
vector<int> ans[25008]; int hrank[25008];
int upd[200008], dp[200008];
void add_edge(int u, int v) {adj[u].push_back(v); adj[v].push_back(u);}
int timer = 0;
void DFS_EulerTour(int u) {
tin[u] = ++timer; euler[timer] = u;
for (int v : adj[u]) DFS_EulerTour(v);
tout[u] = timer;
}
int get(int c, int l, int r) {
if (l > r) return 0;
return upper_bound(occ2[c].begin(), occ2[c].end(), r) - lower_bound(occ2[c].begin(), occ2[c].end(), l);
}
signed main() {
int n, R, q, u, v, r1, r2, res; cin >> n >> R >> q >> color[1];
for (u = 2; u <= n; ++u) cin >> v >> color[u], adj[v].push_back(u);
DFS_EulerTour(1);
// for (int i = 1; i <= n; ++i) cout << tin[i] << ' ' << tout[i] << '\n';
for (u = 1; u <= n; ++u) occ[color[u]].push_back(u);
int S = sqrt(2.0 * n * R / (q * (__lg(n) + 1)));
vector<int> heavy;
for (int c = 1; c <= R; ++c) if (occ[c].size() > S) heavy.push_back(c), hrank[c] = heavy.size();
for (int c = 1; c <= R; ++c) ans[c].resize(heavy.size() + 1);
for (int c : heavy) {
for (int u : occ[c]) ++upd[tin[u] + 1], --upd[tout[u] + 1];
for (int i = 1; i <= n; ++i) dp[i] = dp[i - 1] + upd[i];
for (int i = 1; i <= n; ++i) ans[color[euler[i]]][hrank[c]] += dp[i];
for (int u : occ[c]) --upd[tin[u] + 1], ++upd[tout[u] + 1];
for (int i = 1; i <= n; ++i) dp[i] = dp[i - 1] + upd[i];
}
for (int c = 1; c <= R; ++c) {
for (int u : occ[c]) occ2[c].push_back(tin[u]);
sort(occ2[c].begin(), occ2[c].end());
}
while (q--) {
cin >> r1 >> r2;
if (occ[r1].size() > S) cout << ans[r2][hrank[r1]] << endl;
else {
res = 0;
for (int u : occ[r1]) res += get(r2, tin[u] + 1, tout[u]);
cout << res << endl;
}
}
}
Compilation message (stderr)
regions.cpp: In function 'int main()':
regions.cpp:34:49: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | for (int c = 1; c <= R; ++c) if (occ[c].size() > S) heavy.push_back(c), hrank[c] = heavy.size();
| ~~~~~~~~~~~~~~^~~
regions.cpp:49:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | if (occ[r1].size() > S) cout << ans[r2][hrank[r1]] << endl;
| ~~~~~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |