This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define ll long long
#define nl '\n'
const int N = 200200, R = 25025;
int n, r, q, timer=0, tin[N], tout[N];
vector<int> adj[N], color[R];
indexed_set<int> b[R];
void dfs(int s=0, int e=-1) {
tin[s] = timer++;
for (int node:adj[s]) {
if (node==e) continue;
dfs(node,s);
}
tout[s] = timer;
}
int main() {
scanf("%d%d%d", &n, &r, &q);
int l; scanf("%d", &l); color[l].pb(0);
for (int i=1, s, e;i<=n-1;i++) {
scanf("%d%d", &s, &e);
s--;
adj[s].pb(i), adj[i].pb(s);
color[e].pb(i);
}
dfs();
for (int i = 0; i < R; i++) {
for (int j: color[i]) {
b[i].insert(tin[j]);
}
}
map<pair<int, int>, int> done;
while(q--) {
int i, j; scanf("%d%d", &i, &j);
int ans = 0;
if (done.count({i,j})) { ans = done[{i,j}]; }
else {
for (int parent:color[i])
ans += b[j].order_of_key(tout[parent]) - b[j].order_of_key(tin[parent]);
done[{i,j}] = ans;
}
printf("%d\n", ans);
fflush(stdout);
}
}
Compilation message (stderr)
regions.cpp: In function 'int main()':
regions.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d%d", &n, &r, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | int l; scanf("%d", &l); color[l].pb(0);
| ~~~~~^~~~~~~~~~
regions.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d%d", &s, &e);
| ~~~~~^~~~~~~~~~~~~~~~
regions.cpp:46:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | int i, j; scanf("%d%d", &i, &j);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |