#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
const int maxR = 25000 + 1;
int n, r, q, sq;
vector <int> color[maxR];
vector <int> adj[maxN];
vector <vector<ll>> cnt, rcnt;
int pos[maxN], be[maxN], child[maxN];
int dpos[maxN], time_in;
int bit[maxN];
void update(int x, int val){
for (; x <= n; x += x & -x){
bit[x] += val;
}
}
int get(int x){
int sum = 0;
for (; x; x -= x & -x){
sum += bit[x];
}
return sum;
}
void pre_dfs(int u){
dpos[u] = ++time_in;
for (const int &i: adj[u]){
pre_dfs(i);
child[u] += child[i] + 1;
}
}
ll summ;
int dfs(int u, int c, int up = 0){
summ = 0;
if (be[u] == c) ++up;
else rcnt[rcnt.size() - 1][be[u]] += up;
for (const int &i: adj[u]){
summ += dfs(i, c, up);
}
if (be[u] == c) return summ + 1;
cnt[cnt.size() - 1][be[u]] += summ;
return summ;
}
void Init(){
cin >> n >> r >> q;
sq = sqrt(n) + 1;
int t; cin >> t;
be[1] = t;
color[t].pb(1);
for (int i = 2; i <= n; ++i){
int p, t;
cin >> p >> t;
color[t].pb(i);
be[i] = t;
adj[p].pb(i);
}
pre_dfs(1);
for (int i = 1; i <= r; ++i){
if (color[i].size() >= sq){
pos[i] = cnt.size();
cnt.pb({});
rcnt.pb({});
cnt.back().resize(maxN, 0);
rcnt.back().resize(maxN, 0);
dfs(1, i);
}
}
for (int i = 1; i <= q; ++i){
int u, v;
cin >> u >> v;
if (color[v].size() >= sq){
cout << cnt[pos[v]][u] << endl;
}
else if (color[u].size() >= sq){
cout << rcnt[pos[u]][v] << endl;
}
else{
for (const int &i: color[v]){
update(dpos[i], 1);
}
ll ans = 0;
for (const int &i: color[u]){
ans += get(dpos[i] + child[i]) - get(dpos[i]);
}
cout << ans << endl;
for (const int &i: color[v]){
update(dpos[i], -1);
}
}
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
regions.cpp: In function 'void Init()':
regions.cpp:87:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
87 | if (color[i].size() >= sq){
| ~~~~~~~~~~~~~~~~^~~~~
regions.cpp:99:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | if (color[v].size() >= sq){
| ~~~~~~~~~~~~~~~~^~~~~
regions.cpp:102:34: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | else if (color[u].size() >= sq){
| ~~~~~~~~~~~~~~~~^~~~~
regions.cpp: In function 'int main()':
regions.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4816 KB |
Output is correct |
2 |
Correct |
2 ms |
3280 KB |
Output is correct |
3 |
Correct |
3 ms |
3280 KB |
Output is correct |
4 |
Correct |
6 ms |
3152 KB |
Output is correct |
5 |
Correct |
8 ms |
3280 KB |
Output is correct |
6 |
Correct |
18 ms |
3280 KB |
Output is correct |
7 |
Correct |
25 ms |
3280 KB |
Output is correct |
8 |
Correct |
37 ms |
3280 KB |
Output is correct |
9 |
Correct |
47 ms |
3664 KB |
Output is correct |
10 |
Correct |
88 ms |
3792 KB |
Output is correct |
11 |
Correct |
141 ms |
4036 KB |
Output is correct |
12 |
Correct |
142 ms |
4432 KB |
Output is correct |
13 |
Correct |
205 ms |
4176 KB |
Output is correct |
14 |
Incorrect |
306 ms |
9544 KB |
Output isn't correct |
15 |
Runtime error |
13 ms |
9424 KB |
Execution killed with signal 13 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
80 ms |
44048 KB |
Execution killed with signal 13 |
2 |
Runtime error |
60 ms |
26952 KB |
Execution killed with signal 13 |
3 |
Runtime error |
47 ms |
25408 KB |
Execution killed with signal 13 |
4 |
Correct |
337 ms |
4816 KB |
Output is correct |
5 |
Correct |
367 ms |
6360 KB |
Output is correct |
6 |
Runtime error |
149 ms |
100184 KB |
Execution killed with signal 13 |
7 |
Runtime error |
177 ms |
85504 KB |
Execution killed with signal 13 |
8 |
Runtime error |
242 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
32 ms |
14792 KB |
Execution killed with signal 6 |
10 |
Runtime error |
34 ms |
15604 KB |
Execution killed with signal 6 |
11 |
Runtime error |
30 ms |
13008 KB |
Execution killed with signal 6 |
12 |
Runtime error |
30 ms |
15120 KB |
Execution killed with signal 6 |
13 |
Runtime error |
33 ms |
14612 KB |
Execution killed with signal 6 |
14 |
Runtime error |
32 ms |
14296 KB |
Execution killed with signal 6 |
15 |
Runtime error |
31 ms |
15688 KB |
Execution killed with signal 6 |
16 |
Runtime error |
34 ms |
15616 KB |
Execution killed with signal 6 |
17 |
Runtime error |
31 ms |
15480 KB |
Execution killed with signal 6 |