#include <stdio.h> #include <vector> constexpr auto MOD = 1000000007; //open source implementation of RedBlackTree taken from: //https://github.com/jakobkogler/Algorithm-DataStructures/blob/master/BST/RedBlackTree.h template <typename Key, typename Value> class RedBlackTreeNode { public: RedBlackTreeNode(Key key, Value value) : key(key), value(value), left(nullptr), right(nullptr), color(Color::red) { } static RedBlackTreeNode<Key, Value> *rotateLeft(RedBlackTreeNode<Key, Value> *h) { RedBlackTreeNode<Key, Value> *x = h->right; h->right = x->left; h->rightSum = x->leftSum; x->left = h; x->leftSum = ((x->leftSum + h->value) % MOD + h->leftSum) % MOD; x->color = h->color; h->color = Color::red; return x; } static RedBlackTreeNode<Key, Value> *rotateRight(RedBlackTreeNode<Key, Value> *h) { RedBlackTreeNode<Key, Value> *x = h->left; h->left = x->right; h->leftSum = x->rightSum; x->right = h; x->rightSum = ((x->rightSum + h->value) % MOD + h->rightSum) % MOD; x->color = h->color; h->color = Color::red; return x; } static void flipColors(RedBlackTreeNode<Key, Value> *h) { h->color = Color::red; h->left->color = Color::black; h->right->color = Color::black; } static bool isRed(RedBlackTreeNode<Key, Value> *node) { if (node == nullptr) return false; return node->color == Color::red; } static RedBlackTreeNode<Key, Value> *add(RedBlackTreeNode<Key, Value> *node, Key key, Value value) { if (node == nullptr) { return new RedBlackTreeNode<Key, Value>(key, value); } if (key == node->key) { node->value = (node->value + value) % MOD; } else if (key < node->key) { node->left = add(node->left, key, value); node->leftSum = (node->leftSum + value) % MOD; } else { node->right = add(node->right, key, value); node->rightSum = (node->rightSum + value) % MOD; } if (!isRed(node->left) && isRed(node->right)) node = rotateLeft(node); if (isRed(node->left) && isRed(node->right)) flipColors(node); return node; } static bool contains(RedBlackTreeNode *node, Value elem) { if (node == nullptr) return false; if (elem < node->key) return contains(node->left, elem); else if (elem == node->key) return true; else return contains(node->right, elem); } static Value sum(RedBlackTreeNode *node, const Key &left, const Key &right, const bool useLeftSum, const bool useRightSum) { Value buffer{0}; if (left <= node->key && node->key <= right) { buffer = (buffer + node->value) % MOD; if (useLeftSum) buffer = (buffer + node->leftSum) % MOD; else if (node->left) buffer = (buffer + sum(node->left, left, node->key - 1, false, true)) % MOD; if (useRightSum) buffer = (buffer + node->rightSum) % MOD; else if (node->right) buffer = (buffer + sum(node->right, node->key + 1, right, true, false)) % MOD; } else { if (right < node->key) { if (node->left) { buffer = (buffer + sum(node->left, left, right, false, false)) % MOD; } } else { if (node->right) { buffer = (buffer + sum(node->right, left, right, false, false)) % MOD; } } } return buffer; } private: enum Color { black, red }; Key key; Value value; Value leftSum{0}; Value rightSum{0}; RedBlackTreeNode *left, *right; Color color; }; template <typename Key, typename Value> class RedBlackTree { public: RedBlackTree() : root(nullptr) { } void add(Key key, Value value) { if (root == nullptr) { root = new RedBlackTreeNode<Key, Value>(key, value); } else { root = RedBlackTreeNode<Key, Value>::add(root, key, value); } } bool contains(Key elem) { if (root == nullptr) { return false; } else { return RedBlackTreeNode<Key, Value>::contains(root, elem); } } Value sum(Key left, Key right) { if (root == nullptr) { return 0; } else { return RedBlackTreeNode<Key, Value>::sum(root, left, right, false, false); } } private: RedBlackTreeNode<Key, Value> *root; }; int solution(const std::vector<int> &numbers) { int closeSectionPermutation{1}; RedBlackTree<int, int> even; RedBlackTree<int, int> odd; int factor{0}; for (const auto number : numbers) { int newCloseSectionPermutation{0}; factor = (factor + number) % MOD; const auto inverseFactor = (MOD - factor); if (factor % 2 == 0) { newCloseSectionPermutation = (newCloseSectionPermutation + even.sum(0, inverseFactor)) % MOD; newCloseSectionPermutation = (newCloseSectionPermutation + odd.sum(inverseFactor, MOD)) % MOD; } else { newCloseSectionPermutation = (newCloseSectionPermutation + odd.sum(0, inverseFactor)) % MOD; newCloseSectionPermutation = (newCloseSectionPermutation + even.sum(inverseFactor, MOD)) % MOD; } if (0 < closeSectionPermutation) { if (number % 2 == 0) { newCloseSectionPermutation = (newCloseSectionPermutation + closeSectionPermutation) % MOD; } const auto index = (number + MOD - factor) % MOD; if (index % 2 == 0) even.add(index, closeSectionPermutation); else odd.add(index, closeSectionPermutation); } closeSectionPermutation = newCloseSectionPermutation; } return closeSectionPermutation; } int main() { int n, number; scanf("%d\n", &n); std::vector<int> numbers; for (int i = 0; i < n; ++i) { scanf("%d", &number); numbers.push_back(number); } printf("%d\n", solution(numbers)); 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include <stdio.h> #include <vector> constexpr auto MOD = 1000000007; //open source implementation of RedBlackTree taken from: //https://github.com/jakobkogler/Algorithm-DataStructures/blob/master/BST/RedBlackTree.h template <typename Key, typename Value> class RedBlackTreeNode { public: RedBlackTreeNode(Key key, Value value) : key(key), value(value), left(nullptr), right(nullptr), color(Color::red) { } static RedBlackTreeNode<Key, Value> *rotateLeft(RedBlackTreeNode<Key, Value> *h) { RedBlackTreeNode<Key, Value> *x = h->right; h->right = x->left; h->rightSum = x->leftSum; x->left = h; x->leftSum = ((x->leftSum + h->value) % MOD + h->leftSum) % MOD; x->color = h->color; h->color = Color::red; return x; } static RedBlackTreeNode<Key, Value> *rotateRight(RedBlackTreeNode<Key, Value> *h) { RedBlackTreeNode<Key, Value> *x = h->left; h->left = x->right; h->leftSum = x->rightSum; x->right = h; x->rightSum = ((x->rightSum + h->value) % MOD + h->rightSum) % MOD; x->color = h->color; h->color = Color::red; return x; } static void flipColors(RedBlackTreeNode<Key, Value> *h) { h->color = Color::red; h->left->color = Color::black; h->right->color = Color::black; } static bool isRed(RedBlackTreeNode<Key, Value> *node) { if (node == nullptr) return false; return node->color == Color::red; } static RedBlackTreeNode<Key, Value> *add(RedBlackTreeNode<Key, Value> *node, Key key, Value value) { if (node == nullptr) { return new RedBlackTreeNode<Key, Value>(key, value); } if (key == node->key) { node->value = (node->value + value) % MOD; } else if (key < node->key) { node->left = add(node->left, key, value); node->leftSum = (node->leftSum + value) % MOD; } else { node->right = add(node->right, key, value); node->rightSum = (node->rightSum + value) % MOD; } if (!isRed(node->left) && isRed(node->right)) node = rotateLeft(node); if (isRed(node->left) && isRed(node->right)) flipColors(node); return node; } static bool contains(RedBlackTreeNode *node, Value elem) { if (node == nullptr) return false; if (elem < node->key) return contains(node->left, elem); else if (elem == node->key) return true; else return contains(node->right, elem); } static Value sum(RedBlackTreeNode *node, const Key &left, const Key &right, const bool useLeftSum, const bool useRightSum) { Value buffer{0}; if (left <= node->key && node->key <= right) { buffer = (buffer + node->value) % MOD; if (useLeftSum) buffer = (buffer + node->leftSum) % MOD; else if (node->left) buffer = (buffer + sum(node->left, left, node->key - 1, false, true)) % MOD; if (useRightSum) buffer = (buffer + node->rightSum) % MOD; else if (node->right) buffer = (buffer + sum(node->right, node->key + 1, right, true, false)) % MOD; } else { if (right < node->key) { if (node->left) { buffer = (buffer + sum(node->left, left, right, false, false)) % MOD; } } else { if (node->right) { buffer = (buffer + sum(node->right, left, right, false, false)) % MOD; } } } return buffer; } private: enum Color { black, red }; Key key; Value value; Value leftSum{0}; Value rightSum{0}; RedBlackTreeNode *left, *right; Color color; }; template <typename Key, typename Value> class RedBlackTree { public: RedBlackTree() : root(nullptr) { } void add(Key key, Value value) { if (root == nullptr) { root = new RedBlackTreeNode<Key, Value>(key, value); } else { root = RedBlackTreeNode<Key, Value>::add(root, key, value); } } bool contains(Key elem) { if (root == nullptr) { return false; } else { return RedBlackTreeNode<Key, Value>::contains(root, elem); } } Value sum(Key left, Key right) { if (root == nullptr) { return 0; } else { return RedBlackTreeNode<Key, Value>::sum(root, left, right, false, false); } } private: RedBlackTreeNode<Key, Value> *root; }; int solution(const std::vector<int> &numbers) { int closeSectionPermutation{1}; RedBlackTree<int, int> even; RedBlackTree<int, int> odd; int factor{0}; for (const auto number : numbers) { int newCloseSectionPermutation{0}; factor = (factor + number) % MOD; const auto inverseFactor = (MOD - factor); if (factor % 2 == 0) { newCloseSectionPermutation = (newCloseSectionPermutation + even.sum(0, inverseFactor)) % MOD; newCloseSectionPermutation = (newCloseSectionPermutation + odd.sum(inverseFactor, MOD)) % MOD; } else { newCloseSectionPermutation = (newCloseSectionPermutation + odd.sum(0, inverseFactor)) % MOD; newCloseSectionPermutation = (newCloseSectionPermutation + even.sum(inverseFactor, MOD)) % MOD; } if (0 < closeSectionPermutation) { if (number % 2 == 0) { newCloseSectionPermutation = (newCloseSectionPermutation + closeSectionPermutation) % MOD; } const auto index = (number + MOD - factor) % MOD; if (index % 2 == 0) even.add(index, closeSectionPermutation); else odd.add(index, closeSectionPermutation); } closeSectionPermutation = newCloseSectionPermutation; } return closeSectionPermutation; } int main() { int n, number; scanf("%d\n", &n); std::vector<int> numbers; for (int i = 0; i < n; ++i) { scanf("%d", &number); numbers.push_back(number); } printf("%d\n", solution(numbers)); return 0; } |