题意:给定 2的 n 次方个数,对这些数两个两个的进行或运算,然后会减少一半的数,然后再进行异或运算,又少了一半,然后再进行或运算,再进行异或,不断重复,到最后只剩下一个数,要输出这个数,然后有 m 个询问,

每个询问有 p 和 b,要求把第 p 个数改成 b,再这样运算,输出结果。

析:这个题是不是很像线段树,不过这个题不是随机询问哪个区间,区间是固定的,这样也就简单了很多,就省下了一个query函数,再就是线段树是从上到下的,所以我们要分好到底是异或还是或,然后就很简单了,

其实这个题也可以这样想,也是先构造一棵树,然后再考虑从下到上进行变,因为要改变值,所以先把最下面的改掉,然后再更新上去,这样次数比线段树少,比它更快一点,其实原理也是一样的。

代码如下:

线段树:

#include <bits/stdc++.h>
#define lson l,m,rt<<1,!ok
#define rson m+1,r,rt<<1|1,!ok using namespace std;
const int maxn = (1 << 17) + 5;
int sum[maxn<<2]; void pushup(int rt, bool ok){
if(ok) sum[rt] = sum[rt<<1] | sum[rt<<1|1];
else sum[rt] = sum[rt<<1] ^ sum[rt<<1|1];
} void build(int l, int r, int rt, bool ok){
if(l == r){
scanf("%d", &sum[rt]);
return ;
}
int m = (l+r) >> 1;
build(lson);
build(rson);
pushup(rt, ok);
} void update(int p, int b, int l, int r, int rt, bool ok){
if(l == r){
sum[rt] = b;
return ;
}
int m = (l+r) >> 1;
if(p <= m) update(p, b, lson);
else update(p, b, rson);
pushup(rt, ok);
} int main(){
int num, mm;
cin >> num >> mm;
int n = (1 << num);
bool ok = (num & 1);
build(1, n, 1, ok);
while(mm--){
int p, b;
scanf("%d %d", &p, &b);
update(p, b, 1, n, 1, ok);
printf("%d\n", sum[1]);
}
return 0;
}

另一种:

#include <bits/stdc++.h>

using namespace std;
const int maxn = (1 << 18) + 5;
int a[maxn];
int num; int main(){
int n, m;
cin >> n >> m;
num = 1 << n;
for(int i = 0; i < num; ++i) scanf("%d", &a[i+num]);//构造一棵树
int cnt = 0;
int t = num;
while(t){//把所有的值算一下
t >>= 1;
for(int i = 0; i < t; ++i)
if(cnt & 1) a[t+i] = a[(t+i)<<1] ^ a[(t+i)<<1|1];
else a[t+i] = a[(t+i)<<1] | a[(t+i)<<1|1];
++cnt;//控制是或还是异或
} while(m--){
int p, b;
scanf("%d %d", &p, &b);
cnt = 0; p += num-1;
a[p] = b;
while(p){//从下到上更新
p >>= 1;
if(cnt & 1) a[p] = a[p<<1] ^ a[p<<1|1];
else a[p] = a[p<<1] | a[p<<1|1];
++cnt;
} printf("%d\n", a[1]);
}
return 0;
}

CodeForces 339D Xenia and Bit Operations (线段树)的更多相关文章

  1. codeforces 339C Xenia and Bit Operations(线段树水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Bit Operations Xenia the beginn ...

  2. [线段树]Codeforces 339D Xenia and Bit Operations

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. [Codeforces 339D] Xenia and Bit Operations

    [题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...

  4. CF 197 DIV2 Xenia and Bit Operations 线段树

    线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...

  5. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  6. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  7. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  8. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  9. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

随机推荐

  1. WebApi 返值的实体值前缀加了个下划线

    发现MODEL类中加了个可以被序列化的标记,把它去除即可. [Serializable]

  2. 在CentOS 7中使用VS Code编译调试C++项目

    1. 安装VSCODE 见VSCode官方链接 https://code.visualstudio.com/docs/setup/linux#_rhel-fedora-and-centos-based ...

  3. SQL、PL/SQL、DDL、DML、TCL介绍

    SQL:结构化查询语言(Structured Query Language) PL/SQL:过程化SQL语言(Procedural Language/SQL) DDL(Data Definition ...

  4. Cloudstack 安装记录

    一.条件要求 1.硬件支持虚拟化,并在BIOS中开启(Inter-VT设为 Enable). 2.Centos 6.5 x86_64 3.环境中的每台主机均为静态IP地址. 4.cloudstack安 ...

  5. nodeJS的了解

    JavaScript不仅仅只能运行在浏览器中.任何有JS运行环境(runtime)的地方就可以运行JavaScript. Node.js对Google V8进行了封装. 有了Node.js,JavaS ...

  6. python实现cifar10数据集的可视化

    在学习tensorflow的mnist和cifar实例的时候,官方文档给出的讲解都是一张张图片,直观清晰,当我们看到程序下载下来的数据的时候,宝宝都惊呆了,都是二进制文件,这些二进制文件还不小,用文本 ...

  7. 版本控制git之一 - 仓库管理

    git 再开始这个话题之前,让我想起了一件很痛苦的事情,在我大学写毕业论文的时候,我当时的文件是这样保存的 毕业论文_初稿.doc 毕业论文_修改1.doc 毕业论文_修改2.doc 毕业论文_修改3 ...

  8. 读《分布式一致性原理》zookeeper运维

    1.配置详解 1.1基本配置 基本参数包括clientPort,dataDir和tickTime 1.2高级配置 下面我们再来看看zookeeper中一些高级配置参数的配置实用 2.四字命令 我们曾经 ...

  9. c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存

    我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...

  10. 【译】PGS字幕

    PGS(Presentation graphic stream):图形字幕流,是用来显示蓝光电影中的字幕的流.当蓝光盘中的PGS格式的字幕被分离存储的时候通常保存在一个以sup为扩展名的文件中.(也可 ...