#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define fin cin
#define fout cout
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cerr << "DEBUG " << x << endl
#define debug2(x, y) cerr << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cerr << "DEBUG " << x << " " << y << " " << z << endl
#define debug4(x, y, z, o) cerr << "DEBUG " << x << " " << y << " " << z << " " << o << endl
#define all(x) x.begin(), x.end()
#define left esquerda
#define lb lower_bound
#define right direita
using namespace std;
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
typedef pair<ll, ll> pii;
typedef vector<vector<char>> mat;
typedef pair<int, string> pis;
const ll mod = 1e9 + 7, MAXN = 2e5 + 5;
typedef vector<int> vi;
typedef pair<int, pair<int, int>> piii;
const int N = 2e5; // limit for array size
int t[2 * N];
int n;
void modify(int p, int value) { // set value at position p
for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}
int query(int l, int r) { // sum on interval [l, r)
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res += t[l++];
if (r&1) res += t[--r];
}
return res;
}
void solve() {
int r, q;
cin >> n >> r >> q;
vector<vector<int>> rs(r + 5), g(n + 5);
vector<int> rr(n + 5);
int rc;
cin >> rc;
rr[1] = rc;
rs[rc].push_back(1);
for(int i = 2; i <= n; i++) {
int p, cr;
cin >> p >> cr;
rr[i] = cr;
g[p].push_back(i);
rs[cr].push_back(i);
}
vector<int> s(n + 5), e(n + 5), v(n + 5), v2(n + 5);
int time = 0;
auto dfs = [&](int u, auto &dfs) -> void {
s[u] = time;
time++;
for(int &i : g[u])
dfs(i, dfs);
e[u] = time - 1;
return;
};
dfs(1, dfs);
auto dfs2 = [&](int u, int r, auto &dfs2) -> void {
v[u] += (rr[u] == r);
v2[u] = (rr[u] == r);
for(int &i : g[u]) {
v[i] = v[u];
dfs2(i, r, dfs2);
v2[u] += v2[i];
}
return;
};
unordered_map<long long, int> mapa;
for(int i = 1; i <= r; i++) {
int sz = rs[i].size();
if(sz * sz >= n) {
v[1] = 0;
dfs2(1, i, dfs2);
for(int j = 1; j <= r; j++) {
int ans = 0, ans2 = 0;
for(int &k : rs[j]) {
ans += v2[k];
ans2 += v[k];
}
mapa[((1ll * j)<<32LL) | i] = ans;
mapa[((1ll * i)<<32LL) | j] = ans2;
}
}
}
while(q--) {
int r1, r2;
cin >> r1 >> r2;
if(mapa.find(((1ll * r1)<<32LL) | r2) != mapa.end()) {
cout << mapa[((1ll * r1)<<32LL) | r2] << endl << flush;
continue;
}
for(int &j : rs[r2])
modify(s[j], 1);
int ans = 0;
for(int &k : rs[r1])
ans += query(s[k], e[k]);
for(int &j : rs[r2])
modify(s[j], 0);
mapa[((1ll * r1)<<32LL) | r2] = ans;
cout << ans << '\n' << flush;
}
}
int32_t main() {
FAST;
int ct = 1;
// cin >> ct;
while(ct--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
regions.cpp: In function 'void setIO(std::string)':
regions.cpp:25:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |