This article, in fact, is an addition to the article about universal segment tree (which will be further referred as the Base Article). Here will be given an example, how to use unversal segment tree for the tasks of multiple modification.
So, let’s take the following task as an example for solution:
task #2939 of site www.e-olymp.com
Dima and array
Mother gave Dima as a present an array of length \(n\). The array is not simple, but special. Dima can choose three numbers \(i\), \(j\) and \(d\) (\(1 \le i \le j \le n\), \(-1000 \le d \le 1000\)), and all elements with indexes from \(i\) to \(j\) magically become equal to \(d\). Dima plays with his array, and mother gives his questions time from time – what is the sum of all the numbers in array with indexes from \(f\) to \(t\)? Dima easily managed this problem, but what about you?
Input
The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 5 \cdot 10^5\), \(1 \le q \le 10^5\)) – the number of elements in array and the total number of operations. In the next line \(n\) numbers are given: \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(-1000 \le a_i \le 1000\)) – the initial state of array. The next \(q\) lines contain the operations and queries. The first character in the line can be either = or ?. If the line starts with =, this is an assignment operation. Next values are \(i\), \(j\) and \(d\), their restrictions are given earlier. If the line starts with ?, this is a query. Then go numbers \(f\) and \(t\) (\(1 \le f, t \le n\)).
Output
For each query print on a separate line the sum of the numbers in array with indexes from \(f\) to \(t\) inclusive.
First of all, I want to note, that Dima is very quick boy! According to this statement, he can work even with array, which contains 500000 elements! It’s more likely that Dima is a robot with a pretty quick processor, because simple human (even enough quick to perform = and ? queries) is not able to store such an array even on paper, and even more in a head. Though, array, which was gifted to Dima by mother is magical, and “magic” is pretty acceptable explanation in science. 😀
Of course, it was joke, left for us by the authors of the task. And now let’s proceed to solution.
Solution of the task
1. Revealing of the formal model
First let’s build purely mathematical model, and then correct it for the writing of code with maximal corresponding to it.
First, what we’ll have to do is to adapt the task to the general template of tasks from the Base Article:
Suppose a \(G\) – given certain non-empty set of observed operands, \(\circ:\) \(G\) \(\times\) \(G\) \(\to\) \(G\) – certain associative binary operation on the set \(G\) and \(M\) – non-empty set of modifiers, \(f: G \times M \to G\) – function-modifier of elements of the set \(G\). Let in the input stream be given: numbers \(n, m \in \mathbb{N}\); array of \(n\) elements \(a_{i}\), \(a_{i} \in G\), \(i = \overline{0, n-1}\) \(\left( a_{0}, a_{1}, \ldots, a_{n-2}, a_{n-1} \right)\).
After the array follow \(m\) queries of two types:
- Query of result calculation: in the input stream two numbers \(l\), \(r\) are given, \(0 \le l \le r \le n-1\), \(l, r \in {\mathbb{N}}_0\). Calculate result of operation \(\circ\) on the segment \(\left[ l, r \right]\) – \(A_{\left[ l, r \right]}\), where \(A_{\left[ l, r \right]} = a_l \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_r\), and put it into the output stream;
- Query of segment modification: in the input stream are given two numbers \(l\), \(r\), \(0 \le l \le r \le n-1\), \(l, r \in {\mathbb{N}}_0\) and a certain value \(y\), \(y \in M\). Modify the segment \(\left[ l, r \right]\), by replacing values of elements \(a_i\) to \(f \left( a_i, y \right)\), \(i = \overline{l, r}\).
First what we are going to notice is the difference between the solvable task and template: in the solvable task elements of array are numbered not from \(0\) to \(n-1\), but from \(1\) to \(n\), however this difference does not affect the main mathematical model.
So, as seen from the statement of the task, it’s possible to pick out elements of template:
- The set \(G\): in the given task, the role of the set \(G\) can be played by the set of integers \(\mathbb{Z}\), becayse arrat from the statement contains integers \(-1000 \le a_i \le 1000\), \(i = \overline{0, n-1}\).
- Associative operation \(\circ\) in set \(G\): role of this operation is played by addition \(+\), because in the task we must calculate sums on segments.
- Set of modifiers \(M\) and the function-modifier \(f\): according to the statement of the task, on the elements of array operation of replacement is performed: “Dima can choose three numbers \(i\), \(j\) and \(d\) (\(1 \le i \le j \le n\), \(-1000 \le d \le 1000\)), and all elements with indexes from \(i\) to \(j\) magically become equal to \(d\)“. Due to replacement of one numbers in array with others is performed, role of the set \(M\) can be also played by the set of integers \(\{-1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000\}\).
Now, because sets \(G\) and \(M\) are known, we can define function-modifier \(f\), which modifies one number by replacing it with other:
\(\forall g \in G\), \(\forall m \in M\): \(f \left( g, m \right) = m\).
The obtained elements of the model of the task:
\(G\) \(=\) \(\mathbb{Z}\)
\(\circ\) \(=\) \(+\)
\(M\) \(=\) \(\{-1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000\}\)
\(\forall g \in G\), \(\forall m \in M\): \(f \left( g, m \right) = m\)
An attentive reader will ask the question, why the set \(G\) was not defined as set \(\{-1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000\}\), because in the statement of the task elements of array are exactly elements of the set \(\{-1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000\}\). That’s because definition of the set \(G\) also depends on operation defined in it. If the set \(G\) would be defined as set \(\{-1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000\}\), it would not contain results of addition of elements of this set. I.e. for example the set \(G\) would not contain element \(n \cdot 1000\), which can be partial case of sum of all elements of the given array. But in fact by writing this text I’ve touched topic “Philosophy of interrelations of mathematics and programming”, so let’s continue.
2. Check of conditions of applicability of the universal segment tree
Actually, here are the conditions themselves from the Base Article:
Must be defined extended set of modifiers \(M_{2}\), which satisfies condition:
- if exists certain neutral modifier \(e_{M} \in M\), which satisfies condition \(\forall a \in G:\) \(f \left( a, e_{M} \right)\) \(=\) \(a\), then \(M_{2}\) \(=\) \(M\),
- else must be defined an element \(e_{M}\), which is a neutral marker (i. e. formal neutral modifier) and which satisfies condition \(e_{M} \notin M\), and then \(M_{2}\) \(=\) \(M \cup \{ e_{M} \}\).
For the function-modifier \(f\):
- must exist combining function \(C: M_{2} \times M \to M\), which combines accumulated modifiers, and satisfies conditions:
- condition of unification: \(\forall a \in G\), \(\forall m_{1}, m_{2} \in M\): \(f \left( a, {C \left( m_{1}, m_{2} \right)} \right)\) \(=\) \(f \left( {f \left( a, m_{1} \right)}, m_{2} \right)\);
- condition of the neutral modifier/marker: \(\forall m \in M\): \(C \left( e_{M}, m \right)\) \(=\) \(m\)
- must exist distributing function \(K: M \times \mathbb{N} \to M\), which distributes modifier, applied to the whole segment of certain length, and which for the segment \(\left[ l, r \right]\), which is being modified by modifier \(m \in M\), where \(a_{l}\), \(a_{l+1}\), \(\ldots\), \(a_{r-1}\), \(a_{r}\) – modified elements of this segment, and \(\delta = r – l + 1 \ge 1\) – amount of these elements, satisfies condition:
\(f \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, {K \left( m, \delta \right)} \right)\) \(=\) \(f \left( a_{l}, m \right) \circ f \left( a_{l+1}, m \right) \circ \ldots \circ f \left( a_{r-1}, m \right) \circ f \left( a_{r}, m \right)\) - according to the distributing function \(K\) must be defined an extended function-modifier \(F: G \times M \times \mathbb{N} \to G\), which modifies result on segment \(\left[ l, r \right]\) with amount of elements \(\delta = r – l + 1\):
\(F \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, m, \delta \right)\) \(=\) \(f \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, {K \left( m, \delta \right)} \right)\).
The set \(M_2\)
It’s easy to show, that neutral modifier \(e_M\) simply does not exist in the set \(M\). This follows from the definition from the function-modifier \(f\):
\(\forall g \in G\), \(\forall m \in M\): \(f \left( g, m \right) = m\)
From this definition it follows, that \(\forall g \in G\) \(f \left( g, e_M \right) = e_M\), and condition of the neutral modifier \(\forall g \in G\) \(f \left( g, e_{M} \right) = g\) can’t be satisfied.
So, because the neutral modifier does not exist, we require the definition of the neutral marker \(e_M\), which satisfies the only condition – \(e_M \notin M\). So, suppose \(e_M = 1001\).
Then \(M_2\) \(=\) \(M \cup \{ 1001 \}\) \(=\) \(\{ -1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000 \}\) \(\cup\) \(\{ 1001 \}\) \(=\) \(\{ -1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000, 1001 \}\)
So, \(M_2\) \(=\) \(\{ -1000, -999, \ldots, -1, 0, 1, \ldots, 999, 1000, 1001 \}\)
Conditions for the modifier-function \(f\)
-
Combining function \(C\):
Let’s define this function directly through the conditions of unification and condition of the neutral marker.-
Condition of unification: \(\forall a \in G\), \(\forall m_{1}, m_{2} \in M\): \(f \left( a, {C \left( m_{1}, m_{2} \right)} \right)\) \(=\) \(f \left( {f \left( a, m_{1} \right)}, m_{2} \right)\).
Simplify the left and the right parts of condition, considering the definition of modifier-function \(f \left( g, m \right) = m\).
Левая часть: \(f \left( a, {C \left( m_{1}, m_{2} \right)} \right)\) \(=\) \(C \left( m_{1}, m_{2} \right)\)
Правая часть: \(f \left( {f \left( a, m_{1} \right)}, m_{2} \right)\) \(=\) \(f \left( m_{1}, m_{2} \right)\) \(=\) \(m_{2}\).
Due to left part must equal to the right one, \(C \left( m_1, m_2 \right)\) \(=\) \(m_2\). - Condition of the neutral marker: the gotten due to the check of condition of unification function \(C \left( m_1, m_2 \right)\) \(=\) \(m_2\) satisfies condition of the neutral marker: \(C \left( e_M, m_2 \right)\) \(=\) \(m_2\).
In result the formula for the function \(C\) is following: \(\forall m_1 \in M_2\), \(\forall m_2 \in M\), \(C \left( m_1, m_2 \right)\) \(=\) \(m_2\)
-
Condition of unification: \(\forall a \in G\), \(\forall m_{1}, m_{2} \in M\): \(f \left( a, {C \left( m_{1}, m_{2} \right)} \right)\) \(=\) \(f \left( {f \left( a, m_{1} \right)}, m_{2} \right)\).
-
Distributing function \(K\):
So, suppose a given random segment \(\left[ l, r \right]\), which is being modified by modifier \(m \in M\), where \(a_{l}\), \(a_{l+1}\), \(\ldots\), \(a_{r-1}\), \(a_{r}\) – modified elements of this segment, and \(\delta = r – l + 1 \ge 1\) – amount of these elements. For the distributing function \(K\) the following condition must be satisfied:
\(f \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, {K \left( m, \delta \right)} \right)\) \(=\) \(f \left( a_{l}, m \right) \circ f \left( a_{l+1}, m \right) \circ \ldots \circ f \left( a_{r-1}, m \right) \circ f \left( a_{r}, m \right)\)
Due to \(\circ = +\), and \(\forall g \in G\), \(\forall m \in M\), \(f \left( g, m \right) = m\), we’ll get the following conversions in the condition:
Left part: \(f \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, {K \left( m, \delta \right)} \right)\) \(=\) \(K \left( m, \delta \right)\)
Right part (consider, that it has \(\delta\) addends): \(f \left( a_{l}, m \right) \circ f \left( a_{l+1}, m \right) \circ \ldots \circ f \left( a_{r-1}, m \right) \circ f \left( a_{r}, m \right)\) \(=\) \(f \left( a_{l}, m \right) + f \left( a_{l+1}, m \right) + \ldots + f \left( a_{r-1}, m \right) + f \left( a_{r}, m \right)\) \(=\) \(m + m + \ldots + m + m\) \(=\) \(\delta \cdot m\).
So, we’ve got the definition for the distributing function \(K\): \(K \left( m, \delta \right) = \delta \cdot m\) -
Extended function-modifier \(F\):
By definition, \(F \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, m, \delta \right)\) \(=\) \(f \left( {a_{l} \circ a_{l+1} \circ \ldots \circ a_{r-1} \circ a_{r}}, {K \left( m, \delta \right)} \right)\) \(=\) \(K \left( m, \delta \right)\) \(=\) \(\delta \cdot m\).
So, \(F \left( {a_{l} + a_{l+1} + \ldots + a_{r-1} + a_{r}}, m, \delta \right) = \delta \cdot m\)
Due to all conditions of applicability of segment tree have been satisfied, the segment tree of multiple modifications is really applicable for the given task, and one can proceed to the last step of solution.
3. Programming
On this step everything comes down to the usage of the segment tree with instruction, program description of functions \(C\), \(F\) and operation \(\circ = +\), and other little things, connected with the statement of the task. I will only say, that segment tree in the shown below program is being built with method read – the segment tree calls given to this method function preprocessor() and reads the elements of given in the statement array one by one.
The program can be viewed here:
Solution with included code of the segment tree
|
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
#include <iostream> using namespace std; // Copyright (c) http://vgworld.name/the-universal-segment-tree #include <algorithm> // std::min, size_t template <typename TYPE> TYPE highest_bit_of(const TYPE& Num) { TYPE answer; (answer = 1) <<= (sizeof(TYPE)*8 - 1); while((answer&Num) != answer) answer >>= 1; return answer; } template <typename TYPE, typename MODIFIER_TYPE, typename INDEX = size_t> class segment_tree { TYPE (*operation)(const TYPE&, const TYPE&); TYPE (*modify_)(const TYPE&, const MODIFIER_TYPE&, const INDEX&); MODIFIER_TYPE (*combine)(const MODIFIER_TYPE&, const MODIFIER_TYPE&); MODIFIER_TYPE neutral_modifier; bool defined; struct binary_segment_tree { segment_tree *Parent; TYPE *SegmentTree; MODIFIER_TYPE *ModifierList; INDEX amount_in_array; bool defined; void force_deletion() { delete[] SegmentTree; if (amount_in_array > 1) delete[] ModifierList; defined = false; } void safe_deletion() { if (defined) // if defined == true force_deletion(); } void set_results_on_segments() { if (amount_in_array > 1) { ModifierList = new MODIFIER_TYPE[amount_in_array-1]; for(INDEX i = amount_in_array-1; i;) { --i; SegmentTree[i] = Parent->operation( SegmentTree[2*i+1], SegmentTree[2*i+2] ); ModifierList[i] = Parent->neutral_modifier; } } } void build_tree_from_preprocessor(TYPE preprocessor()) { SegmentTree = new TYPE[2*amount_in_array-1]; const INDEX right_limit_ = 2*amount_in_array-1; for(INDEX i = amount_in_array-1; i < right_limit_; ++i) SegmentTree[i] = preprocessor(); set_results_on_segments(); defined = true; } void build_tree_from_array(TYPE *array) { SegmentTree = new TYPE[2*amount_in_array-1]; const INDEX right_limit_ = 2*amount_in_array-1; for(INDEX i = amount_in_array-1; i < right_limit_; ++i) SegmentTree[i] = *(array++); set_results_on_segments(); defined = true; } void try_push(const INDEX& vertex, const INDEX& left, const INDEX& right) { if (vertex < amount_in_array-1) if (ModifierList[vertex] != Parent->neutral_modifier) { INDEX i = 2*vertex+1, amount_of_elements=(right-left+1)/2; SegmentTree[i] = Parent->modify_(SegmentTree[i], ModifierList[vertex], amount_of_elements); SegmentTree[i+1] = Parent->modify_(SegmentTree[i+1], ModifierList[vertex], amount_of_elements); if (i+1 < amount_in_array-1) { ModifierList[i] = Parent->combine(ModifierList[i], ModifierList[vertex]); ModifierList[i+1] = Parent->combine(ModifierList[i+1], ModifierList[vertex]); } ModifierList[vertex] = Parent->neutral_modifier; } } TYPE result_on_segment_in( const INDEX& vertex, const INDEX& viewed_left, const INDEX& viewed_right, const INDEX& required_left, const INDEX& required_right ) { if ((viewed_left==required_left) && (viewed_right==required_right)) return SegmentTree[vertex]; else { try_push(vertex, viewed_left, viewed_right); INDEX left_middle = (viewed_right+viewed_left)/2, right_middle = left_middle+1; if (required_right <= left_middle) { return result_on_segment_in( 2*vertex+1, viewed_left, left_middle, required_left, required_right ); } else if (required_left >= right_middle) { return result_on_segment_in( 2*vertex+2, right_middle, viewed_right, required_left, required_right ); } else { return Parent->operation( result_on_segment_in( 2*vertex+1, viewed_left, left_middle, required_left, left_middle ), result_on_segment_in( 2*vertex+2, right_middle, viewed_right, right_middle, required_right ) ); } } } void modify_in( const INDEX& vertex, const INDEX& viewed_left, const INDEX& viewed_right, const INDEX& required_left, const INDEX& required_right, const MODIFIER_TYPE& modifier ) { if ((viewed_left==required_left) && (viewed_right==required_right)) { SegmentTree[vertex] = Parent->modify_( SegmentTree[vertex], modifier, viewed_right-viewed_left+1 ); if (vertex < amount_in_array-1) ModifierList[vertex] = Parent->combine(ModifierList[vertex], modifier); } else { try_push(vertex, viewed_left, viewed_right); INDEX left_middle = (viewed_right+viewed_left)/2, right_middle = left_middle+1; if (required_right <= left_middle) { modify_in( 2*vertex+1, viewed_left, left_middle, required_left, required_right, modifier ); } else if (right_middle <= required_left) { modify_in( 2*vertex+2, right_middle, viewed_right, required_left, required_right, modifier ); } else { modify_in( 2*vertex+1, viewed_left, left_middle, required_left, left_middle, modifier ); modify_in( 2*vertex+2, right_middle, viewed_right, right_middle, required_right, modifier ); } SegmentTree[vertex] = Parent->operation( SegmentTree[2*vertex+1], SegmentTree[2*vertex+2] ); } } public: binary_segment_tree() : defined(false) {} ~binary_segment_tree() { safe_deletion(); } void construct( TYPE *begin, TYPE *end, segment_tree *NewParent ) { safe_deletion(); this->Parent = NewParent; amount_in_array = end-begin; build_tree_from_array(begin); } void read( const INDEX& amount_of_elements, TYPE preprocessing_function(), segment_tree *NewParent ) { safe_deletion(); this->Parent = NewParent; amount_in_array = amount_of_elements; build_tree_from_preprocessor(preprocessing_function); } TYPE result(const INDEX& left, const INDEX& right) { return result_on_segment_in(0, 0, amount_in_array-1, left, right); } void modify(const INDEX& left, const INDEX& right, const MODIFIER_TYPE& modifier) { modify_in(0, 0, amount_in_array-1, left, right, modifier); } }; struct node { binary_segment_tree Left; node *Right; node() : Right(NULL) {} ~node() { Left.safe_deletion(); if (Right != NULL) delete Right; } INDEX LeftLimit, RightLimit; }; node Root; void set_variables( TYPE new_operation(const TYPE&, const TYPE&), TYPE new_modify_(const TYPE&, const MODIFIER_TYPE&, const INDEX&), MODIFIER_TYPE new_combine(const MODIFIER_TYPE&, const MODIFIER_TYPE&), const MODIFIER_TYPE& new_neutral_modifier ) { operation = new_operation; modify_ = new_modify_; combine = new_combine; neutral_modifier = new_neutral_modifier; } INDEX innode(node *C, const INDEX& Limit) { return Limit-C->LeftLimit; } void force_deletion() { Root.Left.safe_deletion(); if (Root.Right != NULL) { delete Root.Right; Root.Right = NULL; } } void safe_deletion() { if (defined) force_deletion(); } public: segment_tree() : defined(false) {} ~segment_tree() { safe_deletion(); } void construct( TYPE *begin, TYPE *end, TYPE new_operation(const TYPE&, const TYPE&), TYPE new_modify_(const TYPE&, const MODIFIER_TYPE&, const INDEX&), MODIFIER_TYPE new_combine(const MODIFIER_TYPE&, const MODIFIER_TYPE&), const MODIFIER_TYPE& new_neutral_modifier ) { set_variables( new_operation, new_modify_, new_combine, new_neutral_modifier ); INDEX amount_to_process = end-begin, current = highest_bit_of(amount_to_process), newright = 0; node *Subtree = &Root; while(amount_to_process != current) { Subtree->Left.construct(begin, begin+current, this); begin += current; Subtree->LeftLimit = newright; newright = Subtree->LeftLimit+current; Subtree->RightLimit = newright-1; amount_to_process ^= current; // equivalent to amount_to_process -=... current = highest_bit_of(amount_to_process); Subtree->Right = new node; Subtree = Subtree->Right; } Subtree->Left.construct(begin, begin+current, this); Subtree->LeftLimit = newright; Subtree->RightLimit = newright+current-1; defined = true; } void read( const INDEX& amount_of_elements, TYPE preprocessor(), TYPE new_operation(const TYPE&, const TYPE&), TYPE new_modify_(const TYPE&, const MODIFIER_TYPE&, const INDEX&), MODIFIER_TYPE new_combine(const MODIFIER_TYPE&, const MODIFIER_TYPE&), const MODIFIER_TYPE& new_neutral_modifier ) { set_variables( new_operation, new_modify_, new_combine, new_neutral_modifier ); INDEX amount_to_process = amount_of_elements, current = highest_bit_of(amount_to_process), newright = 0; node *Subtree = &Root; while(amount_to_process != current) { Subtree->Left.read(current, preprocessor, this); Subtree->LeftLimit = newright; newright = Subtree->LeftLimit+current; Subtree->RightLimit = newright-1; amount_to_process ^= current; // equivalent to amount_to_process -=... current = highest_bit_of(amount_to_process); Subtree->Right = new node; Subtree = Subtree->Right; } Subtree->Left.read(current, preprocessor, this); Subtree->LeftLimit = newright; Subtree->RightLimit = newright+current-1; defined = true; } TYPE result(const INDEX& first_required_left, const INDEX& required_right) { TYPE answer; node *C = &Root; INDEX required_left = first_required_left; while(C->RightLimit < required_left) C = C->Right; INDEX current_right = std::min(C->RightLimit, required_right); answer = C->Left.result( innode(C, required_left), innode(C, current_right) ); required_left = C->RightLimit+1; C = C->Right; while(required_left <= required_right) { current_right = std::min(C->RightLimit, required_right); answer = operation( answer, C->Left.result( innode(C, required_left), innode(C, current_right) ) ); required_left = C->RightLimit+1; C = C->Right; } return answer; } void modify(const INDEX& first_required_left, const INDEX& required_right, const MODIFIER_TYPE& modifier) { node *C = &Root; INDEX required_left = first_required_left; while(C->RightLimit < required_left) C = C->Right; while(required_left <= required_right) { INDEX current_right = std::min(C->RightLimit, required_right); C->Left.modify( innode(C, required_left), innode(C, current_right), modifier ); required_left = C->RightLimit+1; C = C->Right; } } void modify(const INDEX& index, const MODIFIER_TYPE& modifier) { modify(index, index, modifier); } }; long int preprocessor() { long int K; std::cin >> K; return K; } long int sum(const long int& a, const long int& b) { return a+b; } long int modify(const long int&, const short& m, const size_t& delta) { return ((long int)m)*delta; } short combine(const short&, const short& m2) { return m2; } int main() { ios::sync_with_stdio(false); // boosting input-output unsigned long n, l, r; unsigned int q; cin >> n >> q; segment_tree<long int, short> Tree; Tree.read(n, preprocessor, sum, modify, combine, 1001); ++q; while(--q) { char Query; cin >> Query >> l >> r; --l; --r; if (Query == '?') cout << Tree.result(l, r) << '\n'; else // if (Query == '=') { short m; cin >> m; Tree.modify(l, r, m); } } return 0; } |
Solution without code of segment tree
|
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 |
#include <iostream> using namespace std; // HERE MUST BE SEGMENT TREE CODE long int preprocessor() { long int K; std::cin >> K; return K; } long int sum(const long int& a, const long int& b) { return a+b; } long int modify(const long int&, const short& m, const size_t& delta) { return ((long int)m)*delta; } short combine(const short&, const short& m2) { return m2; } int main() { ios::sync_with_stdio(false); // boosting input-output unsigned long n, l, r; unsigned int q; cin >> n >> q; segment_tree<long int, short> Tree; Tree.read(n, preprocessor, sum, modify, combine, 1001); ++q; while(--q) { char Query; cin >> Query >> l >> r; --l; --r; if (Query == '?') cout << Tree.result(l, r) << '\n'; else // if (Query == '=') { short m; cin >> m; Tree.modify(l, r, m); } } return 0; } |