zoukankan      html  css  js  c++  java
  • Xenia and Bit Operations CodeForces

     Xenia and Bit Operations CodeForces - 339D 

    Xenia the beginner programmer has a sequence a, consisting of 2nnon-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 sequence a1 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 aafter the i-th query.

    Examples

    Input
    2 4
    1 6 3 5
    1 4
    3 4
    1 2
    1 2
    Output
    1
    3
    3
    3

    Note

    For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

    题意:给出一个长度为2^n序列,和几次查询,每次都将其中下标的某值改掉,之后两两或操作,得到2^(n-1)的长度后开始异或,之后在或,直至只有一个数字

    题解:线段树的操作,在push_up的时候考虑一下层数是 | 还是 ^ ;

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<cstdlib>
    #include <vector>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    #define PI acos(-1.0)
    const int maxn =  1e7+10;
    const int  mod = 1e9+7;
    
    int a[maxn];
    int date[maxn];
    int sum[maxn];
    
    void push_up(int i)
    {
        if(date[i]%2 == 0)
            sum[i] = sum[i<<1|1] | sum[i<<1];
        else
            sum[i] = sum[i<<1|1] ^ sum[i<<1];
    }
    void build(int i,int l,int r)
    {
        sum[i] = 0;
        date[i << 1] = date[i<<1 | 1] = 0;
        if(l == r)
        {
            sum[i] = a[l];  date[i] = -1;   return;
        }
        int mid = (l+r) >> 1;
        build(i<<1,l,mid);
        build((i<<1)|1,mid+1,r);
        date[i] = date[i<<1]+1;
        push_up(i);
    }
    
    void update(int l,int r,int p,int d,int i)
    {
        if(l == r)
        {
            sum[i] = d;
            return;
        }
        int mid = (l+r)>>1;
        if(p <= mid)
            update(l,mid,p,d,i<<1);
        else
            update(mid+1,r,p,d,i<<1|1);
        push_up(i);
    }
    
    
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int num=(1<<n);
        for(int i=1;i<=num;i++)
            scanf("%d",&a[i]);
        date[1]=1;
        build(1,1,num);
        for(int i=1;i<=m;i++)
        {
            int p,b;
            scanf("%d%d",&p,&b);
            update(1,num,p,b,1);
            printf("%d
    ",sum[1] );
        }
    }
    View Code
  • 相关阅读:
    Flash/Flex学习笔记(14):制作涂鸦板
    Flash/Flex学习笔记(9):ActionScript3.0与Javascript的相互调用
    Flash/Flex学习笔记(15):FMS 3.5之远程共享对象(Remote Shared Object)
    vs2008 自动属性
    上传图片 水印位置计算 记录一下
    产生一个int数组,长度为100,并向其中随机插入1100,并且不能重复 的另一种写法
    关于存储过程 select top n 参数问题
    datagrid 和 gridview 与checkbox 获取所选中的id的方法
    如何在webservice中取得sesssionid
    asp.net 将新添加的文件直接加上版权信息
  • 原文地址:https://www.cnblogs.com/smallhester/p/10300550.html
Copyright © 2011-2022 走看看