[codeforces 339]D. Xenia and Bit Operations

试题描述

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 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.

输入

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.

输出

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

输入示例


输出示例


数据规模及约定

见“输入

题解

我们发现“运算之后得到新序列”这一过程只会进行 n 次,事实上,这整个过程就像一个倒过来的完全二叉树,我们不妨将初始序列得到的这颗完全二叉树建出来;以后每改动一个数字即改变了一个叶节点的权值,受到影响需要改动的节点为该叶节点到根节点(最终答案)的路径,于是每次修改时间复杂度也是 O(n) 的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 20
#define maxm 1048576
int n, q, ToT, A[maxm], fa[maxm]; int main() {
n = read(); q = read();
for(int i = 0; i < (1 << n); i++) A[i] = read(); memset(fa, -1, sizeof(fa));
ToT = 1 << n; bool cur = 0;
int nl = 0, nr = (1 << n) - 1;
for(int i = 1; i <= n; i++, cur ^= 1) {
int nnl = -1, nnr = -1;
for(int j = nl; j < nr; j += 2) {
if(cur) A[ToT++] = A[j] ^ A[j^1], fa[j] = fa[j^1] = ToT - 1;
else A[ToT++] = A[j] | A[j^1], fa[j] = fa[j^1] = ToT - 1;
if(nnl < 0) nnl = ToT;
nnr = ToT;
}
nl = nnl; nr = nnr;
}
// printf("%d\n", ToT); while(q--) {
int p = read() - 1; A[p] = read();
cur = 0;
while(fa[p] >= 0) {
if(cur) A[fa[p]] = A[p] ^ A[p^1];
else A[fa[p]] = A[p] | A[p^1];
p = fa[p]; cur ^= 1;
}
printf("%d\n", A[ToT-1]);
} return 0;
}

[codeforces 339]D. Xenia and Bit Operations的更多相关文章

  1. 【Codeforces 339】Xenia and Bit Operations

    Codeforces 339 D 题意:给定\(2^n​\)个数字,现在把它们进行如下操作: 相邻的两个数取\(or\) 相邻的两个数取\(xor\) 以此类推,直到剩下一个数. 问每次修改一个数字, ...

  2. codeforces 339 D.Xenia and Bit Operations(线段树)

    这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了. 题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然 ...

  3. [codeforces 339]C. Xenia and Weights

    [codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...

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

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

  5. Xenia and Bit Operations CodeForces - 339D

    Xenia and Bit Operations CodeForces - 339D Xenia the beginner programmer has a sequence a, consistin ...

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

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

  7. 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 ...

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

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

  9. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

随机推荐

  1. 分析setting源代码获取sd卡大小

    分析setting源代码获取sd卡大小 android系统有一个特点,即开源,我们可以得到任何一个应用的源代码,比如我们不知道这样的android代码怎么写,我们可以打开模拟器里面的设置(settin ...

  2. jquery 解析数据库中的json日期为正常的格式

    //在action从后台数据库中请求获得日期以后,得到的是json格式的数据,因此要解析才能显示在前台1.在jsp页面写的代码如下:<html> <script> Date.p ...

  3. JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证

    本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...

  4. 安卓官方ViewPager与android.support.design.widget.TabLayout双向交互联动切换 。

    该TabLayout的功用,简单的说,就是当用户在该TabLayout的选项卡子item中选择触摸时候,文字和下方的指示器横条滑动指示.android.support.design.widget.Ta ...

  5. cmake 静态调用 c++ dll 的类的一个例子(Clion IDE)[更新1:增加1.模版的应用,2.ma 的算法]

    CMakeLists.txt project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_ ...

  6. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 包含https 非对称秘钥 NB

    JavaWeb学习总结(三)--Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...

  7. 经纬度距离计算Java实现代码

    public class test { private static double rad(double d) { return d * Math.PI / 180.0; } public stati ...

  8. html、css杂记

    1:浮动 <div style="float: left"> 2:清除浮动,把父div撑起来 <div style="clear:both"& ...

  9. js字符串转成数字的三种方法

    js读取的html代码中获得的值 ,统统是以字符串的形式呈现的,为了方便我们后面对数据的操作,有时候我们有必要进行转换一下. 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转 ...

  10. iOS-iOS 获取蓝色文件夹图片

    Xcode创建的iOS项目内存在两种文件夹:Group(黄色, 伪文件夹) 和Folder(蓝色, 真文件夹): Group: Folder: Images.xcassets或Group文件夹内的PN ...