#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> nodes; vector<pair<int, int>> edges; vector<long long> sums; vector<long long> sumsRev; vector<int> f_sums; vector<int> f_sumsRev; vector<int> e_sums; vector<int> e_sumsRev; long long sum = 0; int f_sum = 0; int e_sum = 0; int calcDistance(int from, int to) { if (from == 0) { return e_sums[to]; } if (to == nodes.size()) { return e_sumsRev[from]; } return e_sum - e_sums[from] - e_sumsRev[to]; } long long calcSum(int from, int to) { if (from == 0) { return sums[to]; } if (to == nodes.size()) { return sumsRev[from]; } return sum - sums[from] - sumsRev[to]; } struct Node { int from; int to; vector<Node *> children; }; void updateTree(Node *root, pair<int, int> &edge) { Node *node = root; while (!node->children.empty()) { for (auto &child: node->children) { if (child->from <= edge.second && child->to > edge.second) { node = child; break; } } } if (calcSum(node->from, edge.second + 1) >= 0 && calcSum(edge.second + 1, node->to) >= 0) { Node *child1 = new Node(); Node *child2 = new Node(); child1->from = node->from; child1->to = edge.second + 1; child2->from = edge.second + 1; child2->to = node->to; node->children.push_back(child1); node->children.push_back(child2); } } Node *buildTree(int from, int to, vector<pair<int, int>> &edges) { Node *root = new Node(); root->from = from; root->to = to; for (auto &edge:edges) { updateTree(root, edge); } return root; } int calculateDistances(Node *tree) { if (tree->children.empty()) { return calcDistance(tree->from, tree->to - 1); } int sum = 0; for (auto &child: tree->children) { sum += calculateDistances(child); } return sum; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //------------------------------------------------------------------ // INPUT READ int num; cin >> num; int elem; edges.reserve(num); nodes.reserve(num); for (int i = 0; i < num; i++) { cin >> elem; if (elem == 0) { if (edges.empty()) { continue; } ++edges.back().first; } else { edges.emplace_back(1, edges.size()); nodes.push_back(elem); } } edges.erase(edges.end()); //------------------------------------------------------------------ // SUMS CALCULATIONS f_sums.push_back(0); sums.push_back(0); for (int nodeVal : nodes) { sum += nodeVal; sums.push_back(sum); if (nodeVal > 0) f_sum += 1; f_sums.push_back(f_sum); } e_sums.push_back(0); for (auto &edgePair : edges) { e_sum += edgePair.first; e_sums.push_back(e_sum); } sumsRev.assign(nodes.size() + 1, 0); f_sumsRev.assign(nodes.size() + 1, 0); long long sumRev = 0; int f_sumRev = 0; for (int i = nodes.size() - 1; i >= 0; i--) { sumRev += nodes[i]; sumsRev[i] = sumRev; if (nodes[i] > 0) f_sumRev += 1; f_sumsRev[i] = f_sumRev; } e_sumsRev.assign(edges.size() + 1, 0); int e_sumRev = 0; for (int i = edges.size() - 1; i >= 0; i--) { e_sumRev += edges[i].first; e_sumsRev[i] = e_sumRev; } sort(edges.rbegin(), edges.rend()); //------------------------------------------------------------------ // SOLUTION auto root = buildTree(0, nodes.size(), edges); cout << calculateDistances(root) << endl; return 0; }
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> nodes; vector<pair<int, int>> edges; vector<long long> sums; vector<long long> sumsRev; vector<int> f_sums; vector<int> f_sumsRev; vector<int> e_sums; vector<int> e_sumsRev; long long sum = 0; int f_sum = 0; int e_sum = 0; int calcDistance(int from, int to) { if (from == 0) { return e_sums[to]; } if (to == nodes.size()) { return e_sumsRev[from]; } return e_sum - e_sums[from] - e_sumsRev[to]; } long long calcSum(int from, int to) { if (from == 0) { return sums[to]; } if (to == nodes.size()) { return sumsRev[from]; } return sum - sums[from] - sumsRev[to]; } struct Node { int from; int to; vector<Node *> children; }; void updateTree(Node *root, pair<int, int> &edge) { Node *node = root; while (!node->children.empty()) { for (auto &child: node->children) { if (child->from <= edge.second && child->to > edge.second) { node = child; break; } } } if (calcSum(node->from, edge.second + 1) >= 0 && calcSum(edge.second + 1, node->to) >= 0) { Node *child1 = new Node(); Node *child2 = new Node(); child1->from = node->from; child1->to = edge.second + 1; child2->from = edge.second + 1; child2->to = node->to; node->children.push_back(child1); node->children.push_back(child2); } } Node *buildTree(int from, int to, vector<pair<int, int>> &edges) { Node *root = new Node(); root->from = from; root->to = to; for (auto &edge:edges) { updateTree(root, edge); } return root; } int calculateDistances(Node *tree) { if (tree->children.empty()) { return calcDistance(tree->from, tree->to - 1); } int sum = 0; for (auto &child: tree->children) { sum += calculateDistances(child); } return sum; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //------------------------------------------------------------------ // INPUT READ int num; cin >> num; int elem; edges.reserve(num); nodes.reserve(num); for (int i = 0; i < num; i++) { cin >> elem; if (elem == 0) { if (edges.empty()) { continue; } ++edges.back().first; } else { edges.emplace_back(1, edges.size()); nodes.push_back(elem); } } edges.erase(edges.end()); //------------------------------------------------------------------ // SUMS CALCULATIONS f_sums.push_back(0); sums.push_back(0); for (int nodeVal : nodes) { sum += nodeVal; sums.push_back(sum); if (nodeVal > 0) f_sum += 1; f_sums.push_back(f_sum); } e_sums.push_back(0); for (auto &edgePair : edges) { e_sum += edgePair.first; e_sums.push_back(e_sum); } sumsRev.assign(nodes.size() + 1, 0); f_sumsRev.assign(nodes.size() + 1, 0); long long sumRev = 0; int f_sumRev = 0; for (int i = nodes.size() - 1; i >= 0; i--) { sumRev += nodes[i]; sumsRev[i] = sumRev; if (nodes[i] > 0) f_sumRev += 1; f_sumsRev[i] = f_sumRev; } e_sumsRev.assign(edges.size() + 1, 0); int e_sumRev = 0; for (int i = edges.size() - 1; i >= 0; i--) { e_sumRev += edges[i].first; e_sumsRev[i] = e_sumRev; } sort(edges.rbegin(), edges.rend()); //------------------------------------------------------------------ // SOLUTION auto root = buildTree(0, nodes.size(), edges); cout << calculateDistances(root) << endl; return 0; } |