题意:给定 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. php中将数组转换为指定符号分割的字符串

    如想将一个数组转换为以“,”分割的字符串,只需如下: implode(',', arr);

  2. Echarts运用

    echarts客户端写法:http://echarts.baidu.com/doc/example.html  ,下载echarts-2.0.4.jar包,把src里面的js引入到项目里,在放esl. ...

  3. 使用wifi网卡笔记2----概念及工具iw(STA模式)

    1.认证和加密的概念 (1)概念 (2)阶段划分 初级版本:认证不需要密码, 传输不需要加密 认证不需要密码, 传输需要加密(用WEP算法) 认证需要密码(用WEP算法), 传输需要加密(用WEP算法 ...

  4. JS实战应用之做LOL领图标任务~

    说一个技术造福人类的故事,事情是这样的,我是英雄联盟的忠实玩家,在浏览官网的时候看到这样一个活动(http://lol.qq.com/act/a20161020teemo/index.html),有个 ...

  5. BGP中IBGP和EBGP的区别和联系

    我们知道,在自治系统内部使用IGP路由协议:而在不同自治系统之间使用BGP路由协议(严格来讲,BGP不是路由协议).BGP产生的原因是为了在不同自治系统(AS)之间进行路由转发,而其中又提出了EBGP ...

  6. Rhythmk 一步一步学 JAVA (12) Spring-1 之入门

    (一)简单对象Spring  XML配置说明 使用Spring (Spring 3.0) 实现最简单的类映射以及引用,属性赋值: 1.1.新建类UserModel: package com.sprin ...

  7. KindEditor 和 xss过滤

    KindEditor   1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files/wup ...

  8. dart 命名规范

    1.类型 首字母大写 譬如 abstract class Shape 2.变量 驼峰式命名,首字母小写 class Article { String headUrl; String user; Str ...

  9. [iOS]UIScrollView嵌套内容在左右拨动的时候自动被顶上问题

    遇到的问题是这样的: 适配6+没问题,但是5s就出问题.我UIScrollView嵌套了左侧UIScrollView,右侧UITableView,左右拨动切换,结果5s下拨动之后两边的View都会自动 ...

  10. Mysql合并两列数据

    实例: UPDATE x_yiyuanpinggu_nengli SET ch_yuzhongfangxiang = CONCAT(ch_yuanyuzhong,ch_mubiaoyuzhong) M ...