#include <algorithm> #include <iostream> #include <vector> #include <cmath> using namespace std; #define MODULO 1000000007 struct Sum { int index, sum, scaled; }; struct Tree { int size; vector<int> values; Tree(int size) { this->size = 1 << (int)ceil(log2(size)); values.resize(2 * this->size); } int query(int begin, int end) { return query(1, 0, size - 1, begin, end); } int query(int node, int left, int right, int begin, int end) { if(begin <= left && right <= end) { return values[node]; } int middle = (left + right) / 2; int result = 0; if(begin <= middle) { result = query(2 * node, left, middle, begin, end); } if(end > middle) { result += query(2 * node + 1, middle + 1, right, begin, end); result %= MODULO; } return result; } void add(int node, int value) { node += size; while(node >= 1) { values[node] += value; values[node] %= MODULO; node /= 2; } } }; #ifdef DEBUG #include <functional> #include <sstream> #include <climits> #include <random> #include <chrono> mt19937_64 randomEngine(chrono::system_clock::now().time_since_epoch().count()); template<class Type> Type random(Type min, Type max) { return uniform_int_distribution<Type>(min, max)(randomEngine); } void makeTest(stringstream& cout) { int size = random<int>(5, 12); cout << size << '\n'; while(size--) { cout << random<int>(0, MODULO - 1) << ' '; } cout << '\n'; } void brute(stringstream& cin, stringstream& cout) { int size; cin >> size; vector<int> values(size); for(auto& value : values) { cin >> value; } int result = 0; function<void(int)> solve = [&](int index) { if(index == size) { result++; result %= MODULO; return; } int sum = 0; for(int next = index; next < size; next++) { sum += values[next]; sum %= MODULO; if(sum % 2 == 0) { solve(next + 1); } } }; solve(0); cout << result << '\n'; } void solve(stringstream& cin, stringstream& cout) #else void solve() #endif { int size; cin >> size; vector<int> values(size); vector<Sum> sums(size); int sum = 0; for(int index = 0; index < size; index++) { cin >> values[index]; sum += values[index]; sum %= MODULO; sums[index].index = index; sums[index].sum = sum; } sort(sums.begin(), sums.end(), [](Sum first, Sum second) { if(first.sum == second.sum) { return first.index < second.index; } return first.sum < second.sum; }); int scaledSize = 0; sums[0].scaled = 0; for(int index = 1; index < size; index++) { if(sums[index].sum != sums[index - 1].sum) { scaledSize++; } sums[index].scaled = scaledSize; } sort(sums.begin(), sums.end(), [](Sum first, Sum second) { return first.index < second.index; }); Tree good(scaledSize + 1); Tree bad(scaledSize + 1); int result; for(int index = 0; index < size; index++) { if(sums[index].sum % 2 == 0) { result = (1 + good.query(0, sums[index].scaled) + bad.query(sums[index].scaled, scaledSize)) % MODULO; good.add(sums[index].scaled, result); } else { result = (bad.query(0, sums[index].scaled) + good.query(sums[index].scaled, scaledSize)) % MODULO; bad.add(sums[index].scaled, result); } } cout << result << '\n'; } int main() { #ifdef DEBUG for(int test = 1; test < INT_MAX; test++) { stringstream input, solveOutput, bruteOutput; makeTest(input); solve(input, solveOutput); input.seekg(0); brute(input, bruteOutput); input.seekg(0); if(solveOutput.str() == bruteOutput.str()) { cout << "[#" << test << "] OK\n"; } else { cout << "[#" << test << "] WRONG\nINPUT:\n" << input.str() << "SOLVE:\n" << solveOutput.str() << "BRUTE:\n" << bruteOutput.str(); return 0; } } #else ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); #endif }
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 | #include <algorithm> #include <iostream> #include <vector> #include <cmath> using namespace std; #define MODULO 1000000007 struct Sum { int index, sum, scaled; }; struct Tree { int size; vector<int> values; Tree(int size) { this->size = 1 << (int)ceil(log2(size)); values.resize(2 * this->size); } int query(int begin, int end) { return query(1, 0, size - 1, begin, end); } int query(int node, int left, int right, int begin, int end) { if(begin <= left && right <= end) { return values[node]; } int middle = (left + right) / 2; int result = 0; if(begin <= middle) { result = query(2 * node, left, middle, begin, end); } if(end > middle) { result += query(2 * node + 1, middle + 1, right, begin, end); result %= MODULO; } return result; } void add(int node, int value) { node += size; while(node >= 1) { values[node] += value; values[node] %= MODULO; node /= 2; } } }; #ifdef DEBUG #include <functional> #include <sstream> #include <climits> #include <random> #include <chrono> mt19937_64 randomEngine(chrono::system_clock::now().time_since_epoch().count()); template<class Type> Type random(Type min, Type max) { return uniform_int_distribution<Type>(min, max)(randomEngine); } void makeTest(stringstream& cout) { int size = random<int>(5, 12); cout << size << '\n'; while(size--) { cout << random<int>(0, MODULO - 1) << ' '; } cout << '\n'; } void brute(stringstream& cin, stringstream& cout) { int size; cin >> size; vector<int> values(size); for(auto& value : values) { cin >> value; } int result = 0; function<void(int)> solve = [&](int index) { if(index == size) { result++; result %= MODULO; return; } int sum = 0; for(int next = index; next < size; next++) { sum += values[next]; sum %= MODULO; if(sum % 2 == 0) { solve(next + 1); } } }; solve(0); cout << result << '\n'; } void solve(stringstream& cin, stringstream& cout) #else void solve() #endif { int size; cin >> size; vector<int> values(size); vector<Sum> sums(size); int sum = 0; for(int index = 0; index < size; index++) { cin >> values[index]; sum += values[index]; sum %= MODULO; sums[index].index = index; sums[index].sum = sum; } sort(sums.begin(), sums.end(), [](Sum first, Sum second) { if(first.sum == second.sum) { return first.index < second.index; } return first.sum < second.sum; }); int scaledSize = 0; sums[0].scaled = 0; for(int index = 1; index < size; index++) { if(sums[index].sum != sums[index - 1].sum) { scaledSize++; } sums[index].scaled = scaledSize; } sort(sums.begin(), sums.end(), [](Sum first, Sum second) { return first.index < second.index; }); Tree good(scaledSize + 1); Tree bad(scaledSize + 1); int result; for(int index = 0; index < size; index++) { if(sums[index].sum % 2 == 0) { result = (1 + good.query(0, sums[index].scaled) + bad.query(sums[index].scaled, scaledSize)) % MODULO; good.add(sums[index].scaled, result); } else { result = (bad.query(0, sums[index].scaled) + good.query(sums[index].scaled, scaledSize)) % MODULO; bad.add(sums[index].scaled, result); } } cout << result << '\n'; } int main() { #ifdef DEBUG for(int test = 1; test < INT_MAX; test++) { stringstream input, solveOutput, bruteOutput; makeTest(input); solve(input, solveOutput); input.seekg(0); brute(input, bruteOutput); input.seekg(0); if(solveOutput.str() == bruteOutput.str()) { cout << "[#" << test << "] OK\n"; } else { cout << "[#" << test << "] WRONG\nINPUT:\n" << input.str() << "SOLVE:\n" << solveOutput.str() << "BRUTE:\n" << bruteOutput.str(); return 0; } } #else ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); #endif } |