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

Note

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

题解:把题目给出的所有元素构建一颗线段树,对于更新操作,类似于线段树的操作,从下往上处理线段树的子区间,用当前层的深度来确定对每一层实施什么样的擦操作。然后pushup操作更新当前区间。时间复杂度O(logn)

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int a[1<<18];
int n,m;
int dfs(int x,int dep,int l,int r){if(l == r){return a[x];}int mid = (l + r) / 2;int ls = dfs(2*x,dep+1,l,mid);int rs = dfs(2*x + 1,dep + 1,mid + 1,r);if((n - dep) % 2 == 0){//ora[x] = ls | rs;}else{a[x] = ls ^ rs;}return a[x];
}
int main(){scanf("%d%d",&n,&m);for(int i = 0;i < (1<<n);i++){int tmp;scanf("%d",&tmp);a[(1<<n)+i] = tmp;}dfs(1,1,1,(1<<n));while(m--){int p,b;scanf("%d%d",&p,&b);int fa = (1<<n) + p - 1;a[fa] = b;int dep = 0;while(fa != 1){fa = fa / 2;if(dep % 2 == 0)a[fa] = a[fa*2] | a[fa*2+1];elsea[fa] = a[fa*2] ^ a[fa*2+1];dep ++;}printf("%d\n",a[1]);}return 0;
} 

Summer Training day6 coseforces339D 线段树、位操作相关推荐

  1. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  2. hdu 4391 Paint The Wall 线段树 +优化 2012 Multi-University Training Contest 10 )

    http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意: 刷墙, 以开始 有 n个节点,每个节点有一种颜色 ,m 次询问 m次  输入 a,l,r,z 如果 ...

  3. 2016 UESTC Training for Data Structures F - 郭大侠与“有何贵干?” CDOJ 1335 线段树 扫描线 离散化

    F - 郭大侠与"有何贵干?" 就是给一个三维空间,和N个长方体,问覆盖K次的体积 x和y都是1e9,但是z是[1,3],所以可以把这个分为两个二维平面,求被覆盖K次的面积,最后加 ...

  4. hdu6681 Rikka with Cake【线段树】【离散化】【2019 Multi-University Training Contest 9】

    题意: 在一个封闭的n*m的矩形内,有k条射线,有四个方向,上下左右,射线,射线的端点不重合,该矩形内有多少个封闭的区域 1≤n,m≤1e9,1≤k≤1e51\le n,m \le 1e9 ,1\le ...

  5. HDU5726 线段树求解区间GCD

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  6. 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

    题干: Just days before the JCPC, your internet service went down. You decided to continue your trainin ...

  7. CDOJ-1057 秋实大哥与花(线段树区间更新)

    秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  8. 「hdu6681」Rikka with Cake【线段树】

    Rikka with Cake Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) T ...

  9. Breaking Down News【HDU-6856】【线段树+单调队列/栈】

    2020 Multi-University Training Contest 8 B 有N个权值,每个权值只能是{-1, 0, 1}中的一个,然后现在分成几段区间,每一段的长度在[L, R]之间,每一 ...

最新文章

  1. 谷歌用AI训练“耳机线”,实现了触摸屏大多数功能
  2. MTCNN人脸及特征点检测---代码应用详解c++
  3. 简单的C++程序题总结
  4. 常用FTP命令 1. 连接ftp服务器
  5. 七、有机硅柔软剂在不同发展阶段分子结构特征及主要解决的问题?
  6. 互联网日报 | 美团市值突破万亿港元;北京恢复二级响应;滴滴货运23日上线;微信开放MCN入驻...
  7. 经济师计算机考试取消,2019年经济师考试计算机机考答题要求及说明
  8. 珍惜平时一点一滴,这几个值得跟进学习的阿里、滴滴、微软超级牛人的公众号!...
  9. 微信分享链接php,微信实现分享链接的缩略图和标题
  10. Java打印菱形源码及介绍
  11. php随机生成卡密,PHP随机生成不反复的8位卡号(数字)和卡密(字符串)_后端开发...
  12. Laravel单元测试
  13. SSM毕设项目职业性格测试系统7c78o(java+VUE+Mybatis+Maven+Mysql)
  14. 伙伴们,小毛祝你们新的一年神马都给力!!!
  15. java: 程序包com.zyt.hm.VO不存在
  16. dad my_英文绘本 || My Dad!《我爸爸》
  17. Java进阶(七)Java加密技术之非对称加密算法RSA
  18. IPv4如何向IPv6过渡?IPv6改造方案有哪些?
  19. 2018阿里巴巴全球诸神之战创客大赛总决赛即将举行
  20. mysql不等于null和等于null的写法

热门文章

  1. spyder pyecharts不显示_我的显示器需要定时校色吗?
  2. aqs java 简书,Java AQS源码解读
  3. leetcode18. 四数之和(双指针)
  4. python通过tkinter和json界面库实现考研知识点统计
  5. 二叉树的锯齿形层次遍历
  6. 2019-02-25-算法-进化
  7. Hills And Valleys CodeForces - 1467B 思维
  8. 2020牛客多校第1场I-1 or 2一般图最大匹配带花树
  9. 牛客练习赛76 E 牛牛数数(线性基加二分)
  10. 2019牛客多校第一场