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 sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
} return false;
}
template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
static_assert(dimension > 0, "Dimension must be positive !\n");
template<typename... Args>
tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
template<typename T>
struct tensor<1, T> : public vector<T> {
tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};
const int MAXN = 2e5 + 5;
const int MAXR = 500 * 505;
const int B = 500;
int N, R, Q, H[MAXN], r1[MAXN], r2[MAXN];
vector<int> adj[MAXN];
bool heavy[MAXR];
vector<int> inRegion[MAXR];
int sum[MAXN], target;
ll total_sum[MAXR];
void dfs(int u, vector<ll>& total_sum){
total_sum[H[u]] += sum[u];
sum[u] += (H[u] == target);
for(int v : adj[u]){
sum[v] = sum[u];
dfs(v, total_sum);
}
}
int timerDFS, tin[MAXN], tout[MAXN];
vector<int> pos[MAXR];
vector<ll> save[MAXR];
void init_dfs(int u){
tin[u] = ++timerDFS;
pos[H[u]].push_back(tin[u]);
for(int v : adj[u]){
init_dfs(v);
}
tout[u] = timerDFS;
}
int count(int i, int l, int r){
return upper_bound(all(pos[i]), r) - lower_bound(all(pos[i]), l);
}
void testcase(){
scanf("%d%d%d", &N, &R, &Q);
for(int i = 1; i <= N; ++i){
if(i > 1){
int p;
scanf("%d", &p);
adj[p].push_back(i);
}
scanf("%d", &H[i]);
inRegion[H[i]].push_back(i);
}
for(int i = 1; i <= R; ++i){
if(sz(inRegion[i]) > B){
heavy[i] = true;
sum[1] = 0;
save[i].resize(N + 1);
target = i;
dfs(1, save[i]);
} else{
heavy[i] = false;
}
}
init_dfs(1);
for(int i = 1; i <= Q; ++i){
int r1, r2;
scanf("%d%d", &r1, &r2);
if(heavy[r1]){
cout << save[r1][r2] << endl;
} else{
ll ans = 0;
for(int u : inRegion[r1]){
ans += count(r2, tin[u], tout[u]);
}
if(r1 == r2){ //2x times
ans -= sz(inRegion[r1]);
}
cout << ans << endl;
}
}
}
int main(){
int T = 1;
// cin >> T;
while(T--){
testcase();
}
return 0;
}
Compilation message (stderr)
regions.cpp: In function 'void testcase()':
regions.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d%d", &N, &R, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:87:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
regions.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d", &H[i]);
| ~~~~~^~~~~~~~~~~~~
regions.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d%d", &r1, &r2);
| ~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |