↓ Skip to main content
  1. Posts/

codeforces 339 D. Xenia and Bit Operations(线段树)

·592 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给出n和m,初始给出1«n个数,先相邻的两个数进行或操作(a[1]^a[2],a[3]^a[4]…),得到的新数列再相邻的两个数进行异或操作。

最后得到一个数,即为答案。现在给出m个操作,每个操作两个数p,b,表示令a[p]=b,每次变化后输出最终的结果。

思路:线段树。这道题让我学到了,线段树的数组tree[i]存储的信息可能不唯一,可以不同层存储的是不同的信息。

比如这道题中,距离叶子节点距离为奇数的点存储的是或操作的结果,距离叶子节点距离为偶数的点存储的是异或操作的结果。

还需要注意的是,build和update操作都是从顶向下,最后一个操作是异或还是或取决于n的奇偶性,记得判断。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :Mon 05 Sep 2016 12:56:34 AM CST
 4File Name :code/cf/problem/339D.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31const int N=1<<18;
32int tree[N<<2];
33int n,m;
34void PushUp(int rt,int state)
35{
36    if (state==-1)
37	tree[rt] = tree[rt<<1]|tree[rt<<1|1];
38    else tree[rt] = tree[rt<<1]^tree[rt<<1|1];
39}
40void build(int l,int r,int rt,int state)
41{
42    if (l==r)
43    {
44	scanf("%d",&tree[rt]);
45	return ;
46    }
47    int m = (l+r)>>1;
48    build(lson,-state);
49    build(rson,-state);
50    PushUp(rt,state);
51}
52void update( int p,int sc,int l,int r,int rt,int state)
53{
54    if (l==r)
55    {
56	tree[rt] = sc;
57	return;
58    }
59    int m = (l+r)>>1;
60    if (p<=m) update(p,sc,lson,-state);
61    else update(p,sc,rson,-state);
62    PushUp(rt,state);
63}
64int main()
65{
66	#ifndef  ONLINE_JUDGE
67	freopen("code/in.txt","r",stdin);
68  #endif
69	int state;
70	cin>>n>>m;
71	if (n%2==0) state = 1; //n是树的高度,最后的操作是异或还是或和树的高度的奇偶性有关。
72	else state = -1;
73	n = 1<<n;
74	build(1,n,1,state);
75	while (m--)
76	{
77	    int x,y;
78	    scanf("%d%d",&x,&y);
79	    update(x,y,1,n,1,state);
80	    printf("%d\n",tree[1]);
81	}
82  #ifndef ONLINE_JUDGE
83  fclose(stdin);
84  #endif
85    return 0;
86}

Related

hdu 1754 I Hate It (线段树模板题,炒鸡详细注释版)

·1717 words·4 mins
hdu 1754 题目链接 题意:单点更新,区间查询最大值。 思路:线段树。 一开始借鉴了 clj 的 pointer 写法,wjmzbmr’s code 直接 MLE,看来也许只能在 cf 上用。 下面是 MLE 的代码: 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月18日 星期四 18时40分24秒 4File Name :code/hdu/1754.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27using namespace std; 28const double eps = 1E-8; 29const int dx4[4]={1,0,0,-1}; 30const int dy4[4]={0,-1,1,0}; 31const int inf = 0x3f3f3f3f; 32const int N=2E5+7; 33int a[N],n,m; 34int _max( int x,int y) 35{ 36 if (x==-1||y==-1) 37 return x==-1?y:x; 38 return a[x]>a[y]?x:y; 39} 40struct Tree 41{ 42 Tree *pl,*pr; 43 int l,r,mx; 44 void update() 45 { 46 mx = _max(pl->mx,pr->mx); 47 } 48 Tree(int l,int r) : 49 l(l),r(r) 50 { 51 if ( l + 1 == r) 52 { 53 mx = l ; 54 return ; 55 } 56 pl = new Tree(l,(l+r)>>1); 57 pr = new Tree((l+r)>>1,r); 58 update(); 59 } 60 void change(int p,int x) 61 { 62 if (p < l|| p>=r) return; 63 if (l+1==r) 64 { 65 a[l] = x; 66 return ; 67 } 68 pl->change(p,x); 69 pr->change(p,x); 70 update(); 71 } 72 int queryMax(int L,int R) 73 { 74 if (L <= l && r <= R) return mx; 75 if (L>=r || l >=R) 76 return -1; 77 return _max(pl->queryMax(L,R),pr->queryMax(L,R)); 78 } 79}*rt; 80int main() 81{ 82 #ifndef ONLINE_JUDGE 83 freopen("code/in.txt","r",stdin); 84 #endif 85 while (~scanf("%d%d",&n,&m)) 86 { 87 ms(a,0); 88 for ( int i = 0 ; i < n ; i++) scanf("%d",a+i); 89 rt = new Tree(0,n); 90 while (m--) 91 { 92 char opt[3]; 93 int x,y; 94 scanf("%s %d %d",opt,&x,&y); 95 x--; 96 if (opt[0]=='Q') 97 printf("%d\n",a[rt->queryMax(x,y)]); 98 else rt->change(x,y); 99 } 100 } 101 #ifndef ONLINE_JUDGE 102 fclose(stdin); 103 #endif 104 return 0; 105} 关于线段树的理解,见代码注释:

线段树学习笔记

·141 words·1 min
嘛,终于下定决心搞定线段树了。 之前几次都是被lazy标记卡住,这次大概不会了吧2333 放一些学习资料,最后比较zkw线段树和普通线段树的区别。 codeforces上非递归线段树讲解 (其实就是zkw吧) 线段树进阶(各种花式技巧) 找到了一篇非常赞的tutorial(含lazy标记) 链接

hdoj 2795 Billboard

·486 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:一个尺寸为wh的方格。要按顺序放放n个尺寸为1wi的纸条。问每一个纸条回被放在哪里。如果有多个,放在最上面(编号小) 思路:把没横行能放的最大长度看做一个序列建树。由于h比n大很多。。多出来的没用。。直接取较小值就行。