#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 200001;
const ll VMAX = 21;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 1000;
const ll base = 31;
const ll nr_of_bits = 21;
const ll MMAX = NMAX / BLOCK + 2;
vector <int> v[NMAX], vertices[NMAX];
vector <pii> lista[NMAX];
int stamp;
ll n, R, Q;
ll sol[MMAX][25001];
ll precalc[25001][MMAX];
int region[NMAX];
int bigR[NMAX];
int smallR[NMAX];
int bigIdx[NMAX];
int smallIdx[NMAX];
int cati[NMAX];
int stiva[NMAX];
int vf;
int countAbove[MMAX];
int timestamp;
pii timp[NMAX];
ll preorder[NMAX + 1];
void DFS(ll node, ll p) {
stamp++;
timp[node].first = ++timestamp;
lista[region[node]].push_back({node, stamp});
if(bigIdx[region[node]] != 0) {
countAbove[bigIdx[region[node]]]++;
}
for(ll i = 1; i < MMAX; i++) {
sol[i][region[node]] += 1LL * countAbove[i];
}
for(auto x : v[node]) {
if(x == p)
continue;
DFS(x, node);
}
if(bigIdx[region[node]] != 0) {
countAbove[bigIdx[region[node]]]--;
}
stamp++;
timp[node].second = timestamp;
lista[region[node]].push_back({node, stamp});
}
int main() {
ll i;
cin >> n >> R >> Q;
cin >> region[1];
vertices[region[1]].push_back(1);
cati[region[1]]++;
for(i = 2; i <= n; i++) {
ll p;
cin >> p >> region[i];
v[p].push_back(i);
v[i].push_back(p);
vertices[region[i]].push_back(i);
cati[region[i]]++;
}
ll cc = 0, sc = 0;
for(i = 1; i <= R; i++) {
if(cati[i] >= BLOCK) {
bigR[++cc] = i;
bigIdx[i] = cc;
} else {
smallR[++sc] = i;
smallIdx[i] = sc;
}
}
DFS(1, 0);
for(ll i = 1; i <= R; i++) {
if(bigIdx[i] == 0) continue;
for(auto x : vertices[i]) {
preorder[timp[x].first]++; /// BRUH
}
for(ll j = 1; j <= n; j++) {
preorder[j] += preorder[j - 1];
}
for(ll j = 1; j <= sc; j++) {
for(auto x : vertices[smallR[j]])
precalc[j][bigIdx[i]] += 1LL * (preorder[timp[x].second] - preorder[timp[x].first - 1]);
}
for(ll j = 1; j <= n; j++)
preorder[j] = 0;
}
while(Q--) {
ll a, b;
cin >> a >> b;
if(bigIdx[a] != 0) { /// Aia de sus mare
cout << sol[bigIdx[a]][b];
} else if(bigIdx[b] != 0) { /// Aia de jos mare si aia de sus mica
cout << precalc[smallIdx[a]][bigIdx[b]];
} else { /// Ambele mici
ll i = 0;
ll j = 0;
ll deschise = 0;
vf = 0;
ll rez = 0;
while(i < lista[a].size() && j < lista[b].size()) {
pii x = lista[a][i];
pii y = lista[b][j];
if(x.second < y.second) {
if(vf && stiva[vf] == x.first) {
vf--;
deschise--;
} else {
stiva[++vf] = x.first;
deschise++;
}
i++;
} else {
if(vf && stiva[vf] == y.first) {
vf--;
} else {
rez += deschise;
stiva[++vf] = y.first;
}
j++;
}
}
cout << rez;
}
cout << endl;
}
return 0;
}
Compilation message
regions.cpp: In function 'int main()':
regions.cpp:114:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | while(i < lista[a].size() && j < lista[b].size()) {
| ~~^~~~~~~~~~~~~~~~~
regions.cpp:114:44: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | while(i < lista[a].size() && j < lista[b].size()) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15304 KB |
Output is correct |
2 |
Correct |
8 ms |
15304 KB |
Output is correct |
3 |
Correct |
10 ms |
15304 KB |
Output is correct |
4 |
Correct |
11 ms |
15304 KB |
Output is correct |
5 |
Correct |
14 ms |
15372 KB |
Output is correct |
6 |
Correct |
27 ms |
15816 KB |
Output is correct |
7 |
Correct |
32 ms |
15560 KB |
Output is correct |
8 |
Correct |
44 ms |
15688 KB |
Output is correct |
9 |
Correct |
35 ms |
16456 KB |
Output is correct |
10 |
Correct |
97 ms |
16712 KB |
Output is correct |
11 |
Correct |
109 ms |
16840 KB |
Output is correct |
12 |
Correct |
154 ms |
17800 KB |
Output is correct |
13 |
Correct |
167 ms |
17648 KB |
Output is correct |
14 |
Correct |
210 ms |
18028 KB |
Output is correct |
15 |
Correct |
251 ms |
21952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1073 ms |
22880 KB |
Output is correct |
2 |
Correct |
1065 ms |
22488 KB |
Output is correct |
3 |
Correct |
1475 ms |
25684 KB |
Output is correct |
4 |
Correct |
193 ms |
24128 KB |
Output is correct |
5 |
Correct |
375 ms |
27900 KB |
Output is correct |
6 |
Correct |
842 ms |
30400 KB |
Output is correct |
7 |
Correct |
1346 ms |
36676 KB |
Output is correct |
8 |
Correct |
1181 ms |
43544 KB |
Output is correct |
9 |
Correct |
2007 ms |
52352 KB |
Output is correct |
10 |
Correct |
2649 ms |
73572 KB |
Output is correct |
11 |
Correct |
2700 ms |
69644 KB |
Output is correct |
12 |
Correct |
1277 ms |
80804 KB |
Output is correct |
13 |
Correct |
1782 ms |
82108 KB |
Output is correct |
14 |
Correct |
1706 ms |
94772 KB |
Output is correct |
15 |
Correct |
2269 ms |
115168 KB |
Output is correct |
16 |
Correct |
2730 ms |
123940 KB |
Output is correct |
17 |
Correct |
2223 ms |
107464 KB |
Output is correct |