/** - dwuy -
/> フ
| _ _|
/`ミ _x ノ
/ |
/ ヽ ?
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
**/
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"
#define int long long
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 2+100005;
struct Node{
int lz, sum;
};
struct SMT{
int n;
vector<Node> tree;
SMT(int n = 0){
this->n = n;
this->tree.assign(n<<2|3, {0, 0});
}
void down(int id){
if(tree[id].lz == 0) return;
tree[id<<1].lz = tree[id<<1|1].lz = 1;
tree[id<<1].sum = tree[id<<1|1].sum = 0;
tree[id].lz = 0;
}
void update(int pos, int val){
int id = 1;
for(int lo=1, hi=n; lo<hi;){
int mid = (lo + hi)>>1;
down(id);
if(pos <= mid) hi = mid, id = id<<1;
else lo = mid + 1, id = id<<1|1;
}
tree[id].sum = val;
for(id>>=1; id; id>>=1) tree[id].sum = tree[id<<1].sum + tree[id<<1|1].sum;
}
int get(int l, int r, int id, const int &u, const int &v){
if(l > v || r < u) return 0;
if(l >= u && r <= v) return tree[id].sum;
int mid = (l + r)>>1;
return get(l, mid, id<<1, u, v) + get(mid + 1, r, id<<1|1, u, v);
}
int get(int l, int r){
return get(1, n, 1, l, r);
}
};
int n, m, k;
int dfsID = 0;
int p[MX];
int h[MX];
int w[MX];
int num[MX];
int clo[MX];
vector<int> G[MX];
vector<int> T[MX];
void dfs(int u){
h[u] = h[p[u]] + 1;
num[u] = ++dfsID;
for(int v: G[u]) dfs(v);
clo[u] = dfsID;
}
int32_t main(){
fastIO;
//file(TASK);
cin >> n >> m >> k;
for(int i=2; i<=n; i++){
cin >> p[i];
G[p[i]].push_back(i);
}
for(int i=1; i<=m; i++){
int u, d;
cin >> u >> d;
T[d].push_back(u);
cin >> w[u];
}
dfs(1);
SMT smt(n);
int ans = 0;
for(int i=k; i>=1; i--) if(T[i].size()){
sort(T[i].begin(), T[i].end(), [&](const int &a, const int &b) -> bool {return h[a] > h[b];});
vector<int> vt;
for(int u: T[i]){
int cost = smt.get(num[u] + 1, clo[u]);
if(w[u] > cost){
ans += w[u] - cost;
smt.update(num[u], -cost);
vt.push_back(u);
}
else smt.update(num[u], clo[u]);
}
for(int u: T[i]){
smt.update(num[u], 0);
}
for(int u: vt){
smt.update(num[u], w[u]);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
17756 KB |
Output is correct |
2 |
Correct |
38 ms |
19536 KB |
Output is correct |
3 |
Correct |
75 ms |
18512 KB |
Output is correct |
4 |
Correct |
79 ms |
17876 KB |
Output is correct |
5 |
Correct |
80 ms |
18768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
75 ms |
18632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10332 KB |
Output is correct |
2 |
Correct |
16 ms |
16988 KB |
Output is correct |
3 |
Correct |
16 ms |
17224 KB |
Output is correct |
4 |
Correct |
21 ms |
17244 KB |
Output is correct |
5 |
Correct |
8 ms |
16088 KB |
Output is correct |
6 |
Incorrect |
15 ms |
17500 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |