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;
// types - only for stuff used a lot 
using ll = long long;
#define vv vector
#define Pp pair
// IO
#define get(x) scanf("%d",&x)
#define getl(x) scanf("%lld",&x);
// Operations
#define pb push_back
#define pob pop_back
#define sz(a) int(a.size()) 
#define re(a,b) a=max(a,b) // relax
#define ri(a,b) a=min(a,b) // relaxi
// Debugging
#ifndef LOCAL
#define cerr if (0) cerr
#else
#define cerr cout
#endif
#define print(arr,n) {for (int _ = 0; _ < n; _++) cerr<<arr[_]<<" "; cerr << endl; }
#define printv(vec)  {for (int _ : vec) cerr<<_<<" "; cerr<<endl;}
const int mod = 1e9+7, oo = 1e9;
const ll loo = 1e18;
// Functions 
ll modpow(ll a, ll b) {
	ll ans = 1; // faster modpow than recursive
	for (; b; b/=2,a=a*a%mod)
		if (b&1) ans = (ans*a)%mod;
	return ans;
}
ll gcd(ll a, ll b) {
	while (a) b%=a,swap(a,b);
	return b;
}
#define bitcount __builtin_popcountll
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)
/* 
   ALRIGHT HACKERS, THIS IS WHERE THE ACTUAL CODE BEGINS
 */
const bool DEBUG = 1;
using pii = pair<int,int>;
using vi = vector<int>;
const int N = 200000, R = 25000;
int C = 500;
int n,r,q;
int h[N], s[N], cnt[R];
map<int,ll> F[R];
vector<pii> points;
vector<pii> atpoints[R];
vi adj[N];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL
	if (DEBUG) freopen("input.txt", "r", stdin);
	if (DEBUG) freopen("output.txt", "w", stdout);
	clock_t start = clock();
#endif
	cin>>n>>r>>q;
	f(i,0,n) {
		if (i) {
			cin>>s[i]; s[i]--;
			adj[i].pb(s[i]);
			adj[s[i]].pb(i);
		}
		cin>>h[i]; h[i]--;
	}
	fill(cnt,cnt+R,0);
	f(i,0,n) cnt[h[i]]++;
	int t = 0;
	function<void(int,int)> dfs = [&](int v, int p) {
		points.pb({h[v],1});
		atpoints[h[v]].pb({t++,1});
		for (int w : adj[v]) if (w != p)
			dfs(w,v);
		atpoints[h[v]].pb({t++,-1});
		points.pb({h[v],-1});
	};
	dfs(0,-1);
	f(c,0,R) if (cnt[c] >= C) {
		int num = 0;
		for (pii pp : points) {
			if (pp == pii(c,1))	
				num++;
			else if (pp == pii(c,-1))
				num--;
			else if (pp.second == 1) 
				F[c][pp.first] += num;
		}			
		// O(N/C*R)
		int freq[R];
		fill(freq,freq+R,0);
		for (pii pp : points) {
			if (pp == pii(c,-1))
				continue;
			if (pp == pii(c,1))
				f(i,0,R) F[i][c] += freq[i];
			else
				freq[pp.first] += pp.second;
		}
	}
	f(_,0,q) {
		int r1, r2;
		cin>>r1>>r2;
		r1--; r2--;
		if (!F[r1].count(r2)) {
			int c = 0;
			int i = 0, j = 0;
			F[r1][r2] = 0;
			while (j<atpoints[r2].size()) {
				if (i==atpoints[r1].size() || 
					atpoints[r1][i].first > atpoints[r2][j].first) {
					if (atpoints[r2][j++].second == 1)
						F[r1][r2] += c;
				} else c += atpoints[r1][i++].second;	
			}
		}
		cout << F[r1][r2] << endl;
		fflush(stdout);
	}
#ifdef LOCAL
	cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl;
#endif
	return 0;
}
Compilation message (stderr)
regions.cpp: In function 'int main()':
regions.cpp:132:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while (j<atpoints[r2].size()) {
           ~^~~~~~~~~~~~~~~~~~~~
regions.cpp:133:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (i==atpoints[r1].size() || 
         ~^~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |