-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiff_Main.cpp
More file actions
66 lines (63 loc) · 1.46 KB
/
Copy pathdiff_Main.cpp
File metadata and controls
66 lines (63 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
diff --git a/CodeJam/Main.cpp b/examples/2022/Qualification/D/Main.cpp
index 2150379..a2d3037 100644
--- a/CodeJam/Main.cpp
+++ b/examples/2022/Qualification/D/Main.cpp
@@ -1570,11 +1570,61 @@ void init(){
cin >> T;
}
+d(ll, v(ll)) backwards;
+v(ll) Pi;
+v(ll) Fi;
+ll N;
+
void readInput(){
+ backwards.cl;
+ Pi.cl;
+ Fi.cl;
+ cin >> N;
+ Fi.resize(N);
+ Pi.resize(N);
+ forn(i, N)
+ cin >> Fi[i];
+ forn(i, N){
+ cin >> Pi[i];
+ Pi[i]--;
+ backwards[Pi[i]].pb(i);
+ }
+
+}
+
+p(ll, ll) recursive_fun(ll node){
+ if(backwards[node].sz == 0){
+ lg(node);
+ llog("initiator");
+ return mp(Fi[node], Fi[node]);
+ }
+ ll sum = 0;
+ ll smallest_close_root = INF;
+ forn(i, backwards[node].sz){
+ ll par = backwards[node][i];
+ p(ll, ll) cur_recur = recursive_fun(par);
+ sum += cur_recur.st;
+ smallest_close_root = min(smallest_close_root, cur_recur.nd);
+ }
+ if(node>=0 && Fi[node] > smallest_close_root){
+ sum += Fi[node] - smallest_close_root;
+ lg(node);
+ llog("update");
+ lg(mp(sum, Fi[node]));
+ return mp(sum, Fi[node]);
+ }
+ lg(node);
+ llog("pass through");
+ lg(mp(sum, smallest_close_root));
+ return mp(sum, smallest_close_root);
}
// write to COMM_TYPE result
void calcFunction() {
+ lg(Pi);
+ lg(backwards);
+ lg(Fi);
+ result = recursive_fun(-1).st;
}
} // namespace task