#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int N = 2e5 + 12, BS = 300;
const ll INF = 1e18;
int n, m, q, timer, rv[N], cl[N];
ll ans1[N / BS + 1][N], ans2[N / BS + 1][N];
vector< int > g[N];
vector< pair<int, int> > his[N];
void dfs(int v){
his[cl[v]].push_back(MP(++timer, 1));
for(int to: g[v]){
dfs(to);
}
his[cl[v]].push_back(MP(++timer, -1));
}
ll calc(int x, int y){
int i = 0, j = 0, tmp = 0;
ll r = 0;
while(i < his[x].size() && j < his[y].size()){
if(i < his[x].size() && (j == his[y].size() || his[x][i].X < his[y][j].X))
tmp += his[x][i++].Y;
else{
r += (his[y][j++].Y == 1 ? tmp: 0);
}
}
return r;
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> q >> cl[1];
for(int i = 2, x;i <= n;i++){
cin >> x >> cl[i];
g[x].push_back(i);
}
dfs(1);
for(int i = 1, z = 0;i <= m;i++){
if(his[i].size() >= BS + BS){
rv[i] = ++z;
for(int j = 1;j <= m;j++)
ans1[z][j] = calc(i, j), ans2[z][j] = calc(j, i);
}
}
for(int i = 1, x, y;i <= q;i++){
cin >> x >> y;
if(rv[x])
cout << ans1[rv[x]][y] << endl;
else if(rv[y])
cout << ans2[rv[y]][x] << endl;
else
cout << calc(x, y) << endl;
}
}
Compilation message
regions.cpp: In function 'long long int calc(int, int)':
regions.cpp:29:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while(i < his[x].size() && j < his[y].size()){
| ~~^~~~~~~~~~~~~~~
regions.cpp:29:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while(i < his[x].size() && j < his[y].size()){
| ~~^~~~~~~~~~~~~~~
regions.cpp:30:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(i < his[x].size() && (j == his[y].size() || his[x][i].X < his[y][j].X))
| ~~^~~~~~~~~~~~~~~
regions.cpp:30:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(i < his[x].size() && (j == his[y].size() || his[x][i].X < his[y][j].X))
| ~~^~~~~~~~~~~~~~~~
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status