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.

Sample test(s)
input
2 4
1 6 3 5
1 4
3 4
1 2
1 2
output
1
3
3
3

线段树:

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int N=200006; int n, m, A[N]; struct Segment_Tree {
struct Node {
int s, t, dep, val;
void init(int L, int R, int a) {
s=L, t=R, dep=a, val=A[L];
}
} T[N<<2]; void pushUp(Node &fa, Node &L, Node &R) {
if(fa.dep&1) fa.val=L.val|R.val;
else fa.val=L.val^R.val;
} void build(int id, int dep, int L, int R) {
T[id].init(L, R, dep);
if(L==R) return;
int mid=(L+R)>>1;
build(id<<1, dep-1, L, mid);
build(id<<1|1, dep-1, mid+1, R);
pushUp(T[id], T[id<<1], T[id<<1|1]);
} void update(int id, int pos, int val) {
if(T[id].s==T[id].t) {
T[id].val=val;
return;
}
int mid=(T[id].s+T[id].t)>>1;
if(pos<=mid) update(id<<1, pos, val);
else update(id<<1|1, pos, val);
pushUp(T[id], T[id<<1], T[id<<1|1]);
}
} tree; int main() {
// freopen("in", "r", stdin);
scanf("%d%d", &n, &m);
int old=n;
n=(1<<n);
for(int i=1; i<=n; i++) scanf("%d", &A[i]);
tree.build(1, old, 1, n);
for(int i=0, p, q; i<m; i++) {
scanf("%d%d", &p, &q);
tree.update(1, p, q);
printf("%d\n", tree.T[1].val);
}
return 0;
}

单点更新二叉树 模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <complex>
#include <cassert> using namespace std; #define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(s) int((s).size())
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define LLD "%lld"
#else
#define eprintf(...) 42
#define LLD "%I64d"
#endif
#define next _next
#define prev _prev
#define rank _rank
#define link _link
#define hash _hash typedef long long ll;
typedef long long llong;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef complex <double> tc; const int inf = int(1e9);
const double eps = 1e-9;
const double pi = 4 * atan(double(1));
const int N = 17; int n;
int rmq[(1 << (N + 2))]; inline void update(int pos, int val){
pos += (1 << n);
rmq[pos] = val;
int lev = 0;
while(pos > 1){
pos /= 2;
if(lev == 0){
rmq[pos] = (rmq[pos * 2] | rmq[pos * 2 + 1]);
}
else{
rmq[pos] = (rmq[pos * 2] ^ rmq[pos * 2 + 1]);
}
lev = 1 - lev;
}
} int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int m;
scanf("%d %d", &n, &m);
for(int i = 0; i < (1 << n); i++){
int cur;
scanf("%d", &cur);
update(i, cur);
}
for(int i = 0; i < m; i++){
int pos, val;
scanf("%d %d", &pos, &val);
pos--;
update(pos, val);
printf("%d\n", rmq[1]);
}
return 0;
}

单点更新线段树 RMQ的更多相关文章

  1. HDU 1754 I Hate It 线段树RMQ

    I Hate It Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=175 ...

  2. 《白书》上线段树RMQ的实现

    白书上的线段树RMQ实现,自己重写了一遍: #include <bits/stdc++.h> using namespace std; const int MAXN=1<<17 ...

  3. HDU-1698-Just a Hook-区间更新+线段树成段更新

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...

  4. Hlg 1832 【线段树 && RMQ】.cpp

    题意: 在给出的区间内求出最大买进卖出的差价. 思路: 对于弱数据:维护一个从左到右的最大差价和最小值.即当发现当前值比最小值小的时候更新最小值,否则看一下当前值与之前最小值的差价是否比最大差价大,是 ...

  5. C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)

    题目链接:http://codeforces.com/contest/799/problem/C 题目: 题意: 给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C ...

  6. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  7. CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...

  8. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  9. PKU 2823 Sliding Window(线段树||RMQ||单调队列)

    题目大意:原题链接(定长区间求最值) 给定长为n的数组,求出每k个数之间的最小/大值. 解法一:线段树 segtree节点存储区间的最小/大值 Query_min(int p,int l,int r, ...

随机推荐

  1. linux安装Eclipse c++环境

    yum install eclipse     yum install eclipse-cdt

  2. EXT2/EXT3文件系统(一)

    整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ 1.文件系统概念引入 文件系统是一种存储和组织计算机数据的方法,它使 ...

  3. C++建立动态二维数组

    C++建立动态二维数组主要有两种方法: 1.使用数组指针,分配一个指针数组,将其首地址保存在b中,然后再为指针数组的每个元素分配一个数组                           int * ...

  4. Swift - 启动时的向导页(新手引导)的制作

    在很多iOS产品或者一些应用版本的升级中,新手指导都是一个常用的功能,通过说明页的左右滑动,可以很清晰的展示系统的一些功能特性.制作思路如下: 1,如何检测应用是第一次登陆启动 我们可以使用NSUse ...

  5. javascript中外部js文件取得自身完整路径得办法

    原文:javascript中外部js文件取得自身完整路径得办法 有时候我们需要引入一个外部js文件,这个js文件又需要用到自己的路径或者是所在的目录,别问怎么又这么变态的需求,开发做久了各种奇葩需求也 ...

  6. 利用开源HTML5引擎lufylegend.js结合javascript实现的五子棋人机对弈

    前言     本文主要介绍利用开源引擎 lufylegend.js开发基于Html5的游戏--五子棋,主要叙述其详细开发过程. 游戏规则 玩过五子棋的都应该知道五子棋的规则,这里就简单介绍其规则. 1 ...

  7. 状态压缩动态规划 -- 棋盘问题 POJ 1321

    一个 N * N 的棋盘上面,有些格子不能放,放置 M 的棋子, 每两个棋子不能在同一行或者同一列,问有多少种放法 DFS太慢,用SCR好点点 Python 仅仅有 22 行,事实上能够更短.可是得排 ...

  8. ESP8266学习笔记1:怎样在安信可全功能測试板上实现ESP-01的编译下载和调试

    近期调试用到了安信可的ESP-01模块,最终打通了编译下载调试的整个通道,有一些细节须要记录,方便兴许的开发工作. 转载请注明:http://blog.csdn.net/sadshen/article ...

  9. opencv MAT数据操作

    1.存取单个像素值 最通常的方法就是 img.at<uchar>(i,j) = 255; img.at<Vec3b>(i,j)[0] = 255; 2.用指针扫描一幅图像 对于 ...

  10. 开源搜索引擎评估:lucene sphinx elasticsearch

    开源搜索引擎评估:lucene sphinx elasticsearch 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch sphinx,c++开发,简 ...