codeforces 339 D.Xenia and Bit Operations(线段树)
来源:互联网 发布:网络经济人 编辑:程序博客网 时间:2024/05/05 06:24
这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了。
题意:
输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然后输出整个序列运算后的值,而这个运算就比较复杂了, 最下面一层两个数字之间或运算得到原来数目一半的数字,然后两个之间异或运算,得到一半,再或再异或………………,一直到得到一个数字,这个数字就是要求的结果。
思路:
如果只是一种运算,这就是简单的线段树点更新,区间查询。而现在,我们要确定什么时候用or 什么时候用xor, 想想看,最下面一层是用or, 总共有n层,因为or和xor是交替进行的,我们就可以用n确定每层的运算,然后在建树和更新的时候分情况讨论。
代码如下:
#include <stdio.h>#include <string.h>const int maxn = (1<<17) + 5;struct node{ int l, r; int x;}tree[maxn<<2];int arr[maxn];void build(int l, int r, int o, int d){ tree[o].l = l; tree[o].r = r; if (l == r) { tree[o].x = arr[l]; return ; } int mid = (l + r) >> 1; build(l, mid, o<<1, -d); build(mid+1, r, o<<1|1, -d); if (d == 1) tree[o].x = tree[o<<1].x^tree[o<<1|1].x; else tree[o].x = tree[o<<1].x|tree[o<<1|1].x; } int query(int l, int r, int o) { if (tree[o].l == l && tree[o].r == r) return tree[o].x; int mid = (tree[o].l + tree[o].r) >> 1; if (r <= mid) return query(l, r, o<<1); else if (l > mid) return query(l, r, o<<1|1); else return query(l, mid, o<<1)^query(mid+1, r, o<<1|1); } void update(int x, int v, int o, int d) { if (tree[o].l == tree[o].r && tree[o].l == x) { tree[o].x = v; return; } int mid = (tree[o].l + tree[o].r) >> 1; if (x <= mid) update(x, v, o<<1, -d); else update(x, v, o<<1|1, -d); if (d == 1) tree[o].x = tree[o<<1].x^tree[o<<1|1].x; else tree[o].x = tree[o<<1].x|tree[o<<1|1].x; } int main() { int n, m; int p, b, d; while (scanf("%d %d", &n, &m) != EOF) { int num = 1<<n; for (int i = 1; i <= num; i++) scanf("%d", &arr[i]); if (n&1) d = -1; else d = 1; build(1, num, 1, d); while (m--) { scanf("%d %d", &p, &b); update(p, b, 1, d); printf("%d\n", query(1, num, 1)); } } return 0; }
- codeforces 339 D.Xenia and Bit Operations(线段树)
- CodeForces 339D Xenia and Bit Operations(线段树)
- codeforces 339D Xenia and Bit Operations 线段树
- CodeForces 339D Xenia and Bit Operations 线段树
- Codeforces 339D Xenia and Bit Operations 线段树
- CodeForces 339D Xenia and Bit Operations (线段树水题)
- CodeForces 339D Xenia and Bit Operations(线段树点修改)
- CodeForces 339D Xenia and Bit Operations
- CodeForces 339D Xenia and Bit Operations
- CodeForces 339D D. Xenia and Bit Operations(线段树的单点更新)
- CF 339D - Xenia and Bit Operations(线段树)
- Codeforces 339D Xenia and Bit Operations 线段树单点修改
- CodeForces 339D Xenia and Bit Operations 数据结构+线段树+点更新
- Codeforces Round #197 (Div. 2) -- D. Xenia and Bit Operations(线段树)
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations(线段树)
- 文章标题 coderforces 339D : Xenia and Bit Operations (线段树+点修改)
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations - 线段树
- [题解]codeforces 339d Xenia and Bit Operations
- 一个比较方便的转换NSString为UTF8编码的函数
- ExtJS4+strtus2文件上传实例
- 【*老牌系统*】教你如何让双硬盘自主选择关闭
- ACM之java速成
- oracle 多行数据结合成一行 sql编写
- codeforces 339 D.Xenia and Bit Operations(线段树)
- win7 自建wifi热点供安卓手机/ipad 使用
- Eclipse中Tomcat配置
- iOS经纬度偏移解决
- linq单个结果的处理
- virt-install 安装过程
- IOS系统下实现全屏
- android 文件操作
- 数组的概念及操作方法-----黑马程序员