Wednesday, 7 October 2015

*D. Xenia and Bit Operations


D. Xenia and Bit Operations

Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequencea1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.
Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  → (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.
You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional mqueries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.
Input
The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.
Output
Print m integers — the i-th integer denotes value v for sequence a after the i-th query.
Sample test(s)
input
2 4
1 6 3 5
1 4
3 4
1 2
1 2
output
1
3
3
3

------------------------------------------------------editorial---------------------------------------------------------
/* my-- while building the tree at odd level(starting from leaf at level count =1 ) do  or and at even do xor*/
The problem could be solved by using a typical data structure (segment tree).
The leafs of the segment tree will store the values of ai. At the vertices, the distance from which to the leafs is 1, we will store OR of the numbers from the leafs, which are the sons of this node in the segment tree. Similarly, vertices, the distance from which to the leafs is 2, we will store Xor of the numbers stored in their immediate sons. And so on. Then, the root of the tree will contain the required value v.
There is no need to rebuild all the tree to perform an update operation. To do update, we should find a path from the root to the corresponding leaf and recalculate the values only at the tree vertices that are lying on that path. If everything is done correctly, then each update query will be executed in O(n) time. Also we need O(2n) memory.
dsnas
---------------------------------------------------code--------------------------------------------------------------
#include<iostream>
using namespace std;
int arr[1010000];
#include<bits/stdc++.h>
typedef int lli;
lli laz[10100000];
#define inf 999999999
struct abcd
{
lli lev;
lli val;
}tree[1010000];

void update(lli node ,lli start,lli end,lli r1,lli r2,lli val)
{
//if(c--==0) exit(0);
// cout<<"start "<<start<<" end "<<end<<endl;
  if(r1<=start && r2>=end)
   {
  // cout<<"set base "<<endl;
     tree[node].val=val;
     tree[node].lev=1;
     return ;
   }
 if(r1>end  || r2<start   || start>end) return  ;
  
    update(2*node, start,(start+end)/2,r1,r2,val);
    update(2*node+1, ((start+end)/2)+1,end,r1,r2,val);
    lli leve=tree[2*node].lev;
    if(leve%2==1)
     {
  //   cout<<" set for node "<<node<<"val "<<tree[node].val<<endl;
      tree[node].val=tree[2*node].val | tree[2*node+1].val;
      tree[node].lev=leve+1;
      //cout<<" set for node "<<node<<"val "<<tree[node].val<<endl;
}
    else
    {
    tree[node].val=tree[2*node].val  ^ tree[2*node+1].val;
      tree[node].lev=leve+1;
      //cout<<" set for node "<<node<<"val "<<tree[node].val<<endl;
}
 
   }
 
void build(lli node , lli start,lli end)
 {
 // cout<<"start "<<start<<" end "<<end<<endl;
  if(start==end) 
  {
  tree[node].val=arr[start];
  tree[node].lev=1;
  }
  else if(start>end) return ;
  else
   {
    build(2*node,start,(start+end)/2);
    build(2*node+1,((start+end)/2)+1,end);
    lli leve=max(tree[2*node].lev,tree[2*node+1].lev);
    if(leve%2==1)
     {
      tree[node].val=tree[2*node].val | tree[2*node+1].val;
      tree[node].lev=leve+1;
}
    else
    {
    tree[node].val=tree[2*node].val  ^ tree[2*node+1].val;
      tree[node].lev=leve+1;
}
   }
   
 }
int main()
 {
  
   lli n,q;
  cin>>n>>q;
  n=pow(2,n);
     for(lli i=0;i<n;i++)
      scanf("%d",&arr[i]);
       build(1,0,n-1);
   
       
     
      
       
        while(q--)
         {
  
             
             lli index,val;
              cin>>index>>val;
              update(1,0,n-1,index-1,index-1,val);
              cout<<tree[1].val<<endl;
             
             
         }
       
  return 0;
}

No comments:

Post a Comment