#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n , m , k , tree[N << 2] , st_tim[N] , fn_tim[N] , tim;
vector <int> adj[N];
set <int> st;
struct fruit{
int v , d , w;
};
vector <fruit> all;
bool cmp(fruit a , fruit b)
{
return a.d > b.d;
}
void Dfs(int v)
{
st_tim[v] = tim++;
for(auto u : adj[v])
Dfs(u);
fn_tim[v] = tim;
}
int Get(int l , int r , int node = 1 , int nl = 0 , int nr = n)
{
if(r <= nl || nr <= l)
return 0;
if(l <= nl && nr <= r)
return tree[node];
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
return Get(l , r , lc , nl , mid) + Get(l , r , rc , mid , nr);
}
void Add(int ind , int val , int node = 1 , int nl = 0 , int nr = n)
{
tree[node] += val;
if(nr - nl == 1)
return;
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
if(ind < mid)
Add(ind , val , lc , nl , mid);
else
Add(ind , val , rc , mid , nr);
}
void Res(int ind , int node = 1 , int nl = 0 , int nr = n)
{
if(nr - nl == 1)
{
tree[node] = 0;
return;
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
if(ind < mid)
Res(ind , lc , nl , mid);
else
Res(ind , rc , mid , nr);
tree[node] = tree[lc] + tree[rc];
}
void ins(fruit a)
{
st.insert(st_tim[a.v]);
Add(st_tim[a.v] , a.w);
auto it = st.upper_bound(st_tim[a.v]);
if(it == st.end())
return;
int now = *it;
vector <int> rem;
while(now < fn_tim[a.v])
{
rem.push_back(now);
Res(now);
it++;
if(it == st.end())
break;
now = *it;
}
for(auto u : rem)
st.erase(u);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i = 2 ; i <= n ; i++)
{
int x; cin >> x;
adj[x].push_back(i);
}
for(int i = 0 ; i < m ; i++)
{
int v , d , w; cin >> v >> d >> w;
all.push_back({v , d , w});
}
sort(all.begin() , all.end() , cmp);
Dfs(1);
int ans = 0;
for(auto u : all)
{
int val = Get(st_tim[u.v] , fn_tim[u.v]);
if(u.w > val)
{
ans += u.w - val;
ins(u);
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
10956 KB |
Output is correct |
2 |
Correct |
27 ms |
11012 KB |
Output is correct |
3 |
Correct |
88 ms |
16312 KB |
Output is correct |
4 |
Correct |
82 ms |
16440 KB |
Output is correct |
5 |
Correct |
91 ms |
16572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
15236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7260 KB |
Output is correct |
2 |
Correct |
15 ms |
8752 KB |
Output is correct |
3 |
Correct |
15 ms |
8796 KB |
Output is correct |
4 |
Correct |
15 ms |
8796 KB |
Output is correct |
5 |
Correct |
7 ms |
7384 KB |
Output is correct |
6 |
Incorrect |
14 ms |
8492 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |