单点更新线段树 RMQ
2 seconds
256 megabytes
standard input
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 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
线段树:
#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的更多相关文章
- 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 ...
- 《白书》上线段树RMQ的实现
白书上的线段树RMQ实现,自己重写了一遍: #include <bits/stdc++.h> using namespace std; const int MAXN=1<<17 ...
- 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 ...
- Hlg 1832 【线段树 && RMQ】.cpp
题意: 在给出的区间内求出最大买进卖出的差价. 思路: 对于弱数据:维护一个从左到右的最大差价和最小值.即当发现当前值比最小值小的时候更新最小值,否则看一下当前值与之前最小值的差价是否比最大差价大,是 ...
- 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 ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- PKU 2823 Sliding Window(线段树||RMQ||单调队列)
题目大意:原题链接(定长区间求最值) 给定长为n的数组,求出每k个数之间的最小/大值. 解法一:线段树 segtree节点存储区间的最小/大值 Query_min(int p,int l,int r, ...
随机推荐
- 【Demo 0007】Android 信使(Intent)
本章学习要点: 1. 了解Intent功能作用: 2. 掌握Intent在显示和隐示基本使用方法及规则:
- commondatastorage.googleapis.com訪问失败高速解决
谷歌更新以后非常多sampleproject下载不了. http://commondatastorage.googleapis.com訪问失败高速解决这个问题. 使用在线代理就可以,随便推荐一个htt ...
- Android API中被忽略的几个函数接口
1. MotionEvent的几个函数 下面的方法都支持多点触摸,即可以对单个触摸点调用下面的方法 1.1 getPressure() 这个api 可以获取到手指触摸屏幕时候的压力,但是需要硬件和驱动 ...
- MP3文件的结构与编程
有一个朋友喜欢听MP3,为了获取MP3,写了一个程序,专门从一家音乐网站上搜索下载mp3,一下子下载了有上千首.这时朋友又犯愁了,这些MP3的歌曲名字都是使用1,2,3,4,..等数字命名,挑选起来十 ...
- regsvr32 命令小集注册OCX控件,注册控件(包括十几个举例)
Regsvr32 进程文件: regsvr32 or regsvr32.exe 进程名称: Microsoft DLL Registration Service 英文描述: regsvr32.ex ...
- ORACLE 更改username
曾经一直常常改动oracle的用户password,但非常少改动username的. 曾经仅仅能创建一个用户1.然后将用户2数据导入到用户1.然后经用户1删掉,这样很麻烦并且耗时,今天就整理了下怎样改 ...
- Swift - 各种手势检测大全(UIGestureRecognizer及其子类)
UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1,UISwipeGestureRecognizer:滑动(快速移动) 1 2 3 4 5 6 7 8 ...
- codeforces 604B More Cowbell
题目链接:http://codeforces.com/contest/604/problem/B 题意:n个数字,k个盒子,把n个数放入k个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少 ...
- 最想做的三个Delphi项目:Paint,IM,SQL,另外还有Smart,TMS,FMX,UML,FreePascal,Python4Delphi,Cheat Engine
都是绝美项目- 如果有时间,要做的项目:0. 整整5个Cloud项目(可带来商业收益,其中还包括手机发送, S/D/N/L/NetDriver)1. Heidi/front/SQLITE STUDIO ...
- 24位和8位BMP图片保存纯C代码
BMP图片大家都知道,可以通过查看BMP图片结构使用纯C就可以打开,编辑,处理,保存图片.非常方便使用. 具体BMP结构可以参考:wingdi.h头文件.今天主要在进行删减代码,需要把多余的代码删除, ...