// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
// #define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); }
void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); }
void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
const int N = 2e5 + 5;
const int D = 455;
int n, r, q, h[N], pos[N], tin[N], s[N], f[D][N], f1[D][N], pre[N], num = 0, timer = 0;
vi adj[N], region[N], region1[N];
void input(int test_id) {
cin >> n >> r >> q;
cin >> h[1];
FOR(i, 2, n) {
int s;
cin >> s >> h[i];
adj[s].pb(i);
}
}
void dfs(int u) {
tin[u] = ++timer;
s[u] = 1;
EACH(v, adj[u]) {
dfs(v);
s[u] += s[v];
}
}
void dfs1(int u, int cnt) {
cnt += (pos[h[u]] == num? 1 : 0);
f[num][h[u]] += cnt;
EACH(v, adj[u]) dfs1(v, cnt);
}
void dfs2(int u, int &cnt) {
int cnt1 = (pos[h[u]] == num? 1 : 0);
EACH(v, adj[u]) dfs2(v, cnt1);
f1[num][h[u]] += cnt1;
cnt += cnt1;
}
int cal(int r1, int r2) {
if (region[r1].empty() || region[r2].empty()) return 0;
if (sz(region[r1]) > D) return f[pos[r1]][r2];
if (sz(region[r2]) > D) return f1[pos[r2]][r1];
vi &a = region[r1], &a1 = region1[r1], &b = region[r2];
int pos = 0, rs = 0;
EACH(i, a) {
WHILE(pos < sz(b) && tin[b[pos]] < tin[i]) ++pos;
pre[i] = pos;
}
pos = 0;
EACH(i, a1) {
WHILE(pos < sz(b) && tin[b[pos]] <= tin[i] + s[i] - 1) ++pos;
--pos;
if (pre[i] <= pos) rs += pos - pre[i] + 1;
}
return rs;
}
void solve(int test_id) {
dfs(1);
FOR(i, 1, n) region[h[i]].pb(i);
FOR(i, 1, r) {
if (region[i].empty()) continue;
auto cmp = [&](const int &a, const int &b) -> bool { return tin[a] < tin[b]; };
auto cmp1 = [&](const int &a, const int &b) -> bool { return tin[a] + s[a] < tin[b] + s[b]; };
region1[i] = region[i];
sort(all(region[i]), cmp);
sort(all(region1[i]), cmp1);
}
FOR(i, 1, r) {
if (sz(region[i]) <= D) continue;
pos[i] = ++num;
dfs1(1, 0);
int ss = 0;
dfs2(1, ss);
}
WHILE(q--) {
int r1, r2;
cin >> r1 >> r2;
cout << cal(r1, r2) << endl;
}
}
void reinit(int test_id) {
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
int test_num = 1;
// cin >> test_num; // comment if the problem does not requires multitest
FOR(i, 1, test_num) {
input(i); // input in noninteractive problems for case #i
solve(i); // main function to solve case #i
reinit(i); // reinit global data to default used in case #i
}
#ifdef LOCAL
cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n";
#endif
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14416 KB |
Output is correct |
2 |
Correct |
10 ms |
14360 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
16 ms |
14320 KB |
Output is correct |
5 |
Correct |
14 ms |
14416 KB |
Output is correct |
6 |
Correct |
22 ms |
14416 KB |
Output is correct |
7 |
Correct |
36 ms |
14512 KB |
Output is correct |
8 |
Correct |
37 ms |
14544 KB |
Output is correct |
9 |
Correct |
54 ms |
14928 KB |
Output is correct |
10 |
Correct |
84 ms |
14928 KB |
Output is correct |
11 |
Correct |
123 ms |
15264 KB |
Output is correct |
12 |
Correct |
140 ms |
15772 KB |
Output is correct |
13 |
Correct |
178 ms |
15440 KB |
Output is correct |
14 |
Correct |
228 ms |
15968 KB |
Output is correct |
15 |
Correct |
256 ms |
18640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
932 ms |
19856 KB |
Output is correct |
2 |
Correct |
1041 ms |
18212 KB |
Output is correct |
3 |
Correct |
1722 ms |
22312 KB |
Output is correct |
4 |
Correct |
244 ms |
16080 KB |
Output is correct |
5 |
Correct |
329 ms |
17768 KB |
Output is correct |
6 |
Correct |
644 ms |
21600 KB |
Output is correct |
7 |
Correct |
1077 ms |
19800 KB |
Output is correct |
8 |
Correct |
1085 ms |
34784 KB |
Output is correct |
9 |
Correct |
1815 ms |
24048 KB |
Output is correct |
10 |
Correct |
2248 ms |
40032 KB |
Output is correct |
11 |
Correct |
3050 ms |
24144 KB |
Output is correct |
12 |
Correct |
1051 ms |
25980 KB |
Output is correct |
13 |
Correct |
1540 ms |
26612 KB |
Output is correct |
14 |
Correct |
2041 ms |
29564 KB |
Output is correct |
15 |
Correct |
2242 ms |
32556 KB |
Output is correct |
16 |
Correct |
1875 ms |
41732 KB |
Output is correct |
17 |
Correct |
2199 ms |
43212 KB |
Output is correct |