#include <vector> #include <iostream> #include <array> #include <cinttypes> #include <cstring> #include <climits> #include <string> using namespace std; namespace wzor { const int64_t MMOD = 1'000'000'007; struct Segment_tree { int left = 0; int right = 0; int64_t value = 0; }; Segment_tree segments_to_use[480'000'000 / sizeof(Segment_tree)]; int allocator_index = 1; Segment_tree* get_tree() { return &segments_to_use[allocator_index++]; } void add_value(Segment_tree *tree, int64_t index_to_add, int64_t value_to_add, int st = 0, int en = MMOD - 1) { //std::cout << "ADD_VALUE: " << index_to_add << ' ' << st << ' ' << en << '\n'; tree->value += value_to_add; if(tree->value >= MMOD) tree->value -= MMOD; if(st < en) { int mid = (st + en) / 2; if(index_to_add <= mid) { if(tree->left == 0) tree->left = allocator_index++; add_value(segments_to_use + tree->left, index_to_add, value_to_add, st, mid); } else { if(tree->right== 0) tree->right = allocator_index++; add_value(segments_to_use + tree->right, index_to_add, value_to_add, mid + 1, en); } } } int64_t get_sum(Segment_tree *tree, int st, int en, int a = 0, int b = MMOD - 1) { //std::cout << "GET SUM: " << st << ' ' << en << ' ' << a << ' ' << b << ' ' << (tree - segments_to_use)<< ' ' << tree->left << ' ' << tree->right << '\n'; if(st <= a && b <= en) { return tree->value; } if(en < a || b < st) { return 0; } int mid = (a + b) / 2; int64_t res = 0; if(tree->left != 0) res += get_sum(&segments_to_use[tree->left], st, en, a, mid); /*else cout << "BAD L\n";*/ if(tree->right != 0) res += get_sum(&segments_to_use[tree->right], st, en, mid + 1, b); /*else cout << "BAD R\n";*/ if(res >= MMOD) res -= MMOD; return res; } void print_tree(Segment_tree *tree, int a = 0, int b = MMOD - 1) { cout << tree->value << ' ' << a << ' ' << b << ' ' << tree->left << ' ' << tree->right << "\n"; int mid = (a + b) / 2; if(tree->left != 0) { print_tree(&segments_to_use[tree->left], a, mid); } if(tree->right != 0) { print_tree(&segments_to_use[tree->right], mid + 1, b); } } } int64_t solve_wzor(int n, int* input) { using namespace wzor; auto tree_even = get_tree(); auto tree_odd = get_tree(); add_value(tree_even, 0, 1); //print_tree(tree_even); int64_t prev_dp = 1; int64_t cur_value = 0; for(int i = 1; i <= n; ++i) { cur_value += input[i - 1]; if(cur_value >= MMOD) cur_value -= MMOD; if(cur_value % 2 == 0) { prev_dp = get_sum(tree_even, 0, cur_value); if(cur_value + 1 <= MMOD) prev_dp += get_sum(tree_odd, cur_value + 1, MMOD); if(prev_dp >= MMOD) prev_dp -= MMOD; add_value(tree_even, cur_value, prev_dp); } else { prev_dp = get_sum(tree_odd, 0, cur_value); if(cur_value + 1 <= MMOD) prev_dp += get_sum(tree_even, cur_value + 1, MMOD); if(prev_dp >= MMOD) prev_dp -= MMOD; add_value(tree_odd, cur_value, prev_dp); } /*cout << "PREV DP: " << prev_dp << "\n"; cout << "EVEN: " << i << "\n"; print_tree(tree_even); cout << "ODD: " << i << "\n"; print_tree(tree_odd);*/ } return prev_dp; } #include <iostream> int input[300100]; int main() { int n; std::cin >> n; for(int i = 0; i < n; ++i) { std::cin >> input[i]; } std::cout << solve_wzor(n, input) << '\n'; }
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 | #include <vector> #include <iostream> #include <array> #include <cinttypes> #include <cstring> #include <climits> #include <string> using namespace std; namespace wzor { const int64_t MMOD = 1'000'000'007; struct Segment_tree { int left = 0; int right = 0; int64_t value = 0; }; Segment_tree segments_to_use[480'000'000 / sizeof(Segment_tree)]; int allocator_index = 1; Segment_tree* get_tree() { return &segments_to_use[allocator_index++]; } void add_value(Segment_tree *tree, int64_t index_to_add, int64_t value_to_add, int st = 0, int en = MMOD - 1) { //std::cout << "ADD_VALUE: " << index_to_add << ' ' << st << ' ' << en << '\n'; tree->value += value_to_add; if(tree->value >= MMOD) tree->value -= MMOD; if(st < en) { int mid = (st + en) / 2; if(index_to_add <= mid) { if(tree->left == 0) tree->left = allocator_index++; add_value(segments_to_use + tree->left, index_to_add, value_to_add, st, mid); } else { if(tree->right== 0) tree->right = allocator_index++; add_value(segments_to_use + tree->right, index_to_add, value_to_add, mid + 1, en); } } } int64_t get_sum(Segment_tree *tree, int st, int en, int a = 0, int b = MMOD - 1) { //std::cout << "GET SUM: " << st << ' ' << en << ' ' << a << ' ' << b << ' ' << (tree - segments_to_use)<< ' ' << tree->left << ' ' << tree->right << '\n'; if(st <= a && b <= en) { return tree->value; } if(en < a || b < st) { return 0; } int mid = (a + b) / 2; int64_t res = 0; if(tree->left != 0) res += get_sum(&segments_to_use[tree->left], st, en, a, mid); /*else cout << "BAD L\n";*/ if(tree->right != 0) res += get_sum(&segments_to_use[tree->right], st, en, mid + 1, b); /*else cout << "BAD R\n";*/ if(res >= MMOD) res -= MMOD; return res; } void print_tree(Segment_tree *tree, int a = 0, int b = MMOD - 1) { cout << tree->value << ' ' << a << ' ' << b << ' ' << tree->left << ' ' << tree->right << "\n"; int mid = (a + b) / 2; if(tree->left != 0) { print_tree(&segments_to_use[tree->left], a, mid); } if(tree->right != 0) { print_tree(&segments_to_use[tree->right], mid + 1, b); } } } int64_t solve_wzor(int n, int* input) { using namespace wzor; auto tree_even = get_tree(); auto tree_odd = get_tree(); add_value(tree_even, 0, 1); //print_tree(tree_even); int64_t prev_dp = 1; int64_t cur_value = 0; for(int i = 1; i <= n; ++i) { cur_value += input[i - 1]; if(cur_value >= MMOD) cur_value -= MMOD; if(cur_value % 2 == 0) { prev_dp = get_sum(tree_even, 0, cur_value); if(cur_value + 1 <= MMOD) prev_dp += get_sum(tree_odd, cur_value + 1, MMOD); if(prev_dp >= MMOD) prev_dp -= MMOD; add_value(tree_even, cur_value, prev_dp); } else { prev_dp = get_sum(tree_odd, 0, cur_value); if(cur_value + 1 <= MMOD) prev_dp += get_sum(tree_even, cur_value + 1, MMOD); if(prev_dp >= MMOD) prev_dp -= MMOD; add_value(tree_odd, cur_value, prev_dp); } /*cout << "PREV DP: " << prev_dp << "\n"; cout << "EVEN: " << i << "\n"; print_tree(tree_even); cout << "ODD: " << i << "\n"; print_tree(tree_odd);*/ } return prev_dp; } #include <iostream> int input[300100]; int main() { int n; std::cin >> n; for(int i = 0; i < n; ++i) { std::cin >> input[i]; } std::cout << solve_wzor(n, input) << '\n'; } |