This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* Author: Nonoze
* Created: Monday 19/08/2024
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef _IN_LOCAL
#define dbg(...)
#endif
// #define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x) { cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);}
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
struct SegTree {
int n, k;
vector<map<int, int>> t;
SegTree(int _n, int _k) {
n=_n, k=_k;
t.resize(4*n);
}
void build(int v, int tl, int tr, vector<int>& a) {
if (tl==tr) {
if (a[tl]!=-1) t[v][a[tl]]=1;
return;
}
int tm=(tl+tr)/2;
build(2*v, tl, tm, a);
build(2*v+1, tm+1, tr, a);
for (auto u: t[2*v]) t[v][u.fi]+=u.se;
for (auto u: t[2*v+1]) t[v][u.fi]+=u.se;
}
int query(int v, int tl, int tr, int l, int r, int val) {
if (l>r) return 0;
if (tl==l && tr==r) return t[v][val];
int tm=(tl+tr)/2;
return query(2*v, tl, tm, l, min(r, tm), val)+query(2*v+1, tm+1, tr, max(l, tm+1), r, val);
}
};
int n, k, m=0, q, SQRT;
vector<int> a, in, out, tobuild;
vector<vector<int>> adj, calc, calc2;
string s;
void dfs(int u, int i, int cnt) {
if (a[u]==i) cnt++;
else calc[i][a[u]]+=cnt;
for (auto v: adj[u]) dfs(v, i, cnt);
}
int dfs2(int u, int i) {
int cnt=0;
for (auto v: adj[u]) cnt+=dfs2(v, i);
if (a[u]==i) cnt++;
else calc2[i][a[u]]+=cnt;
return cnt;
}
void dfs(int u) {
in[u]=m++;
tobuild.pb(a[u]);
for (auto v: adj[u]) dfs(v);
out[u]=m;
}
void solve() {
read(n, k, q); SQRT=sqrt(n)*log2(n)/2;
a.clear(), a.resize(n), adj.clear(), adj.resize(n);
vector<int> cnt(k, 0);
vector<vector<int>> nodes(k);
in.clear(), in.resize(n), out.clear(), out.resize(n);
for (int i=0; i<n; i++) {
if (i) {
int x; read(x); x--;
adj[x].pb(i);
}
read(a[i]); a[i]--; cnt[a[i]]++;
nodes[a[i]].pb(i);
}
SegTree st(n, k);
dfs(0);
for (auto &u: tobuild) if (cnt[u]>=SQRT) u=-1;
st.build(1, 0, n-1, tobuild);
calc.resize(k), calc2.resize(k);
for (int i=0; i<k; i++) {
if (cnt[i]>=SQRT) {
calc[i].resize(k), calc2[i].resize(k);
dfs(0, i, 0);
dfs2(0, i);
}
}
while (q--) {
int u, v; read(u, v); u--, v--;
if (cnt[u]>=SQRT) {
cout << calc[u][v] << endl;
continue;
} else if (cnt[v]>=SQRT) {
cout << calc2[v][u] << endl;
continue;
}
int ans=0;
for (auto x: nodes[u]) {
ans+=st.query(1, 0, n-1, in[x], out[x], v);
}
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |