1+ /*
2+ * @Author: LetMeFly
3+ * @Date: 2025-09-28 19:10:08
4+ * @LastEditors: LetMeFly.xyz
5+ * @LastEditTime: 2025-09-28 19:31:14
6+ */
7+ /*
8+
9+ 1
10+ 2 3
11+ 0 1
12+
13+ 2
14+ 3 1
15+ 0 2
16+
17+
18+
19+ root 1->2
20+ times: 1
21+ 2->3: 1
22+ need: 1
23+ 1 -> 2: 1
24+ need: 1
25+ 3->1:
26+ need: 3
27+ times: 2
28+ sum: 1+2=3
29+ */
30+
31+
32+ /*
33+
34+ 1
35+ 2 3
36+ 1 2 3 1
37+
38+
39+ 3
40+ 1 2
41+ 3 1 2 1
42+
43+ 1->3: 2
44+ times: 2
45+ 2->1: 4
46+ times: 2
47+ 3->2: 4
48+ times: 2
49+ 1->3: (5->3) 3
50+ times: 3
51+ 2->1: (1->1) 0
52+ 3->2: (2->2) 0
53+ 1->1: (5->1) 1
54+ times 1
55+ sum: 2 + 2 + 2 + 3 + 1 = 10
56+ */
57+ #include < bits/stdc++.h>
58+ using namespace std ;
59+ typedef long long ll;
60+
61+ struct Node {
62+ int original, to;
63+ Node *left, *right;
64+ int already; // 由于father导致的已变更次数
65+ };
66+
67+ // 变化范围是1,2,3,4,5
68+ inline int change (int original, int times) {
69+ return (original + times - 1 + 5 ) % 5 + 1 ;
70+ }
71+
72+ inline int calc (int from, int to) {
73+ return (to - from + 5 ) % 5 ;
74+ }
75+
76+ /*
77+ 5
78+ 1 2 3 0 1
79+ 2 3 1 0 2
80+
81+ */
82+
83+ int main () {
84+ int n;
85+ cin >> n;
86+ queue<Node*> q;
87+ Node* head = new Node ();
88+ q.push (head);
89+ for (int i = 0 ; i < n; i++) {
90+ Node* thisNode = q.front ();
91+ q.pop ();
92+ cin >> thisNode->original ;
93+ thisNode->left = new Node ();
94+ thisNode->right = new Node ();
95+ q.push (thisNode->left );
96+ q.push (thisNode->right );
97+ }
98+ q = queue<Node*>();
99+ q.push (head);
100+ ll ans = 0 ;
101+ for (int i = 0 ; i < n; i++) {
102+ Node* thisNode = q.front ();
103+ q.pop ();
104+ cin >> thisNode->to ;
105+ q.push (thisNode->left );
106+ q.push (thisNode->right );
107+ if (thisNode->original == 0 ) {
108+ continue ;
109+ }
110+ int from = change (thisNode->original , thisNode->already );
111+ int need = calc (from, thisNode->to );
112+ ans += need;
113+ thisNode->left ->already = (thisNode->already + need) % 5 ;
114+ thisNode->right ->already = (thisNode->already + need) % 5 ;
115+ }
116+ cout << ans << endl;
117+ return 0 ;
118+ }
0 commit comments