hdu 5367 digger(动态线段树,区间合并)
题意:
地主小花有n座山,这些山在地主家门前排成一条直线。这些山一开始均有相同的高度。 每一天,小花都会要求ZJiaQ开挖机把几座山挖掉一定高度,或者给一些山堆上一些高度。并且要求报告ZJiaQ报告现在有多少座山属于“高山脉”
当一排山的高度相等,并且比这排山左边和右边的山要高时,这排山被称为高山脉。
当然,最左边和最右边的山不可能是“高山脉”的一部分
思路:线段树,要维护的域蛮多的。
下面高山脉简称"HM"
sum:区间中HM的总长度。
lsum,rsum,区间中包含左端点,右端点的高度相同的山的长度。
lh,rh:区间中包含左端点,包含右端点的的高度相同的山的高度。
llh,rrh:从左端点向右,从有段点向左的,第一个高度不相同的山的高度。
由于这道题n有1E9,没办法像以前的办法build 线段树,因此我们采用动态线段树的技巧。
官方题解:
对于求“高山脉”长度,可以利用线段树。树节点中保存左高度连续长度,左高度连续区间的高度,左高度连续区间的状态(即是否高于第一个高度不同的山),右高度连续长度,右高度连续区间的高度,右高度连续区间的状态,每段区间内的“高山脉”数量。每次更新时更新高度即可,在pushup过程中去计算新产生的“高山脉”。写起来难度不是很大,然后对于n很大且必须在线做这个条件只需对于**线段树动态建立节点**去维护即可
关于动态线段树:
平时我们做的线段树,假设区间为[1,n],那我们通常都是直接以 1 号点表示区间【1,n】,以 i2 号点表示 i 节点代表区间的左半区间,以 i2+1 号点表示 i 节点多代表区间的右半区间,一半空间长度都定义为4*n。
在本题中,n比较大,直接将所有线段树中的所有节点定义出来不现实,那我们注意到,这道题实际上是对区间进行的一系列操作,那么就可能存在一个区间【l,r】,在所有处理过程中,该区间都可以被当做一个整体来看待。 如果是这样的话,我们定义出与该区间的子区间相对应的节点就没有意义了。
动态生成线段树就是:假设对某一节点 p (代表区间【l,r】)进行处理时,并不是对整个区间[l,r]进行处理,而是对其某个子区间进行处理,那这个时候,该区间就不能一直被当做一个整体来看待,所以生成两个初始子节点lp、rp(节点之均为初始值),表示该区间的左半部分与右半部分,然后父节点 p 在对两个新生成的子节点进行更新。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年11月27日 星期日 14时45分27秒
4File Name :code/hdu/5367.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 ms(a,x) memset(a,x,sizeof(a))
21typedef long long LL;
22#define pi pair < int ,int >
23#define MP make_pair
24using namespace std;
25const double eps = 1E-8;
26const int dx4[4]={1,0,0,-1};
27const int dy4[4]={0,-1,1,0};
28const int INF = 0x3f3f3f3f;
29const int N=5E4+7;
30int cur,root;
31int n,q,nnn;
32struct Tree
33{
34 int sum;//高山脉的长度和
35 int child[2];//左右孩子的编号
36 int rsum,lsum;//从左端点,右端点其高度相同的连续的山有多少个。
37 int lh,rh;//左边连续,右边连续的山的高度。
38 int llh,rrh;//从左边往右第一个不连续,从右边往左第一个不连续的山的高度。
39 int lazy;
40 void init(int l,int r)
41 {
42 sum = child[0] = child[1] = lh = rh = llh = rrh = lazy = 0;
43 lsum = rsum = r-l+1; //初始所有高度相同,所以高度相同的连续的山的个数等于区间长度。
44 }
45};
46struct SegmentTree{
47 Tree tree[N*60];
48 void check(int &rt,int l,int r)
49 {
50 //cout<<"rt:"<<rt<<" l:"<<l<<" r:"<<r<<endl;
51 if (rt) return;
52 rt=++cur;
53 tree[rt].init(l,r);
54 if (l==1) tree[rt].llh = INF;
55 if (r==n) tree[rt].rrh = INF;//最左边和最右边的山一定不是“high mountain line"的一部分/
56 }
57 void add( int rt,int val)
58 {
59 tree[rt].lazy +=val;
60 tree[rt].rrh +=val;
61 tree[rt].llh +=val;
62 tree[rt].lh +=val;
63 tree[rt].rh +=val;
64 }
65 void PushDown( int rt,int l,int r)
66 {
67 if (tree[rt].lazy)
68 {
69 int m = (l+r)>>1;
70 check(tree[rt].child[0],l,m);
71 check(tree[rt].child[1],m+1,r);
72 add(tree[rt].child[0],tree[rt].lazy);
73 add(tree[rt].child[1],tree[rt].lazy);
74 tree[rt].lazy = 0;
75 }
76 }
77 void PushUp( int rt,int l,int r)
78 {
79 int m = (l+r)>>1;
80 check(tree[rt].child[0],l,m);
81 check(tree[rt].child[1],m+1,r);
82 int lson = tree[rt].child[0];
83 int rson = tree[rt].child[1];
84 tree[rt].sum = tree[lson].sum + tree[rson].sum;
85 tree[rt].lsum = tree[lson].lsum;
86 tree[rt].rsum = tree[rson].rsum;
87 tree[rt].lh = tree[lson].lh;
88 tree[rt].rh = tree[rson].rh;
89 tree[rt].llh = tree[lson].llh;
90 tree[rt].rrh = tree[rson].rrh;
91 //先继承先前小区间的,肯定不会比该答案小
92 if (tree[lson].rh==tree[rson].lh)
93 {
94 if (tree[lson].rh>tree[lson].rrh&&tree[rson].lh>tree[rson].llh)
95 tree[rt].sum += tree[lson].rsum + tree[rson].lsum;
96 if (tree[lson].rsum==m-l+1) //整段区间都是high mountain line,可能延续到其他区间。
97 {
98 tree[rt].lsum += tree[rson].lsum;
99 tree[rt].llh = tree[rson].llh;
100 }
101 if (tree[rson].lsum == r-m) //同上
102 {
103 tree[rt].rsum += tree[lson].rsum;
104 tree[rt].rrh = tree[lson].rrh;
105 }
106 }else
107 {
108 if (tree[lson].lsum == m-l+1) tree[rt].llh = tree[rson].lh;//如果整个区间山高度相同(但是和右边区间高度不同没办法合并)从左往右第一个不相同的山的高度就是右区间包含左端点的连续的山的高度。
109 if (tree[lson].rh>tree[rson].lh&&tree[lson].rh>tree[lson].rrh)
110 tree[rt].sum +=tree[lson].rsum;//如果左区间包含右端点的一段的高度大于右区间包含左端点的山的高度并且大于左区间从右端点往左第一个高度不相同的山的高度,那么这段是一段high M line.累加到答案。
111 if (tree[rson].rsum== r-m) tree[rt].rrh = tree[lson].rh;//同上
112 if (tree[rson].lh>tree[lson].rh&&tree[rson].lh>tree[rson].llh) //同上。
113 tree[rt].sum += tree[rson].lsum;
114 }
115 }
116 void update(int &rt,int l,int r,int L,int R,int x)
117 {
118 // printf("rt %d l %d r %d\n",rt,l,r);
119 check(rt,l,r);
120 if (L<=l&&r<=R)
121 {
122 add(rt,x);
123 return;
124 }
125 PushDown(rt,l,r);
126 int m = (l+r)>>1;
127 if (L<=m) update(tree[rt].child[0],l,m,L,R,x);
128 if (R>=m+1) update(tree[rt].child[1],m+1,r,L,R,x);
129 PushUp(rt,l,r);
130 }
131}SegTree;
132int main()
133{
134#ifndef ONLINE_JUDGE
135 freopen("code/in.txt","r",stdin);
136#endif
137 while (~scanf("%d%d%*d",&n,&q))
138 {
139 cur = root = 0 ;
140 int ans = 0 ;
141 int l,r,val;
142 for ( int i = 1; i <= q ; i++)
143 {
144 scanf("%d%d%d",&l,&r,&val);
145 l ^= ans;
146 r ^= ans;
147 val ^= ans;
148 if (l>r) swap(l,r);
149 SegTree.update(root,1,n,l,r,val);
150 ans = SegTree.tree[1].sum;
151 printf("%d\n",ans);
152 }
153 }
154#ifndef ONLINE_JUDGE
155 fclose(stdin);
156#endif
157 return 0;
158}