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

输入示例

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

输出示例

1
3
3
3

数据规模及约定

见“输入

题解

我们发现“运算之后得到新序列”这一过程只会进行 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;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5833913.html

[codeforces 339]D. Xenia and Bit Operations相关推荐

  1. 线段树——思维(Codeforces 339D Xenia and Bit Operations/Billboard HDU - 2795)

    Codeforces 339D Xenia and Bit Operations vj地址 题意:给出2的n次方个数,每次将现在这个序列中相邻的两个数运算后合并为一个数,得到一个新的序列,这个新序列的 ...

  2. [codeforces 339]E. Three Swaps

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

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

    题意:定义一个长度2^n的序列{a1, a2-an},序列相邻两个元素或运算之后再进行异或运算再进行或运算(两种运算交替进行)直到只剩下一个数字,这个数字即为序列的值,输入第一行两个数n,m表示序列的 ...

  4. 【CodeForces - 357D】Xenia and Hamming (字符串问题,数论,思维)

    题干: Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance. T ...

  5. Codeforces Global Round 23 C. Permutation Operations

    赛时随便写了个做法,看standing,发现很多人的做法也是很奇特 赛后看了官方题解..翻译下 题意: 每次可以给一个任意的后缀里的所有数 + i +i +i 执行n次. 使得逆序对最少. 思路: t ...

  6. 【Codeforces 339C】Xenia and Weights

    [链接] 我是链接,点我呀:) [题意] 在天平上放砝码 你要在左边放一下然后到右边放一下 一直重复这样放m次 每次你放在其中一边都要让另外一边的重量比你少 你可以用1~10中的某些砝码 问你要怎样放 ...

  7. Codeforces-339D. Xenia and Bit Operations

    传送门 给定n,m,代表有2n个数,m次询问 每次询问时先将此时序列第p个数值改为b,在求序列的值.序列的值定义如下. 将序列中的第2*i项与2*i-1项或,得到新序列,再把新序列的第2*i项与2*i ...

  8. CF339D Xenia and Bit Operations(线段树)

    乱搞线段树,单点修改,每次询问整个表达式的值 不同点在于up函数需要进行修改,根据区间长度不同运算符号也有所不同 代码: #include <bits/stdc++.h> #define ...

  9. CF 2022寒假练习

    CF 2022寒假练习 CF_2A Winner 链接 CF_2A Winner 题目大意 有一个游戏,由nnn个玩家参与,每一轮会有一个玩家获得sss点数.游戏最后一轮结束后,点数最多的玩家获胜.如 ...

最新文章

  1. 中国工程院2021年院士增选第二轮候选人名单公布
  2. 使用fluentd管理docker日志
  3. centos安装ES(elasticsearch)
  4. P2323-[HNOI2006]公路修建问题【并查集】
  5. 程序员面试金典 - 面试题 03.01. 三合一(数组栈)
  6. 《Accelerated C++中文版》--- 读书笔记
  7. Flash捕神--swf seeker 下载试用版
  8. 在DOS中使用系统还原工具
  9. 【转】opencv中widthStep不一定等于width*nChannels的原因
  10. JavaEE之Filter过滤器、登录状态验证、敏感词汇过滤
  11. NMF扩展名是什么文件
  12. 蓝牙耳机南卡和vivo哪个好用?南卡与vivo实际评测!
  13. Windows11图片密码的设置方法(用喜欢的图片作为开机密码)
  14. 记录下echarts Map3D地图渐变
  15. oracle ppt讲义,oracle课件.ppt
  16. 数据库系统,数据库,数据库管理系统
  17. 比ownCloud/Nextcloud更好用的免费私有企业网盘
  18. 攻防世界MISC练习区(this_is_flag 到 坚持60s)
  19. 【计算机网络 (谢希仁) 习题题解】第4章 网络层 (1)
  20. 【蓝桥杯】第十四届模拟赛第一期及第二期填空汇总

热门文章

  1. 2010年厦门商报报导《监控》小说
  2. 在GridView里做单选按钮,总结了三种方法
  3. 批量替换文本中字符代码-python3
  4. 中修改环境变量_嵌入式 Linux下永久生效环境变量bashrc
  5. python讲1020逆序输出_手把手带你学 Python3(九)| 快速实现数据处理的不二工具(文末有彩蛋)...
  6. 反arp攻击软件_网络安全工程师教Kali Linux:ARP欺骗概述
  7. java 80端口_Java80端口占用异常解决方法
  8. go语言的iota是什么意思_关于Golang中的iota
  9. linux6用户t密码,linux系统 用户和组管理类命令的使用方法
  10. 点击鼠标左键 自动锁定计算机图标,鼠标一按左键桌面图标就消失了怎么办_为什么按鼠标左键时桌面图标都不见了...