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 OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
const int MAXN = 2e5+1, MAXR = 2.5e4+1;
int32_t n,r,q;
vector<int32_t> adj[MAXN];
vector<pair<int32_t,int32_t>> v[MAXR], reg[MAXR];
int32_t h[MAXN];
vector<int32_t> tour;
vector<int32_t> emp[MAXR];
int32_t mp[MAXN];
UM<int32_t,int32_t> pre[MAXR];
int timer;
void dfs(int s) {
tour.PB(s);
emp[h[s]].PB(s);
v[h[s]].PB({timer,1});
timer++;
FORX(u,adj[s]) {
dfs(u);
}
v[h[s]].PB({timer,-1});
}
int calc(int a, int b) {
int i = 0, j = 0, res = 0;
while (i < reg[a].size() && j < emp[b].size()) {
if (reg[a][i].F < mp[emp[b][j]]) i++;
else {
res += reg[a][i].S;
j++;
}
}
return res;
}
signed main() {
OPTM;
cin >> n >> r >> q >> h[1];
int blk = sqrt(n+.0)+1;
FOR(i,2,n+1) {
int x;
cin >> x >> h[i];
adj[x].PB(i);
}
dfs(1);
FOR(i,0,n) mp[tour[i]] = i;
FOR(i,1,r+1) {
sort(ALL(v[i]));
int vn = v[i].size();
int j = 0, j1 = 0, cur = 0;
while (j < vn) {
if (v[i][j].F-1 >= 0) reg[i].PB({v[i][j].F-1,cur});
cur += v[i][j].S;
while (j+1 < vn && v[i][j+1].F == v[i][j].F) cur += v[i][++j].S;
j++;
}
reg[i].PB({n-1,cur});
}
FOR(i,1,r+1) {
if (emp[i].size() >= blk) {
FOR(j,1,r+1) {
if (i == j) continue;
pre[i][j] = calc(i,j);
pre[j][i] = calc(j,i);
}
}
}
FOR(_,0,q) {
int a,b; cin >> a >> b;
if (emp[a].size() >= blk || emp[b].size() >= blk) cout << pre[a][b];
else cout << calc(a,b);
cout << endl;
}
}
Compilation message (stderr)
regions.cpp: In function 'int calc(int, int)':
regions.cpp:49:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | while (i < reg[a].size() && j < emp[b].size()) {
| ~~^~~~~~~~~~~~~~~
regions.cpp:49:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | while (i < reg[a].size() && j < emp[b].size()) {
| ~~^~~~~~~~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:73:20: warning: unused variable 'j1' [-Wunused-variable]
73 | int j = 0, j1 = 0, cur = 0;
| ^~
regions.cpp:83:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
83 | if (emp[i].size() >= blk) {
| ~~~~~~~~~~~~~~^~~~~~
regions.cpp:93:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
93 | if (emp[a].size() >= blk || emp[b].size() >= blk) cout << pre[a][b];
| ~~~~~~~~~~~~~~^~~~~~
regions.cpp:93:51: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
93 | if (emp[a].size() >= blk || emp[b].size() >= blk) cout << pre[a][b];
| ~~~~~~~~~~~~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |