#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Os")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define pii pair<int, short>
vector<vector<int>> graph;
vector<short> region;
vector<int> rind;
vector<int> start;
vector<int> ende;
int c =0 ;
vector<vector<int>> dpback;
vector<vector<ll>> backsolutions;
vector<vector<ll>> frontsolutions;
int K = 80;
void dfsback(int i)
{
start[i] = c++;
for (int ele:graph[i])
{
dfsback(ele);
for (int j = 0; j < K; j++)
{
dpback[i][j] += dpback[ele][j];
}
}
for (int j = 0; j < K; j++)
{
backsolutions[region[i]][j] = dpback[i][j];
}
if (rind[region[i]]!= -1) dpback[i][rind[region[i]]]++;
ende[i] = c;
}
void dfsfront(int i, vector<int> dpfront)
{
for (int j = 0; j < K; j++)
{
frontsolutions[j][region[i]] = dpfront[j];
}
if (rind[region[i]]!=-1) dpfront[rind[region[i]]]++;
for (int ele:graph[i])
{
dfsfront(ele, dpfront);
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, r, q;
cin >> n >> r >> q;
graph.assign(n, vector<int>());
region.assign(n, 0);
rind.assign(r, -1);
start.assign(n, 0);
ende.assign(n, 0);
dpback.assign(n, vector<int>(K, 0));
backsolutions.assign(r, vector<ll>(K, -1));
vector<vector<int>> part(r);
cin >> region[0];
region[0]--;
part[region[0]].push_back(0);
for (int i = 1; i < n; i++)
{
int b;
cin >> b;
graph[b-1].push_back(i);
cin >> region[i];
region[i]--;
part[region[i]].push_back(i);
}
vector<int> rsum(r);
int d =0;
for (int i = 0; i < n; i++) rsum[region[i]]++;
for (int i = 0; i < r; i++)
{
if (rsum[i]> (n+K+K)/K)
{
rind[i] = d++;
}
}
rsum.resize(0);
dfsback(0);
dpback.resize(0);
frontsolutions.assign(K, vector<ll>(r, -1));
dfsfront(0, vector<int>(K, 0));
while (q--)
{
int a, b;
cin >> a >> b;
a--; b--;
if (rsum[a]>(n+K+K)/K) cout << frontsolutions[rind[a]][b] << endl;
else if (rsum[b]>(n+K+K)/K) cout << backsolutions[a][rind[b]] << endl;
else
{
vector<pii> s;
for (int ele:part[a])
{
s.push_back({start[ele], 0});
s.push_back({ende[ele], 1});
}
for (int ele:part[b])
{
s.push_back({start[ele], 2});
}
sort(s.begin(), s.end());
int open = 0;
int sol = 0;
for (pii ele:s)
{
if (ele.second == 0) open++;
if (ele.second == 1) open--;
if (ele.second == 2) sol += open;
}
cout << sol << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
6 |
Correct |
13 ms |
1112 KB |
Output is correct |
7 |
Correct |
24 ms |
1112 KB |
Output is correct |
8 |
Correct |
17 ms |
1368 KB |
Output is correct |
9 |
Correct |
36 ms |
3416 KB |
Output is correct |
10 |
Correct |
70 ms |
5204 KB |
Output is correct |
11 |
Correct |
138 ms |
7016 KB |
Output is correct |
12 |
Correct |
156 ms |
9648 KB |
Output is correct |
13 |
Correct |
240 ms |
10584 KB |
Output is correct |
14 |
Correct |
422 ms |
13188 KB |
Output is correct |
15 |
Correct |
425 ms |
21472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2008 ms |
33520 KB |
Output isn't correct |
2 |
Incorrect |
1814 ms |
33628 KB |
Output isn't correct |
3 |
Incorrect |
3572 ms |
41856 KB |
Output isn't correct |
4 |
Correct |
366 ms |
15960 KB |
Output is correct |
5 |
Correct |
415 ms |
21984 KB |
Output is correct |
6 |
Correct |
2359 ms |
27564 KB |
Output is correct |
7 |
Correct |
3037 ms |
37200 KB |
Output is correct |
8 |
Correct |
2614 ms |
56232 KB |
Output is correct |
9 |
Correct |
4179 ms |
76032 KB |
Output is correct |
10 |
Execution timed out |
8009 ms |
98532 KB |
Time limit exceeded |
11 |
Correct |
6937 ms |
100412 KB |
Output is correct |
12 |
Incorrect |
1801 ms |
92228 KB |
Output isn't correct |
13 |
Incorrect |
2500 ms |
93152 KB |
Output isn't correct |
14 |
Incorrect |
4934 ms |
98756 KB |
Output isn't correct |
15 |
Incorrect |
4710 ms |
109296 KB |
Output isn't correct |
16 |
Runtime error |
258 ms |
131072 KB |
Execution killed with signal 9 |
17 |
Incorrect |
4873 ms |
115568 KB |
Output isn't correct |