单点更新线段树 RMQ
来源:互联网 发布:java中如何计算时间差 编辑:程序博客网 时间:2024/05/16 10:22
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 41 6 3 51 43 41 21 2
1333
线段树:
#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 _hashtypedef 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 LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);#endifint 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
- 线段树求区间最大值RMQ(单点更新)
- UVa 12299 - RMQ with Shifts 线段树 单点更新
- TOJ 4325 RMQ with Shifts / 线段树单点更新
- wustoj 1260 RMQ with Shifts 线段树单点更新
- UVA12299 RMQ with Shifts 线段树查询 单点更新
- uva 12299 - RMQ with Shifts--- 线段树单点更新
- 12299 - RMQ with Shifts(线段树单点更新、区间求最值)
- 线段树 单点更新
- 线段树单点更新
- 线段树 单点更新
- 单点更新线段树
- 线段树单点更新
- 线段树单点更新
- 线段树 单点更新
- 线段树 单点更新
- 线段树单点更新
- 线段树 单点更新
- 详解clientHeight、offsetHeight、scrollHeight
- TomcatMemcached
- nginx添加model
- 执行计划
- 简要介绍 Hadoop
- 单点更新线段树 RMQ
- 2 WAN 和1 Evo/3g Routeros PCC 方法负载平衡
- android Web Service
- 使用Xcode修改iOS项目工程名和路径名
- JavaFTP - Apache Ftp Server Integration
- Linux Commands for Beginners--Creating Your First Scripting
- 考试系统之选择题评分
- oracle 如何将同一表中的某一列更新到另一列
- 测试neo4j