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;
#define task ""
#define fi first
#define se second
#define MASK(i) (1 << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int MAXR = 25005;
const int MAX = 200005;
const int C = 500;
int n, R, q;
vector<int> adj[MAX];
vector<int> node[MAXR];
vector<pair<int, int>> queries[MAXR];
int timer;
int st[MAX];
int en[MAX];
int cntLarge;
int idLarge[MAXR];
void dfs(int u) {
st[u] = timer++;
for (int v: adj[u]) dfs(v);
en[u] = timer - 1;
}
int pre[MAX];
long long ansLarge[C][MAXR];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> R >> q;
int r1; cin >> r1;
node[--r1].push_back(0);
for (int i = 1; i < n; ++i) {
int p, r; cin >> p >> r;
node[--r].push_back(i);
adj[--p].push_back(i);
}
dfs(0);
memset(idLarge, -1, sizeof idLarge);
for (int i = 0; i < R; ++i) {
sort(all(node[i]), [](const int &u, const int &v) {
return st[u] < st[v];
});
if (node[i].size() >= C) {
idLarge[i] = cntLarge++;
memset(pre, 0, sizeof pre);
for (int j: node[i]) ++pre[st[j] + 1], --pre[en[j] + 1];
for (int j = 1; j < n; ++j) pre[j] += pre[j - 1];
for (int k = 0; k < R; ++k) {
for (int j: node[k]) ansLarge[idLarge[i]][k] += pre[st[j]];
}
} else {
for (int j: node[i]) {
queries[i].push_back({st[j], -1});
queries[i].push_back({en[j], +1});
}
sort(all(queries[i]));
}
}
while (q--) {
int r1, r2; cin >> r1 >> r2; --r1; --r2;
if (idLarge[r1] != -1) cout << ansLarge[idLarge[r1]][r2] << endl;
else {
long long answer = 0;
for (int i = 0, j = -1; i < queries[r1].size(); ++i) {
while (j + 1 < node[r2].size() && st[node[r2][j + 1]] <= queries[r1][i].fi) ++j;
answer += queries[r1][i].se * (j + 1);
}
cout << answer << endl;
}
}
return 0;
}
Compilation message (stderr)
regions.cpp: In function 'int main()':
regions.cpp:88:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0, j = -1; i < queries[r1].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~~~~
regions.cpp:89:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | while (j + 1 < node[r2].size() && st[node[r2][j + 1]] <= queries[r1][i].fi) ++j;
| ~~~~~~^~~~~~~~~~~~~~~~~
regions.cpp:49:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:50:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |