#include <bits/stdc++.h>
#define Y8o "REGIONS"
#define maxn (int) 2e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
const int can = 160;
int n, m, Q, cnt;
int a[maxn];
int in[maxn], out[maxn], vir[maxn], dem[maxn];
vector<pii> cur;
vector<int> o[maxn], pos[maxn];
vector<int> ans_dw[maxn], ans_up[maxn];
void pre_dfs(int u = 1) {
in[u] = ++cnt;
for(int v : o[u]) pre_dfs(v);
out[u] = cnt;
}
void dfs(int u, int cl, int d) {
ans_dw[cl][a[u]] += d;
d += (dem[u] = (a[u] == cl));
for(int v : o[u]) {
dfs(v, cl, d);
dem[u] += dem[v];
}
ans_up[cl][a[u]] += dem[u];
}
void solve() {
cin >> n >> m >> Q;
fi(i, 1, n) {
if(i > 1) {
int u; cin >> u;
o[u].push_back(i);
}
cin >> a[i], pos[a[i]].push_back(i);
}
pre_dfs();
cnt = 0;
fi(i, 1, m) {
if(pos[i].size() > can) {
ans_up[i].resize(m + 1);
ans_dw[i].resize(m + 1);
dfs(1, i, 0);
}
sort( all(pos[i]), [](int x, int y) { return in[x] < in[y]; });
}
fi(_, 1, Q) {
int u, v; cin >> u >> v;
if(pos[u].size() > can) cout << ans_dw[u][v] << endl;
else if(pos[v].size() > can) cout << ans_up[v][u] << endl;
else {
cur.clear();
fi(i, 0, pos[u].size() - 1) {
int node = pos[u][i];
cur.push_back({ in[node], +1 }), cur.push_back({ out[node] + 1, -1 });
}
sort(all(cur));
int j = 0, op = 0; ll res = 0;
fi(i, 0, pos[v].size() - 1) {
int node = pos[v][i];
while(j < cur.size() && cur[j].first <= in[node]) op += cur[j ++].second;
res += op;
}
cout << res << endl;
}
}
}
signed main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
// ctime();
return 0;
}
Compilation message
regions.cpp: In function 'void solve()':
regions.cpp:10:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define fi(i, a, b) for(int i = a; i <= b; i ++)
......
88 | fi(i, 0, pos[u].size() - 1) {
| ~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:88:13: note: in expansion of macro 'fi'
88 | fi(i, 0, pos[u].size() - 1) {
| ^~
regions.cpp:10:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define fi(i, a, b) for(int i = a; i <= b; i ++)
......
95 | fi(i, 0, pos[v].size() - 1) {
| ~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:95:13: note: in expansion of macro 'fi'
95 | fi(i, 0, pos[v].size() - 1) {
| ^~
regions.cpp:97:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | while(j < cur.size() && cur[j].first <= in[node]) op += cur[j ++].second;
| ~~^~~~~~~~~~~~
regions.cpp: In function 'void iof()':
regions.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
42344 KB |
Execution killed with signal 11 |
2 |
Runtime error |
33 ms |
42340 KB |
Execution killed with signal 11 |
3 |
Runtime error |
32 ms |
42376 KB |
Execution killed with signal 11 |
4 |
Correct |
9 ms |
21088 KB |
Output is correct |
5 |
Correct |
10 ms |
21072 KB |
Output is correct |
6 |
Runtime error |
36 ms |
42568 KB |
Execution killed with signal 11 |
7 |
Correct |
26 ms |
21072 KB |
Output is correct |
8 |
Correct |
34 ms |
21072 KB |
Output is correct |
9 |
Correct |
51 ms |
21328 KB |
Output is correct |
10 |
Correct |
71 ms |
21584 KB |
Output is correct |
11 |
Correct |
124 ms |
21840 KB |
Output is correct |
12 |
Correct |
127 ms |
22096 KB |
Output is correct |
13 |
Correct |
163 ms |
21840 KB |
Output is correct |
14 |
Correct |
238 ms |
22436 KB |
Output is correct |
15 |
Correct |
222 ms |
26192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
858 ms |
25816 KB |
Output is correct |
2 |
Correct |
1063 ms |
24188 KB |
Output is correct |
3 |
Correct |
1300 ms |
28396 KB |
Output is correct |
4 |
Runtime error |
57 ms |
46920 KB |
Execution killed with signal 11 |
5 |
Runtime error |
50 ms |
47376 KB |
Execution killed with signal 11 |
6 |
Runtime error |
126 ms |
54092 KB |
Execution killed with signal 11 |
7 |
Runtime error |
318 ms |
60944 KB |
Execution killed with signal 11 |
8 |
Runtime error |
680 ms |
78696 KB |
Execution killed with signal 11 |
9 |
Runtime error |
677 ms |
83312 KB |
Execution killed with signal 11 |
10 |
Runtime error |
1443 ms |
131072 KB |
Execution killed with signal 9 |
11 |
Runtime error |
3967 ms |
131072 KB |
Execution killed with signal 9 |
12 |
Runtime error |
5251 ms |
131072 KB |
Execution killed with signal 9 |
13 |
Runtime error |
3613 ms |
131072 KB |
Execution killed with signal 9 |
14 |
Runtime error |
4214 ms |
131072 KB |
Execution killed with signal 9 |
15 |
Runtime error |
2225 ms |
131072 KB |
Execution killed with signal 9 |
16 |
Runtime error |
1744 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Runtime error |
1852 ms |
131072 KB |
Execution killed with signal 9 |