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

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

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

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

代码如下:

线段树:

  1. #include <bits/stdc++.h>
  2. #define lson l,m,rt<<1,!ok
  3. #define rson m+1,r,rt<<1|1,!ok
  4.  
  5. using namespace std;
  6. const int maxn = (1 << 17) + 5;
  7. int sum[maxn<<2];
  8.  
  9. void pushup(int rt, bool ok){
  10. if(ok) sum[rt] = sum[rt<<1] | sum[rt<<1|1];
  11. else sum[rt] = sum[rt<<1] ^ sum[rt<<1|1];
  12. }
  13.  
  14. void build(int l, int r, int rt, bool ok){
  15. if(l == r){
  16. scanf("%d", &sum[rt]);
  17. return ;
  18. }
  19. int m = (l+r) >> 1;
  20. build(lson);
  21. build(rson);
  22. pushup(rt, ok);
  23. }
  24.  
  25. void update(int p, int b, int l, int r, int rt, bool ok){
  26. if(l == r){
  27. sum[rt] = b;
  28. return ;
  29. }
  30. int m = (l+r) >> 1;
  31. if(p <= m) update(p, b, lson);
  32. else update(p, b, rson);
  33. pushup(rt, ok);
  34. }
  35.  
  36. int main(){
  37. int num, mm;
  38. cin >> num >> mm;
  39. int n = (1 << num);
  40. bool ok = (num & 1);
  41. build(1, n, 1, ok);
  42. while(mm--){
  43. int p, b;
  44. scanf("%d %d", &p, &b);
  45. update(p, b, 1, n, 1, ok);
  46. printf("%d\n", sum[1]);
  47. }
  48. return 0;
  49. }

另一种:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int maxn = (1 << 18) + 5;
  5. int a[maxn];
  6. int num;
  7.  
  8. int main(){
  9. int n, m;
  10. cin >> n >> m;
  11. num = 1 << n;
  12. for(int i = 0; i < num; ++i) scanf("%d", &a[i+num]);//构造一棵树
  13. int cnt = 0;
  14. int t = num;
  15. while(t){//把所有的值算一下
  16. t >>= 1;
  17. for(int i = 0; i < t; ++i)
  18. if(cnt & 1) a[t+i] = a[(t+i)<<1] ^ a[(t+i)<<1|1];
  19. else a[t+i] = a[(t+i)<<1] | a[(t+i)<<1|1];
  20. ++cnt;//控制是或还是异或
  21. }
  22.  
  23. while(m--){
  24. int p, b;
  25. scanf("%d %d", &p, &b);
  26. cnt = 0; p += num-1;
  27. a[p] = b;
  28. while(p){//从下到上更新
  29. p >>= 1;
  30. if(cnt & 1) a[p] = a[p<<1] ^ a[p<<1|1];
  31. else a[p] = a[p<<1] | a[p<<1|1];
  32. ++cnt;
  33. }
  34.  
  35. printf("%d\n", a[1]);
  36. }
  37. return 0;
  38. }

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. Druid.io系列(二):基本概念与架构

    原文链接:  https://blog.csdn.net/njpjsoftdev/article/details/52955788 在介绍Druid架构之前,我们先结合有关OLAP的基本原理来理解Dr ...

  2. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  3. BLE 周边设备断开

    运行中,突然远程设备断开. TBluetoothLEDevice.IsConnected 为false了. 再次搜索,订阅, BluetoothLE1.SubscribeToCharacteristi ...

  4. vs2010下设置release版本调试设置

    设置在Release模式下调试的方法: 1.工程项目上右键 -> 属性 2.c++ -> 常规 -〉调试信息格式    选  程序数据库(/Zi)或(/ZI), 注意:如果是库的话,只能( ...

  5. Quartz.NET文档 入门教程

    概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...

  6. android 积累

    图片资源 图片资源是简单的Drawable资源,目前Android支持的图片格式有:gif.png.jpg等.我们只需要把图片资源放置到\res\drawable目中,那么在编译后的R.java类中就 ...

  7. jq 遍历 each方法

    1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function  ...

  8. spring boot 项目打包到maven仓库供其它模块使用

    在对spring boot项目进行打包发布的时候发现其它spring boot项目服务真正引用使用该spring boot包中的类 需对打包插件做如下修改: <build> <plu ...

  9. 利用maven实现差异化配置

    回顾过去 生产环境,测试环境,开发环境在不同的环境下会有各种各样的配置,比如数据库链接地址,账户名,密码等等.不同环境下都需要配置,但是配置却又不同.以前分享过一篇文章,介绍了我之前A公司的差异化配置 ...

  10. C# unsafe模式内存操作深入探索

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...