#include <bits/stdc++.h>
#pragma GCC Optimize("O3")
#pragma GCC Optimize("unroll-loops")
#pragma GCC target("sse4,avx2,fma,avx")
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
const int c = 600;
struct Node {
int id, region, low, high;
vector<int> children;
};
struct Region {
vector<int> ids;
vector<pair<int, int>> ranges;
int depth;
};
Node nodes[200001];
Region regions[25001];
int ans1[200000 / c][25001], ans2[25001][200000 / c], maps_to[25001];
void dfs(int node, int& id_pool) {
int id = id_pool++;
int r = nodes[node].region;
regions[r].ids.push_back(id);
regions[r].depth++;
regions[r].ranges.push_back({id_pool, regions[r].depth});
for (int i : nodes[node].children) dfs(i, id_pool);
regions[r].depth--;
regions[r].ranges.push_back({id_pool, regions[r].depth});
}
inline int query(Region a, Region b) {
if (a.ranges.empty()) return 0;
int ans = 0;
vector<int>::iterator id = b.ids.begin();
while (id != b.ids.end() && *id < a.ranges[0].first) id++;
for (int i = 0; i < a.ranges.size() - 1 && id != b.ids.end(); i++) {
int pos2 = a.ranges[i + 1].first, depth = a.ranges[i].second;
vector<int>::iterator old = id;
while (id != b.ids.end() && *id < pos2) id++;
ans += depth * (id - old);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, r, q;
cin >> n >> r >> q >> nodes[1].region;
FOR(i, 2, n + 1) {
int p;
cin >> p >> nodes[i].region;
nodes[p].children.push_back(i);
}
int id_pool = 1;
dfs(1, id_pool);
int cnt = 1;
FOR(i, 1, r + 1) {
if (regions[i].ids.size() > c) {
FOR(j, 1, r + 1) {
ans1[cnt][j] = query(regions[i], regions[j]);
ans2[j][cnt] = query(regions[j], regions[i]);
}
maps_to[i] = cnt++;
}
}
while (q--) {
int a, b;
cin >> a >> b;
if (regions[a].ids.size() > c) cout << ans1[maps_to[a]][b] << endl;
else if (regions[b].ids.size() > c) cout << ans2[a][maps_to[b]] << endl;
else cout << query(regions[a], regions[b]) << endl;
}
return 0;
}
Compilation message
regions.cpp:2:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
#pragma GCC Optimize("O3")
regions.cpp:3:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
#pragma GCC Optimize("unroll-loops")
regions.cpp: In function 'int query(Region, Region)':
regions.cpp:44:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.ranges.size() - 1 && id != b.ids.end(); i++) {
~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9464 KB |
Output is correct |
2 |
Correct |
10 ms |
9464 KB |
Output is correct |
3 |
Correct |
10 ms |
9464 KB |
Output is correct |
4 |
Correct |
14 ms |
9464 KB |
Output is correct |
5 |
Correct |
19 ms |
9464 KB |
Output is correct |
6 |
Correct |
28 ms |
9720 KB |
Output is correct |
7 |
Correct |
39 ms |
9592 KB |
Output is correct |
8 |
Correct |
56 ms |
9720 KB |
Output is correct |
9 |
Correct |
64 ms |
10272 KB |
Output is correct |
10 |
Correct |
60 ms |
10148 KB |
Output is correct |
11 |
Correct |
136 ms |
10488 KB |
Output is correct |
12 |
Correct |
154 ms |
11304 KB |
Output is correct |
13 |
Correct |
193 ms |
10744 KB |
Output is correct |
14 |
Correct |
242 ms |
11512 KB |
Output is correct |
15 |
Correct |
307 ms |
15236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1161 ms |
15812 KB |
Output is correct |
2 |
Correct |
1236 ms |
14316 KB |
Output is correct |
3 |
Correct |
1840 ms |
18328 KB |
Output is correct |
4 |
Correct |
247 ms |
11468 KB |
Output is correct |
5 |
Correct |
336 ms |
13776 KB |
Output is correct |
6 |
Correct |
712 ms |
12796 KB |
Output is correct |
7 |
Correct |
1180 ms |
13948 KB |
Output is correct |
8 |
Correct |
1231 ms |
20816 KB |
Output is correct |
9 |
Correct |
2013 ms |
20788 KB |
Output is correct |
10 |
Correct |
2865 ms |
26932 KB |
Output is correct |
11 |
Correct |
3146 ms |
19860 KB |
Output is correct |
12 |
Correct |
4721 ms |
42540 KB |
Output is correct |
13 |
Correct |
4939 ms |
43504 KB |
Output is correct |
14 |
Execution timed out |
8013 ms |
49148 KB |
Time limit exceeded |
15 |
Execution timed out |
8048 ms |
60880 KB |
Time limit exceeded |
16 |
Correct |
6887 ms |
69768 KB |
Output is correct |
17 |
Execution timed out |
8047 ms |
63068 KB |
Time limit exceeded |