zoukankan      html  css  js  c++  java
  • Codeforces339D(SummerTrainingDay06-A 线段树)

    D. Xenia and Bit Operations

    time limit per test:2 seconds
    memory limit per test:256 megabytes
    input:standard input
    output:standard output

    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 sequence aor a2, aor 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.

    Examples

    input

    2 4
    1 6 3 5
    1 4
    3 4
    1 2
    1 2

    output

    1
    3
    3
    3
     1 //2017-08-06
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #define ll long long
     6 #define mid ((st[id].l+st[id].r)>>1)
     7 #define lson (id<<1)
     8 #define rson ((id<<1)|1)
     9 
    10 using namespace std;
    11 
    12 const int N = 150000;
    13 int arr[N];
    14 struct Node{
    15     int l, r, OR;
    16 }st[N<<3];
    17 
    18 void build(int id, int l, int r, bool op)
    19 {
    20     st[id].l = l; st[id].r = r;
    21     if(l == r){
    22         st[id].OR = arr[l];
    23         return;
    24     }
    25     build(lson, l, mid, !op);
    26     build(rson, mid+1, r, !op);
    27     if(op)st[id].OR = st[lson].OR | st[rson].OR;
    28     else st[id].OR = st[lson].OR ^ st[rson].OR;
    29 }
    30 
    31 void update(int id, int pos, int w, bool op)
    32 {
    33     if(st[id].l == pos && st[id].r == pos){
    34         st[id].OR = w;
    35         return;
    36     }
    37     if(pos <= mid)update(lson, pos, w, !op);
    38     else if(pos > mid)update(rson, pos, w, !op);
    39     if(op)st[id].OR  = st[lson].OR | st[rson].OR;
    40     else st[id].OR  = st[lson].OR ^ st[rson].OR;
    41 }
    42 
    43 int main()
    44 {
    45     int n, m;
    46     while(scanf("%d%d", &n, &m)!=EOF)
    47     {
    48         int deep = n;
    49         n = (1<<n);
    50         for(int i = 1; i <= n; i++)
    51             scanf("%d", &arr[i]);
    52         build(1, 1, n, deep&1 ? 1 : 0);
    53         int a, b;
    54         while(m--){
    55             scanf("%d%d", &a, &b);
    56             update(1, a, b, deep&1 ? 1 : 0);
    57             printf("%d
    ", st[1].OR);
    58         }
    59     }
    60 
    61     return 0;
    62 }
     
  • 相关阅读:
    C#无限分级实现,前端WEB页面接收,后台提供层级Json数据
    消息队列、OSS常用操作封装
    ABP增删改查代码片段
    WebApi实现验证授权Token,WebApi生成文档等
    项目收集-AutoMapper使用,事务,Json.Net序列化反序列化,代码生成调用等
    ssi服务器端指令详解(shtml)
    Linq常用List操作总结,ForEach、分页、交并集、去重、SelectMany等
    81.Search in Rotated Sorted Array II---二分变形
    48.Rotate Image
    89.Gray Code
  • 原文地址:https://www.cnblogs.com/Penn000/p/7296192.html
Copyright © 2011-2022 走看看