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+;
const int mod = 1e9+; int a[maxn];
int date[maxn];
int sum[maxn]; void push_up(int i)
{
if(date[i]% == )
sum[i] = sum[i<<|] | sum[i<<];
else
sum[i] = sum[i<<|] ^ sum[i<<];
}
void build(int i,int l,int r)
{
sum[i] = ;
date[i << ] = date[i<< | ] = ;
if(l == r)
{
sum[i] = a[l]; date[i] = -; return;
}
int mid = (l+r) >> ;
build(i<<,l,mid);
build((i<<)|,mid+,r);
date[i] = date[i<<]+;
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)>>;
if(p <= mid)
update(l,mid,p,d,i<<);
else
update(mid+,r,p,d,i<<|);
push_up(i);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
int num=(<<n);
for(int i=;i<=num;i++)
scanf("%d",&a[i]);
date[]=;
build(,,num);
for(int i=;i<=m;i++)
{
int p,b;
scanf("%d%d",&p,&b);
update(,num,p,b,);
printf("%d\n",sum[] );
}
}

Xenia and Bit Operations CodeForces - 339D的更多相关文章

  1. [线段树]Codeforces 339D Xenia and Bit Operations

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. [codeforces 339]D. Xenia and Bit Operations

    [codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...

  3. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  4. codeforces 339C Xenia and Bit Operations(线段树水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Bit Operations Xenia the beginn ...

  5. Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. cf339d Xenia and Bit Operations

    Xenia and Bit Operations Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Xenia and Bit Operations(线段树单点更新)

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. [Codeforces 339D] Xenia and Bit Operations

    [题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...

  9. CodeForces 339D Xenia and Bit Operations (线段树)

    题意:给定 2的 n 次方个数,对这些数两个两个的进行或运算,然后会减少一半的数,然后再进行异或运算,又少了一半,然后再进行或运算,再进行异或,不断重复,到最后只剩下一个数,要输出这个数,然后有 m ...

随机推荐

  1. Likely root cause: java.lang.IllegalStateException: jar hell!

    jar hell 解决方案: 问题: Likely root cause: java.lang.IllegalStateException: jar hell! 解决: 当一一个类或者一一个资源文件存 ...

  2. Android入门:Activity生命周期

    一.Activity生命周期介绍 我们在学Java Web时会学到Servlet的生命周期,因此对生命周期的概念已经有一定了解,简单地说就是某个事物从出生到死亡的过程. Activity也存在声明周期 ...

  3. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第八天(非原创)

    文章大纲 一.课程介绍二.Solr基本介绍三.ssm整合Solr四.项目源码与资料下载五.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的 ...

  4. JAVA 面试重点知识个人总结

    一.集合: 1 .Collection(是java.util下的接口) 和 Collections(是java.util下的类). 2 .List, Set,是否继承自Collection接口,Map ...

  5. Java并发(六):并发策略

    通过多次优化实例来了解选择并发策略的正确姿势 通过模拟浏览器程序的渲染页面(Page-Rendering)功能,为了方便,假设HTML页面只会包含标签文本和图片以及URL; 第一个版本:串行加载页面元 ...

  6. 树莓派-(一)开箱到点亮一些坑(无屏、无wlan、无直连键鼠)

    0x00.前期准备: 材料: 树莓派3b+ 板子 * 1,适配电源 * 1,网线 * 2,sd卡16G * 1,读卡器 * 1 安装时注意,3b+三个散热片贴好.小风扇接线要接对 工具: 0x01. ...

  7. 使用startForeground让android服务前台运行

    最近在使用android 4.1系统的时候,发现在手机休眠一段时间后(1-2小时),后台运行的服务被强行kill掉,有可能是系统回收内存的一种机制,要想避免这种情况可以通过startForegroun ...

  8. DDL与DML语句

    1. DDL语句 SQL语句:结构化查询语句,使用SQL与数据库“沟通”,完成相应的数据库操作. l DDL:数据定义语言,用来维护数据库对象 1.1 创建表 Ø CREATE:创建表 演示:创建员工 ...

  9. pat乙级1045

    从左到右扫描时记录扫描到当前下标为止的最大值,如果当前元素大于这个最大值,那么它就大于它左边的所有值.同理,从右到左扫描记录扫描到当前下标为止的最小值,如果当前元素小于这个最大小值,那么它就小于它右边 ...

  10. java Vamei快速教程21 事件响应

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在GUI中,我们看到了如何用图形树来组织一个图形界面.然而,这样的图形界面是静态的 ...