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. (升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)

    本课程主要讲解目前大数据领域最热门.最火爆.最有前景的技术——Spark.在本课程中,会从浅入深,基于大量案例实战,深度剖析和讲解Spark,并且会包含完全从企业真实复杂业务需求中抽取出的案例实战.课 ...

  2. boost::asio async_write也不能保证一次发完所有数据 一

    你要是看过basic_stream_socket的文档,里面提到async_write_some不能保证将所有要发送的数据都发出去.并且提到如果想这样做,需要使用boost asio的async_wr ...

  3. Unity3D手游-横版ACT游戏完整源代码下载

    说明: 这不是武林.这不是江湖,没有道不完的恩怨,没有斩不断的情仇,更没有理不清的烦恼,这是剑的世界,一代剑魁闯入未知世界,将会为这个世界展开什么样的蓝图.让你来创造它的未来,剑魁道天下,一剑斗烛龙! ...

  4. BootStrap - FileUpload美化样式

    效果: 代码: <div class="panel panel-default" style="border: 1px solid #ffd800;"&g ...

  5. JSTL解析——001

    JSTL 全称jsp standard tag library ,即jsp标准标签库. 是不是想问标签是什么东西? 标签就是jsp专门用于显示数据的,可重复利用的类库: 是不是想问标签由那些部分组成的 ...

  6. jquery控制动态生成的gridview中多列checkbox的全选反选及自动判断是否全选状态

    动态生成的Gridview的前台html代码如下:     <table class="usertableborder" cellspacing="0" ...

  7. hdu 4090 GemAnd Prince

    题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量  的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...

  8. Java加入背景音乐

    近期有几个师妹找我给她们的Java期末作业加入背景音乐,非常久不琢磨Java的我花费整晚才搞定,羞愧.在博客中记录下来.警示自己.也帮助一下大家. Java中能够通过AudioClip类来实现音乐播放 ...

  9. 百度地图 javascript相关Bug搜集

    一 在手机里用百度地图js版做webapp   bug集合 1 之前用2.0版本的时候发现只要地图添加了覆盖物,无论数量多少,当地图放大到很小的范围时候,会卡死 1.1 当时处理办法:将版本降低至1. ...

  10. 在StatusBar中显示当前时间

    在StatusBar中显示当前时间,如下: 1.在String Table中插入一项 (注意:状态栏将根据字符串的长度来确定相应窗格的缺省宽度,所以指定为00:00:00就为时间的显示预留了空间)   ...