#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define mpr make_pair
#define fi first
#define se second
#define int ll
#define all(v) v.begin(), v.end()
const int MXN = 2e5 + 5;
const int B = 2000;
int n, r, q;
int reg[MXN], p[MXN];
vector<int> idx[MXN];
vector<pii> v1[MXN];
vector<int> v2[MXN];
vector<int> adj[MXN];
int dp[MXN];
int in[MXN], out[MXN];
int tim = -1;
int cur = -1;
map<pii, ll> mp;
void dfs(int a, int p)
{
in[a] = ++tim;
for (int v : adj[a])
{
if (v == p) continue;
dfs(v, a);
}
out[a] = ++tim;
}
void dfs1(int a, int p, int seen)
{
if (reg[a] != cur) mp[mpr(cur, reg[a])] = mp[mpr(cur, reg[a])] + seen;
for (int v : adj[a])
{
if (v == p) continue;
dfs1(v, a, seen + (reg[a] == cur));
dp[a] += dp[v];
}
dp[a] += (reg[a] == cur);
if (cur != reg[a] && idx[reg[a]].size() < B) mp[mpr(reg[a], cur)] = mp[mpr(reg[a], cur)] + dp[a];
}
signed main()
{
cin >> n >> r >> q;
cin >> reg[1];
idx[reg[1]].pb(1);
for (int i = 2; i <= n; i++)
{
cin >> p[i] >> reg[i];
adj[p[i]].pb(i);
idx[reg[i]].pb(i);
}
dfs(1, 1);
for (int i = 1; i <= r; i++)
{
for (int x : idx[i])
{
v1[i].pb(mpr(in[x], 1));
v1[i].pb(mpr(out[x] + 1, -1));
v2[i].pb(in[x]);
}
sort(all(v1[i]));
sort(all(v2[i]));
}
for (int i = 1; i <= r; i++)
{
if (idx[i].size() < B) continue;
cur = i;
dfs1(1, 1, 0);
}
return 0;
while (q--)
{
int u, v;
cin >> u >> v;
if (mp.find(mpr(u, v)) != mp.end())
{
cout << mp[mpr(u, v)] << endl;
continue;
}
int i = 0;
int j = 0;
ll ans = 0;
int rn = 0;
while (i < v1[u].size() && j < v2[v].size())
{
if (v1[u][i].fi <= v2[v][j]) rn += v1[u][i++].se;
else
{
ans = ans + rn;
j++;
}
}
mp[mpr(u, v)] = ans;
cout << ans << endl;
}
}
Compilation message
regions.cpp: In function 'int main()':
regions.cpp:96:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | while (i < v1[u].size() && j < v2[v].size())
| ~~^~~~~~~~~~~~~~
regions.cpp:96:38: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | while (i < v1[u].size() && j < v2[v].size())
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
22872 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
4 ms |
22872 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
4 ms |
22872 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
4 ms |
22872 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
5 ms |
22872 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
5 ms |
23048 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
5 ms |
23128 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
5 ms |
23236 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
7 ms |
23640 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
10 ms |
23920 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
13 ms |
24408 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
17 ms |
25288 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
18 ms |
25732 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
22 ms |
26976 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
27 ms |
29928 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
83 ms |
33508 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
89 ms |
31080 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
154 ms |
39604 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
24 ms |
26344 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
27 ms |
28004 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
36 ms |
28028 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
48 ms |
30520 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
66 ms |
38384 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
118 ms |
42996 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
126 ms |
46984 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
142 ms |
43808 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
333 ms |
48216 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
313 ms |
50340 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
338 ms |
50356 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
346 ms |
62828 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
324 ms |
80104 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
394 ms |
78372 KB |
Unexpected end of file - int32 expected |