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
  1. 2 4
  2. 1 6 3 5
  3. 1 4
  4. 3 4
  5. 1 2
  6. 1 2
output
  1. 1
  2. 3
  3. 3
  4. 3

线段树:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. const int N=200006;
  8.  
  9. int n, m, A[N];
  10.  
  11. struct Segment_Tree {
  12. struct Node {
  13. int s, t, dep, val;
  14. void init(int L, int R, int a) {
  15. s=L, t=R, dep=a, val=A[L];
  16. }
  17. } T[N<<2];
  18.  
  19. void pushUp(Node &fa, Node &L, Node &R) {
  20. if(fa.dep&1) fa.val=L.val|R.val;
  21. else fa.val=L.val^R.val;
  22. }
  23.  
  24. void build(int id, int dep, int L, int R) {
  25. T[id].init(L, R, dep);
  26. if(L==R) return;
  27. int mid=(L+R)>>1;
  28. build(id<<1, dep-1, L, mid);
  29. build(id<<1|1, dep-1, mid+1, R);
  30. pushUp(T[id], T[id<<1], T[id<<1|1]);
  31. }
  32.  
  33. void update(int id, int pos, int val) {
  34. if(T[id].s==T[id].t) {
  35. T[id].val=val;
  36. return;
  37. }
  38. int mid=(T[id].s+T[id].t)>>1;
  39. if(pos<=mid) update(id<<1, pos, val);
  40. else update(id<<1|1, pos, val);
  41. pushUp(T[id], T[id<<1], T[id<<1|1]);
  42. }
  43. } tree;
  44.  
  45. int main() {
  46. // freopen("in", "r", stdin);
  47. scanf("%d%d", &n, &m);
  48. int old=n;
  49. n=(1<<n);
  50. for(int i=1; i<=n; i++) scanf("%d", &A[i]);
  51. tree.build(1, old, 1, n);
  52. for(int i=0, p, q; i<m; i++) {
  53. scanf("%d%d", &p, &q);
  54. tree.update(1, p, q);
  55. printf("%d\n", tree.T[1].val);
  56. }
  57. return 0;
  58. }

单点更新二叉树 模板

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <set>
  10. #include <map>
  11. #include <bitset>
  12. #include <queue>
  13. #include <deque>
  14. #include <stack>
  15. #include <complex>
  16. #include <cassert>
  17.  
  18. using namespace std;
  19.  
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fs first
  23. #define sc second
  24. #define sz(s) int((s).size())
  25. #ifdef LOCAL
  26. #define eprintf(...) fprintf(stderr, __VA_ARGS__)
  27. #define LLD "%lld"
  28. #else
  29. #define eprintf(...) 42
  30. #define LLD "%I64d"
  31. #endif
  32. #define next _next
  33. #define prev _prev
  34. #define rank _rank
  35. #define link _link
  36. #define hash _hash
  37.  
  38. typedef long long ll;
  39. typedef long long llong;
  40. typedef unsigned long long ull;
  41. typedef unsigned int uint;
  42. typedef pair <int, int> pii;
  43. typedef vector <int> vi;
  44. typedef complex <double> tc;
  45.  
  46. const int inf = int(1e9);
  47. const double eps = 1e-9;
  48. const double pi = 4 * atan(double(1));
  49. const int N = 17;
  50.  
  51. int n;
  52. int rmq[(1 << (N + 2))];
  53.  
  54. inline void update(int pos, int val){
  55. pos += (1 << n);
  56. rmq[pos] = val;
  57. int lev = 0;
  58. while(pos > 1){
  59. pos /= 2;
  60. if(lev == 0){
  61. rmq[pos] = (rmq[pos * 2] | rmq[pos * 2 + 1]);
  62. }
  63. else{
  64. rmq[pos] = (rmq[pos * 2] ^ rmq[pos * 2 + 1]);
  65. }
  66. lev = 1 - lev;
  67. }
  68. }
  69.  
  70. int main(){
  71. #ifdef LOCAL
  72. freopen("input.txt", "r", stdin);
  73. freopen("output.txt", "w", stdout);
  74. #endif
  75. int m;
  76. scanf("%d %d", &n, &m);
  77. for(int i = 0; i < (1 << n); i++){
  78. int cur;
  79. scanf("%d", &cur);
  80. update(i, cur);
  81. }
  82. for(int i = 0; i < m; i++){
  83. int pos, val;
  84. scanf("%d %d", &pos, &val);
  85. pos--;
  86. update(pos, val);
  87. printf("%d\n", rmq[1]);
  88. }
  89. return 0;
  90. }

单点更新线段树 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. 设计模式6:Composite

    Entry.java: package gendwang.cisco.com; public abstract class Entry { private int height = 0; privat ...

  2. GDI 总结三: CImage类使用

    前言          CImage类是基于GDI+的.可是这里为什么要讲归于GDI? 主要是基于这种考虑: 在GDI+环境中,我们能够直接使用GDI+ ,没多少必要再使用CImage类 可是,假设再 ...

  3. TWinControl.DefaultHandler处理WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC消息的两个参数很有意思,两个都是传递句柄

    procedure TWinControl.DefaultHandler(var Message); begin then begin with TMessage(Message) do begin ...

  4. hdu 4778 Rabbit Kingdom(减少国家)

    题目链接:hdu 4778 Rabbit Kingdom 题目大意:Alice和Bob玩游戏,有一个炉子.能够将S个同样颜色的宝石换成一个魔法石.如今有B个包,每一个包里有若干个宝石,给出宝石的颜色. ...

  5. ASP.NET - 在线编辑器(KindEditor)

    效果: 项目结构: 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  6. Spring Security Source Code -- 验证标准流程

    除了初始阶段: 主干验证流程链: MyInvocationSecurityMetadataSource.getAttributes(Object) line: 43     MyFilterSecur ...

  7. 使用gdb调试游戏服务器

    前言 谈论gdb重要性 一般来说.提gdb,命令用于调试."命令",用户是几乎相同的复杂话.而事实确实如此,实际的开发调试必须用到gdb. 如今.大多数Linux系统是存在于ser ...

  8. linux运维常用命令集

    1.删除0字节文件 find -type f -size 0 -exec rm -rf {} \;   2.查看进程 按内存从大到小排列 PS -e   -o "%C   : %p : %z ...

  9. DLL五篇

    http://www.cnblogs.com/NeuqUstcIim/archive/2009/01/12/1374511.htmlhttp://www.cnblogs.com/NeuqUstcIim ...

  10. UVA 10003 Cutting Sticks

    题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...