CodeForces 242E "XOR on Segment" (tree line)

 

Portal

 

• the meaning of problems

  To give you a sequence comprising a number n a, the operation on two defined sequences:

    (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$;

    (2)$2,l,r,x\ :\ \forall\ i \in[l,r],a_i = a_i\ xor\ x$;

  Output Operation (1) corresponding to the value ans;

•answer

  Because the operation is an exclusive OR, and XOR operations are designed to be in binary;

  So consider the establishment of tree line in accordance with the binary bits;

  For each leaf node, the number corresponding to each bit is converted into binary information stored in the node;

  Then is the update interval, the interval query;

•Code

  CodeForces242E(1).cpp

• CF242E upgraded version

  Sources with the title [ 2019ICPC Asian regional tournament Yinchuan Cup qualifiers station network A. Simple Data Structures ]

  CF242E compared to this question, the operation of the two sequences increases:

    (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$;

    (2)$2,l,r,x\ :\ \forall\ i \in[l,r],a_i = a_i\ xor\ x$;

    (3)$2,l,r,x\ :\ \forall\ i \in[l,r],a_i = a_i\ |\  x$

    (4)$2,l,r,x\ :\ \forall\ i \in[l,r],a_i = a_i\ \&\  x$

• ideas

  And similar ideas on the topic, taking into account the opening 20 above tree line maintenance operations;

  Difficulty is how lazy, and how pushDown ();

  Taking into account ^, |, & characteristics;

  For the i-th bit of a binary number, it is assumed to be 'bit [i];

  易得 bit[i] ^ 0 = bit[i] , bit[i] | 0 = bit[i] , bit[i] &1 = bit[i];

     bit [i] ^ 1: corresponds to the reverse bit [i];

     bit[i] | 1 = 1;

     bit[i] & 0 = 0;

  That is only after three operations to be useful;

  Considering the bit [i] may be done sequentially operate than before;

  But one thing is certain, encountered bit [i] | 1, bit [i] = 1, no matter what kind of operation it has done before;

  Similarly, encountered bit [i] & 0, bit [i] = 0;

  If 0 & encountered first, in the face ^ 1, corresponding to the bit [i] value of 1;

  Conversely, if the first encounter | 1, in the face ^ 1, is equivalent to bit [i] assigned to 0;

  In this case, the definition of lazy there are four values:

    $lazy=\begin{cases} -1\ ,\ no\ operate\\ \ 0\ \ ,\ become\ 0 \\ \ 1\ \ ,\ become\ 1 \\ \ 2\ \ ,\ reverse\end{cases}$;

  lazy lazy and passed down a child node labeled lazy time, in conjunction with the parent node is updated;

•Code

  CodeForces242E(2).cpp

Guess you like

Origin www.cnblogs.com/violet-acmer/p/11493713.html