[线段树]Codeforces 339D Xenia and Bit Operations
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 m queries. 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
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
题意:
给出2的n次方个数,每次将现在这个序列中相邻的两个数运算后合并为一个数,得到一个新的序列,这个新序列的长度是上一个序列长度-1,当新序列长度为1时停止运算,奇数次操作进行OR运算,偶数次操作进行XOR运算,
现在有m个询问,每次询问会改变上一个序列中的一个值,问新序列运算后的值为多少
思路:
树上的每一层是一个新序列,从叶子节点那一层记为0,向上更新层数,每层的层数是它下面那层的层数+1
记录了层数之后就知道每一层要进行什么计算了,奇数层OR,偶数层XOR,
最后对每次询问单点修改,向上更新后输出根节点的值就可以了
#include<bits/stdc++.h>
using namespace std;
const int amn=<<+;
typedef long long ll;
int a[amn];
struct tree{
ll l,r,sum,deep;
#define ls rt<<1
#define rs rt<<1|1
#define trl tr[rt].l
#define trr tr[rt].r
#define trsum tr[rt].sum
#define trlssum tr[ls].sum
#define trrssum tr[rs].sum
#define trdp tr[rt].deep
}tr[amn];
void push_up(int rt){
trdp=tr[ls].deep+; ///记录计算层数,叶子节点设为0,父节点的层数为儿子节点层数+1
if(trdp&) ///奇数层数OR运算,偶数层数XOR运算
trsum=trlssum|trrssum;
else
trsum=trlssum^trrssum;
}
void build(int rt,int l,int r){
trl=l,trr=r;
if(trl==trr){
trsum=a[l];
trdp=; ///叶子节点计算层数
return ;
}
int mid=(l+r)>>;
build(ls,l,mid);
build(rs,mid+,r);
push_up(rt);
}
void updata(int rt,int l,int r,int pos,ll val){
int mid=(l+r)>>;
if(trl==trr&&trl==pos){
trsum=val;
return;
}
if(pos<=mid)updata(ls,l,mid,pos,val);
else updata(rs,mid+,r,pos,val);
push_up(rt);
}
int main(){
int n,m,p,b;
ios::sync_with_stdio();
cin>>n>>m;
int len=<<n; ///注意这里长度为2的n次方
for(int i=;i<=len;i++)
cin>>a[i];
build(,,len);
while(m--){
cin>>p>>b;
updata(,,len,p,b);
printf("%d\n",tr[].sum);
}
}
/***
给出2的n次方个数,每次将现在这个序列中相邻的两个数运算后合并为一个数,得到一个新的序列,这个新序列的长度是上一个序列长度-1,当新序列长度为1时停止运算,奇数次操作进行OR运算,偶数次操作进行XOR运算,
现在有m个询问,每次询问会改变上一个序列中的一个值,问新序列运算后的值为多少
在纸上模拟可以看出这是一个完全二叉树的结构,单点修改,向上更新数值,可以想到用线段树来维护
树上的每一层是一个新序列,从叶子节点那一层记为0,向上更新层数,每层的层数是它下面那层的层数+1
记录了层数之后就知道每一层要进行什么计算了,奇数层OR,偶数层XOR,
最后对每次询问单点修改,向上更新后输出根节点的值就可以了
***/
[线段树]Codeforces 339D Xenia and Bit Operations的更多相关文章
- CodeForces 339D Xenia and Bit Operations (线段树)
题意:给定 2的 n 次方个数,对这些数两个两个的进行或运算,然后会减少一半的数,然后再进行异或运算,又少了一半,然后再进行或运算,再进行异或,不断重复,到最后只剩下一个数,要输出这个数,然后有 m ...
- [Codeforces 339D] Xenia and Bit Operations
[题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- Xenia and Bit Operations CodeForces - 339D
Xenia and Bit Operations CodeForces - 339D Xenia the beginner programmer has a sequence a, consistin ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- codeforces 339 D.Xenia and Bit Operations(线段树)
这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了. 题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然 ...
- Xenia and Bit Operations(线段树单点更新)
Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- Particle Filter Algorithm
目录 问题提出 算法研究现状 算法原理 问题提出 在现实科研问题中,其中有很多都是非线性的.要想求得问题的解,就需要非线性的算法.所谓非线性滤波,就是基于带有噪声的观测值,估计非线性系统动态变化的状态 ...
- date成字符串
//获取当前时间 Date date=new Date(); System.out.println("当前date: "+date); //将时间转化成yyyy-MM-dd格式的字 ...
- 压力测试(八)-多节点JMeter分布式压测实战
1.Jmeter4.0分布式压测准备工作 简介:讲解Linux服务器上jmeter进行分布式压测的相关准备工作 1.压测注意事项 the firewalls on the systems are tu ...
- 《一步步成为 Hacker_Day 01》
环境搭建 传统运行模式 - 一台机器同时只能运行一个操作系统 |:----------|----------:| | 应用程序 | 应用程序 | |:----------|----------:| | ...
- 复盘MySQL分页查询优化方案
一.前言 MySQL分页查询作为Java面试的一道高频面试题,这里有必要实践一下,毕竟实践出真知. 很多同学在做测试时苦于没有海量数据,官方其实是有一套测试库的. 二.模拟数据 这里模拟数据分2种情况 ...
- 前端劝退预警:JavaScript 工具链不完全指南
经过这么多年的发展,JavaScript 早已经不是当年那个不太起眼的脚本语言.如今的 JavaScript 可以说是风光无限,在 Web 前端.移动端.服务端甚至物联网设备上都大展身手,到处都有它的 ...
- Python线性优化基础讲解~
目前,各组织正在利用数据科学和机器学习来解决各种业务问题.为了创造一个真正的业务影响,如何弥合数据科学管道和业务决策管道之间的差距显得尤为重要. 数据科学管道的结果往往是数据中的预测.模式和洞察(通常 ...
- 通过mockjs来制作假数据
需用用到的模块为express和mockjs //导入模块开启服务器模块 const express=require('express') //导入假数据模块 const mockjs=require ...
- C++ 选择排序的理解
#include<stdio.h> #include <iostream> using namespace std; void swap(int *a, int *b) //元 ...
- burpsuit之Spider、Scanner、Intruder模块
1.spider模块 1.spider模块介绍 被动爬网:(被动爬网获得的链接是手动爬网的时候返回页面的信息中分析发现超链接) 对于爬网的时候遇到HTML表单如何操作: 需要表单身份认证时如何操作(默 ...