标签:style blog http io ar color os sp for
http://www.notonlysuccess.com/(今天看二叉树,想回来看看,发现大牛博客进不去。。。)
如果要学,就要好好学。我copy的,如有错,请看http://www.cnblogs.com/Mu-Tou/archive/2011/08/11/2134427.html
【完全版】线段树
很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉得当时的代码风格实在是太丑了,很多线段树的初学者可能就是看着这篇文章来练习的,如果不小心被我培养出了这么糟糕的风格,实在是过意不去,正好过几天又要给集训队讲解线段树,所以决定把这些题目重新写一遍,顺便把近年我接触到的一些新题更新上去~;并且学习了splay等更高级的数据结构后对线段树的体会有更深了一层,线段树的写法也就比以前飘逸,简洁且方便多了.
在代码前先介绍一些我的线段树风格:
maxn是题目给的最大区间,而节点数要开4倍,确切的来说节点数要开大于maxn的最小2x的两倍
lson和rson分辨表示结点的左儿子和右儿子,由于每次传参数的时候都固定是这几个变量,所以可以用预定于比较方便的表示
以前的写法是另外开两个个数组记录每个结点所表示的区间,其实这个区间不必保存,一边算一边传下去就行,只需要写函数的时候多两个参数,结合lson和rson的预定义可以很方便
PushUP(int rt)是把当前结点的信息更新到父结点
PushDown(int rt)是把当前结点的信息更新给儿子结点
rt表示当前子树的根(root),也就是当前所在的结点
整理这些题目后我觉得线段树的题目整体上可以分成以下四个部分:
单点更新:最最基础的线段树,只更新叶子节点,然后把信息用PushUP(int r)这个函数更新上来
hdu1166 敌兵布阵
题意:O(-1)
思路:O(-1)
线段树功能:update:单点增减 query:区间求和
1 #include <cstdio> 2 3 #define lson l , m , rt << 1 4 #define rson m + 1 , r , rt << 1 | 1 5 const int maxn = 55555; 6 int sum[maxn<<2]; 7 void PushUP(int rt) { 8 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 9 } 10 void build(int l,int r,int rt) { 11 if (l == r) { 12 scanf("%d",&sum[rt]); 13 return ; 14 } 15 int m = (l + r) >> 1; 16 build(lson); 17 build(rson); 18 PushUP(rt); 19 } 20 void update(int p,int add,int l,int r,int rt) { 21 if (l == r) { 22 sum[rt] += add; 23 return ; 24 } 25 int m = (l + r) >> 1; 26 if (p <= m) update(p , add , lson); 27 else update(p , add , rson); 28 PushUP(rt); 29 } 30 int query(int L,int R,int l,int r,int rt) { 31 if (L <= l && r <= R) { 32 return sum[rt]; 33 } 34 int m = (l + r) >> 1; 35 int ret = 0; 36 if (L <= m) ret += query(L , R , lson); 37 if (R > m) ret += query(L , R , rson); 38 return ret; 39 } 40 int main() { 41 int T , n; 42 scanf("%d",&T); 43 for (int cas = 1 ; cas <= T ; cas ++) { 44 printf("Case %d:\n",cas); 45 scanf("%d",&n); 46 build(1 , n , 1); 47 char op[10]; 48 while (scanf("%s",op)) { 49 if (op[0] == ‘E‘) break; 50 int a , b; 51 scanf("%d%d",&a,&b); 52 if (op[0] == ‘Q‘) printf("%d\n",query(a , b , 1 , n , 1)); 53 else if (op[0] == ‘S‘) update(a , -b , 1 , n , 1); 54 else update(a , b , 1 , n , 1); 55 } 56 } 57 return 0; 58 }
hdu1754 I Hate It
题意:O(-1)
思路:O(-1)
线段树功能:update:单点替换 query:区间最值
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 const int maxn = 222222; 8 int MAX[maxn<<2]; 9 void PushUP(int rt) { 10 MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]); 11 } 12 void build(int l,int r,int rt) { 13 if (l == r) { 14 scanf("%d",&MAX[rt]); 15 return ; 16 } 17 int m = (l + r) >> 1; 18 build(lson); 19 build(rson); 20 PushUP(rt); 21 } 22 void update(int p,int sc,int l,int r,int rt) { 23 if (l == r) { 24 MAX[rt] = sc; 25 return ; 26 } 27 int m = (l + r) >> 1; 28 if (p <= m) update(p , sc , lson); 29 else update(p , sc , rson); 30 PushUP(rt); 31 } 32 int query(int L,int R,int l,int r,int rt) { 33 if (L <= l && r <= R) { 34 return MAX[rt]; 35 } 36 int m = (l + r) >> 1; 37 int ret = 0; 38 if (L <= m) ret = max(ret , query(L , R , lson)); 39 if (R > m) ret = max(ret , query(L , R , rson)); 40 return ret; 41 } 42 int main() { 43 int n , m; 44 while (~scanf("%d%d",&n,&m)) { 45 build(1 , n , 1); 46 while (m --) { 47 char op[2]; 48 int a , b; 49 scanf("%s%d%d",op,&a,&b); 50 if (op[0] == ‘Q‘) printf("%d\n",query(a , b , 1 , n , 1)); 51 else update(a , b , 1 , n , 1); 52 } 53 } 54 return 0; 55 }
hdu1394 Minimum Inversion Number
题意:求Inversion后的最小逆序数
思路:用O(nlogn)复杂度求出最初逆序数后,就可以用O(1)的复杂度分别递推出其他解
线段树功能:update:单点增减 query:区间求和
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 const int maxn = 5555; 8 int sum[maxn<<2]; 9 void PushUP(int rt) { 10 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 11 } 12 void build(int l,int r,int rt) { 13 sum[rt] = 0; 14 if (l == r) return ; 15 int m = (l + r) >> 1; 16 build(lson); 17 build(rson); 18 } 19 void update(int p,int l,int r,int rt) { 20 if (l == r) { 21 sum[rt] ++; 22 return ; 23 } 24 int m = (l + r) >> 1; 25 if (p <= m) update(p , lson); 26 else update(p , rson); 27 PushUP(rt); 28 } 29 int query(int L,int R,int l,int r,int rt) { 30 if (L <= l && r <= R) { 31 return sum[rt]; 32 } 33 int m = (l + r) >> 1; 34 int ret = 0; 35 if (L <= m) ret += query(L , R , lson); 36 if (R > m) ret += query(L , R , rson); 37 return ret; 38 } 39 int x[maxn]; 40 int main() { 41 int n; 42 while (~scanf("%d",&n)) { 43 build(0 , n - 1 , 1); 44 int sum = 0; 45 for (int i = 0 ; i < n ; i ++) { 46 scanf("%d",&x[i]); 47 sum += query(x[i] , n - 1 , 0 , n - 1 , 1); 48 update(x[i] , 0 , n - 1 , 1); 49 } 50 int ret = sum; 51 for (int i = 0 ; i < n ; i ++) { 52 sum += n - x[i] - x[i] - 1; 53 ret = min(ret , sum); 54 } 55 printf("%d\n",ret); 56 } 57 return 0; 58 }
hdu2795 Billboard
题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 const int maxn = 222222; 8 int h , w , n; 9 int MAX[maxn<<2]; 10 void PushUP(int rt) { 11 MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]); 12 } 13 void build(int l,int r,int rt) { 14 MAX[rt] = w; 15 if (l == r) return ; 16 int m = (l + r) >> 1; 17 build(lson); 18 build(rson); 19 } 20 int query(int x,int l,int r,int rt) { 21 if (l == r) { 22 MAX[rt] -= x; 23 return l; 24 } 25 int m = (l + r) >> 1; 26 int ret = (MAX[rt<<1] >= x) ? query(x , lson) : query(x , rson); 27 PushUP(rt); 28 return ret; 29 } 30 int main() { 31 while (~scanf("%d%d%d",&h,&w,&n)) { 32 if (h > n) h = n; 33 build(1 , h , 1); 34 while (n --) { 35 int x; 36 scanf("%d",&x); 37 if (MAX[1] < x) puts("-1"); 38 else printf("%d\n",query(x , 1 , h , 1)); 39 } 40 } 41 return 0; 42 }
练习:
poj2828 Buy Tickets
poj2886 Who Gets the Most Candies?
成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候
hdu1698 Just a Hook
题意:O(-1)
思路:O(-1)
线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息)
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 const int maxn = 111111; 8 int h , w , n; 9 int col[maxn<<2]; 10 int sum[maxn<<2]; 11 void PushUp(int rt) { 12 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 13 } 14 void PushDown(int rt,int m) { 15 if (col[rt]) { 16 col[rt<<1] = col[rt<<1|1] = col[rt]; 17 sum[rt<<1] = (m - (m >> 1)) * col[rt]; 18 sum[rt<<1|1] = (m >> 1) * col[rt]; 19 col[rt] = 0; 20 } 21 } 22 void build(int l,int r,int rt) { 23 col[rt] = 0; 24 sum[rt] = 1; 25 if (l == r) return ; 26 int m = (l + r) >> 1; 27 build(lson); 28 build(rson); 29 PushUp(rt); 30 } 31 void update(int L,int R,int c,int l,int r,int rt) { 32 if (L <= l && r <= R) { 33 col[rt] = c; 34 sum[rt] = c * (r - l + 1); 35 return ; 36 } 37 PushDown(rt , r - l + 1); 38 int m = (l + r) >> 1; 39 if (L <= m) update(L , R , c , lson); 40 if (R > m) update(L , R , c , rson); 41 PushUp(rt); 42 } 43 int main() { 44 int T , n , m; 45 scanf("%d",&T); 46 for (int cas = 1 ; cas <= T ; cas ++) { 47 scanf("%d%d",&n,&m); 48 build(1 , n , 1); 49 while (m --) { 50 int a , b , c; 51 scanf("%d%d%d",&a,&b,&c); 52 update(a , b , c , 1 , n , 1); 53 } 54 printf("Case %d: The total value of the hook is %d.\n",cas , sum[1]); 55 } 56 return 0; 57 } 58
poj3468 A Simple Problem with Integers
题意:O(-1)
思路:O(-1)
线段树功能:update:成段增减 query:区间求和
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 #define LL long long 8 const int maxn = 111111; 9 LL add[maxn<<2]; 10 LL sum[maxn<<2]; 11 void PushUp(int rt) { 12 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 13 } 14 void PushDown(int rt,int m) { 15 if (add[rt]) { 16 add[rt<<1] += add[rt]; 17 add[rt<<1|1] += add[rt]; 18 sum[rt<<1] += add[rt] * (m - (m >> 1)); 19 sum[rt<<1|1] += add[rt] * (m >> 1); 20 add[rt] = 0; 21 } 22 } 23 void build(int l,int r,int rt) { 24 add[rt] = 0; 25 if (l == r) { 26 scanf("%lld",&sum[rt]); 27 return ; 28 } 29 int m = (l + r) >> 1; 30 build(lson); 31 build(rson); 32 PushUp(rt); 33 } 34 void update(int L,int R,int c,int l,int r,int rt) { 35 if (L <= l && r <= R) { 36 add[rt] += c; 37 sum[rt] += (LL)c * (r - l + 1); 38 return ; 39 } 40 PushDown(rt , r - l + 1); 41 int m = (l + r) >> 1; 42 if (L <= m) update(L , R , c , lson); 43 if (m < R) update(L , R , c , rson); 44 PushUp(rt); 45 } 46 LL query(int L,int R,int l,int r,int rt) { 47 if (L <= l && r <= R) { 48 return sum[rt]; 49 } 50 PushDown(rt , r - l + 1); 51 int m = (l + r) >> 1; 52 LL ret = 0; 53 if (L <= m) ret += query(L , R , lson); 54 if (m < R) ret += query(L , R , rson); 55 return ret; 56 } 57 int main() { 58 int N , Q; 59 scanf("%d%d",&N,&Q); 60 build(1 , N , 1); 61 while (Q --) { 62 char op[2]; 63 int a , b , c; 64 scanf("%s",op); 65 if (op[0] == ‘Q‘) { 66 scanf("%d%d",&a,&b); 67 printf("%lld\n",query(a , b , 1 , N , 1)); 68 } else { 69 scanf("%d%d%d",&a,&b,&c); 70 update(a , b , c , 1 , N , 1); 71 } 72 } 73 return 0; 74 } 75
poj2528 Mayor’s posters
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 8 const int maxn = 11111; 9 bool hash[maxn]; 10 int li[maxn] , ri[maxn]; 11 int X[maxn*3]; 12 int col[maxn<<4]; 13 int cnt; 14 15 void PushDown(int rt) { 16 if (col[rt] != -1) { 17 col[rt<<1] = col[rt<<1|1] = col[rt]; 18 col[rt] = -1; 19 } 20 } 21 void update(int L,int R,int c,int l,int r,int rt) { 22 if (L <= l && r <= R) { 23 col[rt] = c; 24 return ; 25 } 26 PushDown(rt); 27 int m = (l + r) >> 1; 28 if (L <= m) update(L , R , c , lson); 29 if (m < R) update(L , R , c , rson); 30 } 31 void query(int l,int r,int rt) { 32 if (col[rt] != -1) { 33 if (!hash[col[rt]]) cnt ++; 34 hash[ col[rt] ] = true; 35 return ; 36 } 37 if (l == r) return ; 38 int m = (l + r) >> 1; 39 query(lson); 40 query(rson); 41 } 42 int Bin(int key,int n,int X[]) { 43 int l = 0 , r = n - 1; 44 while (l <= r) { 45 int m = (l + r) >> 1; 46 if (X[m] == key) return m; 47 if (X[m] < key) l = m + 1; 48 else r = m - 1; 49 } 50 return -1; 51 } 52 int main() { 53 int T , n; 54 scanf("%d",&T); 55 while (T --) { 56 scanf("%d",&n); 57 int nn = 0; 58 for (int i = 0 ; i < n ; i ++) { 59 scanf("%d%d",&li[i] , &ri[i]); 60 X[nn++] = li[i]; 61 X[nn++] = ri[i]; 62 } 63 sort(X , X + nn); 64 int m = 1; 65 for (int i = 1 ; i < nn; i ++) { 66 if (X[i] != X[i-1]) X[m ++] = X[i]; 67 } 68 for (int i = m - 1 ; i > 0 ; i --) { 69 if (X[i] != X[i-1] + 1) X[m ++] = X[i] + 1; 70 } 71 sort(X , X + m); 72 memset(col , -1 , sizeof(col)); 73 for (int i = 0 ; i < n ; i ++) { 74 int l = Bin(li[i] , m , X); 75 int r = Bin(ri[i] , m , X); 76 update(l , r , i , 0 , m , 1); 77 } 78 cnt = 0; 79 memset(hash , false , sizeof(hash)); 80 query(0 , m , 1); 81 printf("%d\n",cnt); 82 } 83 return 0; 84 } 85
poj3225 Help with Intervals
题意:区间操作,交,并,补等
思路:
我们一个一个操作来分析:(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)
U:把区间[l,r]覆盖成1
I:把[-∞,l)(r,∞]覆盖成0
D:把区间[l,r]覆盖成0
C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换
S:[l,r]区间0/1互换
成段覆盖的操作很简单,比较特殊的就是区间0/1互换这个操作,我们可以称之为异或操作
很明显我们可以知道这个性质:当一个区间被覆盖后,不管之前有没有异或标记都没有意义了
所以当一个节点得到覆盖标记时把异或标记清空
而当一个节点得到异或标记的时候,先判断覆盖标记,如果是0或1,直接改变一下覆盖标记,不然的话改变异或标记
开区间闭区间只要数字乘以2就可以处理(偶数表示端点,奇数表示两端点间的区间)
线段树功能:update:成段替换,区间异或 query:简单hash
1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 using namespace std; 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 9 const int maxn = 131072; 10 bool hash[maxn]; 11 int cover[maxn<<2]; 12 int XOR[maxn<<2]; 13 void FXOR(int rt) { 14 if (cover[rt] != -1) cover[rt] ^= 1; 15 else XOR[rt] ^= 1; 16 } 17 void PushDown(int rt) { 18 if (cover[rt] != -1) { 19 cover[rt<<1] = cover[rt<<1|1] = cover[rt]; 20 XOR[rt<<1] = XOR[rt<<1|1] = 0; 21 cover[rt] = -1; 22 } 23 if (XOR[rt]) { 24 FXOR(rt<<1); 25 FXOR(rt<<1|1); 26 XOR[rt] = 0; 27 } 28 } 29 void update(char op,int L,int R,int l,int r,int rt) { 30 if (L <= l && r <= R) { 31 if (op == ‘U‘) { 32 cover[rt] = 1; 33 XOR[rt] = 0; 34 } else if (op == ‘D‘) { 35 cover[rt] = 0; 36 XOR[rt] = 0; 37 } else if (op == ‘C‘ || op == ‘S‘) { 38 FXOR(rt); 39 } 40 return ; 41 } 42 PushDown(rt); 43 int m = (l + r) >> 1; 44 if (L <= m) update(op , L , R , lson); 45 else if (op == ‘I‘ || op == ‘C‘) { 46 XOR[rt<<1] = cover[rt<<1] = 0; 47 } 48 if (m < R) update(op , L , R , rson); 49 else if (op == ‘I‘ || op == ‘C‘) { 50 XOR[rt<<1|1] = cover[rt<<1|1] = 0; 51 } 52 } 53 void query(int l,int r,int rt) { 54 if (cover[rt] == 1) { 55 for (int it = l ; it <= r ; it ++) { 56 hash[it] = true; 57 } 58 return ; 59 } else if (cover[rt] == 0) return ; 60 if (l == r) return ; 61 PushDown(rt); 62 int m = (l + r) >> 1; 63 query(lson); 64 query(rson); 65 } 66 int main() { 67 cover[1] = XOR[1] = 0; 68 char op , l , r; 69 int a , b; 70 while ( ~scanf("%c %c%d,%d%c\n",&op , &l , &a , &b , &r) ) { 71 a <<= 1 , b <<= 1; 72 if (l == ‘(‘) a ++; 73 if (r == ‘)‘) b --; 74 if (a > b) { 75 if (op == ‘C‘ || op == ‘I‘) { 76 cover[1] = XOR[1] = 0; 77 } 78 } else update(op , a , b , 0 , maxn , 1); 79 } 80 query(0 , maxn , 1); 81 bool flag = false; 82 int s = -1 , e; 83 for (int i = 0 ; i <= maxn ; i ++) { 84 if (hash[i]) { 85 if (s == -1) s = i; 86 e = i; 87 } else { 88 if (s != -1) { 89 if (flag) printf(" "); 90 flag = true; 91 printf("%c%d,%d%c",s&1?‘(‘:‘[‘ , s>>1 , (e+1)>>1 , e&1?‘)‘:‘]‘); 92 s = -1; 93 } 94 } 95 } 96 if (!flag) printf("empty set"); 97 puts(""); 98 return 0; 99 }
练习:
poj1436 Horizontally Visible Segments
poj2991 Crane
Another LCIS
Bracket Sequence
区间合并
这类题目会询问区间中满足条件的连续最长区间,所以PushUp的时候需要对左右儿子的区间进行合并
poj3667 Hotel
题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边
2 a b:将[a,a+b-1]的房间清空
思路:记录区间中最长的空房间
线段树操作:update:区间替换 query:询问满足条件的最左断点
1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 using namespace std; 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 9 const int maxn = 55555; 10 int lsum[maxn<<2] , rsum[maxn<<2] , msum[maxn<<2]; 11 int cover[maxn<<2]; 12 13 void PushDown(int rt,int m) { 14 if (cover[rt] != -1) { 15 cover[rt<<1] = cover[rt<<1|1] = cover[rt]; 16 msum[rt<<1] = lsum[rt<<1] = rsum[rt<<1] = cover[rt] ? 0 : m - (m >> 1); 17 msum[rt<<1|1] = lsum[rt<<1|1] = rsum[rt<<1|1] = cover[rt] ? 0 : (m >> 1); 18 cover[rt] = -1; 19 } 20 } 21 void PushUp(int rt,int m) { 22 lsum[rt] = lsum[rt<<1]; 23 rsum[rt] = rsum[rt<<1|1]; 24 if (lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt<<1|1]; 25 if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt<<1]; 26 msum[rt] = max(lsum[rt<<1|1] + rsum[rt<<1] , max(msum[rt<<1] , msum[rt<<1|1])); 27 } 28 void build(int l,int r,int rt) { 29 msum[rt] = lsum[rt] = rsum[rt] = r - l + 1; 30 cover[rt] = -1; 31 if (l == r) return ; 32 int m = (l + r) >> 1; 33 build(lson); 34 build(rson); 35 } 36 void update(int L,int R,int c,int l,int r,int rt) { 37 if (L <= l && r <= R) { 38 msum[rt] = lsum[rt] = rsum[rt] = c ? 0 : r - l + 1; 39 cover[rt] = c; 40 return ; 41 } 42 PushDown(rt , r - l + 1); 43 int m = (l + r) >> 1; 44 if (L <= m) update(L , R , c , lson); 45 if (m < R) update(L , R , c , rson); 46 PushUp(rt , r - l + 1); 47 } 48 int query(int w,int l,int r,int rt) { 49 if (l == r) return l; 50 PushDown(rt , r - l + 1); 51 int m = (l + r) >> 1; 52 if (msum[rt<<1] >= w) return query(w , lson); 53 else if (rsum[rt<<1] + lsum[rt<<1|1] >= w) return m - rsum[rt<<1] + 1; 54 return query(w , rson); 55 } 56 int main() { 57 int n , m; 58 scanf("%d%d",&n,&m); 59 build(1 , n , 1); 60 while (m --) { 61 int op , a , b; 62 scanf("%d",&op); 63 if (op == 1) { 64 scanf("%d",&a); 65 if (msum[1] < a) puts("0"); 66 else { 67 int p = query(a , 1 , n , 1); 68 printf("%d\n",p); 69 update(p , p + a - 1 , 1 , 1 , n , 1); 70 } 71 } else { 72 scanf("%d%d",&a,&b); 73 update(a , a + b - 1 , 0 , 1 , n , 1); 74 } 75 } 76 return 0; 77 }
练习:
hdu3308 LCIS
hdu3397 Sequence operation
hdu2871 Memory Control
hdu1540 Tunnel Warfare
CF46-D Parking Lot
扫描线
这类题目需要将一些操作排序,然后从左到右用一根扫描线(当然是在我们脑子里)扫过去
最典型的就是矩形面积并,周长并等题
hdu1542 Atlantis
题意:矩形面积并
思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用cnt表示该区间下边比上边多几个
线段树操作:update:区间增减 query:直接取根节点的值
1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 using namespace std; 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 9 const int maxn = 2222; 10 int cnt[maxn << 2]; 11 double sum[maxn << 2]; 12 double X[maxn]; 13 struct Seg { 14 double h , l , r; 15 int s; 16 Seg(){} 17 Seg(double a,double b,double c,int d) : l(a) , r(b) , h(c) , s(d) {} 18 bool operator < (const Seg &cmp) const { 19 return h < cmp.h; 20 } 21 }ss[maxn]; 22 void PushUp(int rt,int l,int r) { 23 if (cnt[rt]) sum[rt] = X[r+1] - X[l]; 24 else if (l == r) sum[rt] = 0; 25 else sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 26 } 27 void update(int L,int R,int c,int l,int r,int rt) { 28 if (L <= l && r <= R) { 29 cnt[rt] += c; 30 PushUp(rt , l , r); 31 return ; 32 } 33 int m = (l + r) >> 1; 34 if (L <= m) update(L , R , c , lson); 35 if (m < R) update(L , R , c , rson); 36 PushUp(rt , l , r); 37 } 38 int Bin(double key,int n,double X[]) { 39 int l = 0 , r = n - 1; 40 while (l <= r) { 41 int m = (l + r) >> 1; 42 if (X[m] == key) return m; 43 if (X[m] < key) l = m + 1; 44 else r = m - 1; 45 } 46 return -1; 47 } 48 int main() { 49 int n , cas = 1; 50 while (~scanf("%d",&n) && n) { 51 int m = 0; 52 while (n --) { 53 double a , b , c , d; 54 scanf("%lf%lf%lf%lf",&a,&b,&c,&d); 55 X[m] = a; 56 ss[m++] = Seg(a , c , b , 1); 57 X[m] = c; 58 ss[m++] = Seg(a , c , d , -1); 59 } 60 sort(X , X + m); 61 sort(ss , ss + m); 62 int k = 1; 63 for (int i = 1 ; i < m ; i ++) { 64 if (X[i] != X[i-1]) X[k++] = X[i]; 65 } 66 memset(cnt , 0 , sizeof(cnt)); 67 memset(sum , 0 , sizeof(sum)); 68 double ret = 0; 69 for (int i = 0 ; i < m - 1 ; i ++) { 70 int l = Bin(ss[i].l , k , X); 71 int r = Bin(ss[i].r , k , X) - 1; 72 if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1); 73 ret += sum[1] * (ss[i+1].h - ss[i].h); 74 } 75 printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++ , ret); 76 } 77 return 0; 78 }
hdu1828 Picture
题意:矩形周长并
思路:与面积不同的地方是还要记录竖的边有几个(numseg记录),并且当边界重合的时候需要合并(用lbd和rbd表示边界来辅助)
线段树操作:update:区间增减 query:直接取根节点的值
1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 using namespace std; 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 9 const int maxn = 22222; 10 struct Seg{ 11 int l , r , h , s; 12 Seg() {} 13 Seg(int a,int b,int c,int d):l(a) , r(b) , h(c) , s(d) {} 14 bool operator < (const Seg &cmp) const { 15 return h < cmp.h; 16 } 17 }ss[maxn]; 18 bool lbd[maxn<<2] , rbd[maxn<<2]; 19 int numseg[maxn<<2]; 20 int cnt[maxn<<2]; 21 int len[maxn<<2]; 22 void PushUP(int rt,int l,int r) { 23 if (cnt[rt]) { 24 lbd[rt] = rbd[rt] = 1; 25 len[rt] = r - l + 1; 26 numseg[rt] = 2; 27 } else if (l == r) { 28 len[rt] = numseg[rt] = lbd[rt] = rbd[rt] = 0; 29 } else { 30 lbd[rt] = lbd[rt<<1]; 31 rbd[rt] = rbd[rt<<1|1]; 32 len[rt] = len[rt<<1] + len[rt<<1|1]; 33 numseg[rt] = numseg[rt<<1] + numseg[rt<<1|1]; 34 if (lbd[rt<<1|1] && rbd[rt<<1]) numseg[rt] -= 2;//两条线重合 35 } 36 } 37 void update(int L,int R,int c,int l,int r,int rt) { 38 if (L <= l && r <= R) { 39 cnt[rt] += c; 40 PushUP(rt , l , r); 41 return ; 42 } 43 int m = (l + r) >> 1; 44 if (L <= m) update(L , R , c , lson); 45 if (m < R) update(L , R , c , rson); 46 PushUP(rt , l , r); 47 } 48 int main() { 49 int n; 50 while (~scanf("%d",&n)) { 51 int m = 0; 52 int lbd = 10000, rbd = -10000; 53 for (int i = 0 ; i < n ; i ++) { 54 int a , b , c , d; 55 scanf("%d%d%d%d",&a,&b,&c,&d); 56 lbd = min(lbd , a); 57 rbd = max(rbd , c); 58 ss[m++] = Seg(a , c , b , 1); 59 ss[m++] = Seg(a , c , d , -1); 60 } 61 sort(ss , ss + m); 62 int ret = 0 , last = 0; 63 for (int i = 0 ; i < m ; i ++) { 64 if (ss[i].l < ss[i].r) update(ss[i].l , ss[i].r - 1 , ss[i].s , lbd , rbd - 1 , 1); 65 ret += numseg[1] * (ss[i+1].h - ss[i].h); 66 ret += abs(len[1] - last); 67 last = len[1]; 68 } 69 printf("%d\n",ret); 70 } 71 return 0; 72 }
练习
hdu3265 Posters
hdu3642 Get The Treasury
poj2482 Stars in Your Window
poj2464 Brownie Points II
hdu3255 Farming
ural1707 Hypnotoad’s Secret
uva11983 Weird Advertisement
线段树与其他结合练习(欢迎大家补充):
hdu3333 Turing Tree
hdu3874 Necklace
hdu3016 Man Down
hdu3340 Rain in ACStar
zju3511 Cake Robbery
UESTC1558 Charitable Exchange
CF85-D Sum of Medians
spojGSS2 Can you answer these queries II
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/ya-cpp/p/4165777.html