• 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
  • 相关阅读:
    团队项目01应用场景
    团队项目冲刺第一阶段03
    团队冲刺第一阶段02
    团队冲刺第一阶段01
    基于 Scrapy-redis 的分布式爬虫详细设计
    创建CrawlSpider爬虫简要步骤
    如何将redis中的数据导入到本地MongoDB和MySQL数据库
    远程访问ubuntu下mysql的问题
    vi命令汇总
    Chrome/FireFox处理JSON的插件
  • 原文地址:https://www.cnblogs.com/smallhester/p/10300550.html
Copyright © 2020-2023  润新知