線段樹—Lazy_Tag

轉載LINK:LAZY_TAGhtml

先看一個具體問題吧 PKU 3468數據結構

http://poj.org/problem?id=3468優化

題意很清楚 1 ≤ N,Q ≤ 100000.ui

"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.spa

 

用樸素的作法是O(NQ)的 明顯TLE3d

因爲是區間統計問題 咱們嘗試用線段樹解決code

先考慮線段樹節點記錄什麼htm

左右兒子 區間範圍是必須的:ls[] rs[] l[] r[]blog

爲了回答詢問咱們還要記錄一個S[]來保存當前區間的和遞歸

*建樹不是很難 注意要把s[]值從兒子處遞推上來

 

複製代碼
 1 procedure build(a,b:longint);
2  var x,mid:longint;
3  begin
4 inc(tt); x:=tt;
5 l[x]:=a; r[x]:=b;
6  if b-a=1
7 thenbegin
8 s[x]:=m[b];
9 exit; end
10 elsebegin
11 mid:=(a+b)shr 1;
12 ls[x]:=tt+1; build(a,mid);
13 rs[x]:=tt+1; build(mid,b);
14 s[x]:=s[ls[x]]+s[rs[x]];
15 end;
16  end;
複製代碼

 

 

*當咱們碰到C操做的時候把被a到b之間的區間覆蓋全部線段樹區間都修改一下

  +第三行判斷是否被徹底覆蓋 是就直接修改s[]值

  +第五-七行判斷是否有交集 遞歸修改

  +第八行 當且僅當沒有被徹底覆蓋 才從兒子處獲得新的s[]值

 

複製代碼
 1 procedure insert(x,a,b,c:longint);
2  var mid:longint;
3  if (a<=l[x])and(r[x]<=b)
4 then s[x]:=s[x]+(r[x]-l[x])*c;
5 mid:=(l[x]+r[x])shr 1;
6  if a<mid then insert(ls[x],a,b,c);
7  if mid<b then insert(rs[x],a,b,c);
8  ifnot(a<=l[x])and(r[x]<=b)
9 then s[x]:=s[ls[x]]+s[rs[x]];
10  end;  
複製代碼

 

 

*碰到Q操做的時候就遞歸查詢全部在a到b區間內的子區間 求和獲得答案

  -第五行若是被所查區間徹底覆蓋 直接返回值

  -第10-14行 根據交集狀況遞歸查詢

 

複製代碼
 1 function query(x,a,b:longint):int64;
2  var mid:longint;
3 ans:int64;
4  begin
5  if (a<=l[x])and(r[x]<=b)
6 thenbegin
7 query:=s[x];
8 exit;
9 end;
10 ans:=0;
11 mid:=(l[x]+r[x])shr 1;
12  if a<mid then ans:=ans+query(ls[x],a,b);
13  if mid<b then ans:=ans+query(rs[x],a,b);
14 query:=ans;
15  end;
複製代碼

 

 

 

能夠證實 查詢操做的複雜度不超過O(Log2N)[常數小於2] 能夠知足問題需求

可是修改操做的最壞複雜度能夠達到O(N) 並且常數比樸素還大 必須考慮優化

咱們能夠經過一下兩個示意圖 看到兩個操做的差距

查詢[2,10]

修改[2,10]

爲了解決這個問題 著名的Lazy-Tag思想應運而生

每次都把全部區間修改了太慢了 不如懶一點 先把賬記着 到時候再作

懶-記賬=Lazy-Tag

怎麼記錄呢? 咱們須要再加一個域v[] 這個域就是標記

用來記錄當前節點爲根的子樹 是否須要統一加一個數 具體要加多少

咱們的程序就要改一下了

  +每當碰到要當前區間徹底被欲修改區間覆蓋時 直接給加在標記上 而後退出

  +每次訪問到一個節點時 首先清空當前節點的標記

    -訪問包括各類操做 不論是插入 刪除 仍是查詢 甚至是僅僅用到節點的s[]值

    -清空不只僅是用標記更新當前節點 還包括把標記下傳給左右子樹

*咱們用一個單獨的過程clean來執行清空標記的操做

須要注意的是 葉子節點不用下傳標記 不然RE

 

複製代碼
 1 procedure clean(x:longint);
2  begin
3  if v[x]<>0
4 thenbegin
5 s[x]:=s[x]+(r[x]-l[x])*v[x];
6 if ls[x]<>0then v[ls[x]]:=v[ls[x]]+v[x];
7 if rs[x]<>0then v[rs[x]]:=v[rs[x]]+v[x];
8 v[x]:=0;
9 end;
10  end;
複製代碼

*修改後的修改過程

 

  +第一行行先清空標記

  +第五行若是徹底覆蓋 修改標記 退出

  +第10-12行 遞歸修改子樹

  +第13-14行 更新當前區間的s[]值 必須先清空標記!

 

複製代碼
 1 procedure insert(x,a,b,c:longint);
2  var mid:longint;
3  begin
4 clean(x);
5  if (a<=l[x])and(r[x]<=b)
6 thenbegin
7 v[x]:=v[x]+c;
8 exit;
9 end;
10 mid:=(l[x]+r[x])shr 1;
11  if a<mid then insert(ls[x],a,b,c);
12 if mid<b then insert(rs[x],a,b,c);
13 clean(ls[x]); clean(rs[x]);
14 s[x]:=s[ls[x]]+s[rs[x]];
15 end;
複製代碼

*修改後的查詢

 

也要加上清空標記

而因爲加上了這個遞歸完了更新s[]值也在所不免

 

複製代碼
 1 function query(x,a,b:longint):int64;
2 var mid:longint;
3 ans:int64;
4 begin
5 clean(x);
6 if (a<=l[x])and(r[x]<=b)
7 thenbegin
8 query:=s[x];
9 exit;
10 end;
11 ans:=0;
12 mid:=(l[x]+r[x])shr 1;
13 if a<mid then ans:=ans+query(ls[x],a,b);
14 if mid<b then ans:=ans+query(rs[x],a,b);
15 clean(ls[x]); clean(rs[x]);
16 s[x]:=s[ls[x]]+s[rs[x]];
17 query:=ans;
18 end;
複製代碼

完整的代碼在文章最後

 

固然Lazy-Tag能夠用於極爲強大的數據結構Splay

解決的這個問題 咱們還沒獲得運用Lazy-Tag的通常規律

我還想到了另一個問題 若是還帶區間總體乘一個數呢?

 

接下來討論雙標記的問題

因爲還要乘一個數 咱們再加一個標記域

用u[]表示節點x爲根的子樹須要加u[x]

用v[]表示節點x爲根的子樹須要乘v[x]

實際運算中 咱們發現乘法和加法的操做序列是不肯定

因此咱們要用2個標記來歸納整個操做序列還須要考慮

回過頭去看上一個問題 只有加法的時候 不管加法序列是什麼樣子的

根據加法的交換律和結合律 咱們只要把整個序列的和保存下來就能夠了

可是有乘有加就不能簡單的累記了

比較好的方法是先規定標記的操做規則 先乘再加

對於任意節點x v[x]和u[x]表示當前的s[x]須要更新爲s[x]*v[x]+u[x]

而後 每當咱們要修改標記的時候

  若是要求給當前區間乘一個數c則將v[x]和u[x]都乘c

    (s[x]*v[x]+u[x])*c=s[x]*(v[x]*c)+(u[x]*c)

  若是要求給當前區間加一個數c 則只將u[x]+c便可

    (s[x]*v[x]+u[x])+c=s[x]*v[x]+(u[x]+c)

只有當對於任意一種操做

咱們都保證可以無條件直接修改區間上的標記來達到效果

咱們才能夠方便地使用Lazy-Tag

不然當下傳標記的時候還要考慮先清空兒子的標記

clean過程就變成遞歸的過程 效率又變回O(N)了

代碼之須要在上一個問題上加以修改 貼在文章最後

核心的過程是clean

 

複製代碼
 1 procedure clean(x:longint);
2 begin
3 if (u[x]<>0)or(v[x]<>1)
4 thenbegin
5 s[x]:=s[x]*v[x]+u[x]*(r[x]-l[x]);
6 if ls[x]<>0
7 thenbegin
8 v[ls[x]]:=v[ls[x]]*v[x];
9 u[ls[x]]:=u[ls[x]]*v[x]+u[x];
10 end;
11 if rs[x]<>0
12 thenbegin
13 v[rs[x]]:=v[rs[x]]*v[x];
14 u[rs[x]]:=u[rs[x]]*v[x]+u[x];
15 end;
16 v[x]:=1; u[x]:=0;
17 end;
18 end;
複製代碼

咱們再討論一個問題 也是雙標記的

 

維護一個序列

  支持操做給一個區間統一一個數

  和給一個區間統一修改爲一個數

  還要詢問區間和是多少

很容易想到額外維護三個域 s[]{Sum} c[]{Cover} d[]{Delta}

分別表示當前區間的和爲s 當前區間被覆蓋成c 當前區間須要加d

一樣的 分析兩個標記域d[]和c[]的關係

若是兩個標記域都存在 那將會是一件棘手的事情 是先加再覆蓋仍是先覆蓋再加呢?

咱們能夠發現如下幾個性質

  若是對某個區間x覆蓋了c[x]以後 之前的c[x]和d[x]都會自動消失

  對某個區間加d[x]以後 若是c[x]有值 能夠直接加在c[x]上

這樣咱們就能夠保證c[x]和d[x]最多隻一個值存在 就沒有上面的問題了

因此不妨規定c[]的優先級高於d[] 當若是c[x]有值就只執行c[x] 不然執行d[x]

咱們又獲得了一個結論 對於互相干涉的標記要定下明確的優先級 保證操做有序

核心仍是clean過程

 

複製代碼
 1 procedure down(x:longint);
2 begin
3 if c[x]<>key
4 thenbegin
5 s[x]:=c[x]*(r[x]-l[x]);
6 if ls[x]<>0
7 thenbegin
8 c[ls[x]]:=c[x];
9 d[ls[x]]:=0;
10 end;
11 if rs[x]<>0
12 thenbegin
13 c[rs[x]]:=c[x];
14 d[rs[x]]:=0;
15 end;
16 c[x]:=key;
17 end
18 elseif d[x]<>0
19 thenbegin
20 s[x]:=s[x]+d[x]*(r[x]-l[x]);
21 if ls[x]<>0thenif c[ls[x]]<>key
22 then c[ls[x]]:=c[ls[x]]+d[x]
23 else d[ls[x]]:=d[ls[x]]+d[x];
24 if rs[x]<>0thenif c[rs[x]]<>key
25 then c[rs[x]]:=c[rs[x]]+d[x]
26 else d[rs[x]]:=d[rs[x]]+d[x];
27 d[x]:=0;
28 end;
29 end;
複製代碼

 

 

對Lazy-Tag的討論就到這裏

下一篇討論 線段樹的擴展

 

Bob HAN 原創 轉載請註明出處 http://www.cnblogs.com/Booble/

 

附 完整代碼

Simple

 

複製代碼
 1 const    maxn=200000;
2  var l,r,ls,rs:array[1..maxn shl 1]of longint;
3 v,s:array[1..maxn shl 1]of int64;
4 m:array[1..maxn]of longint;
5 n,tt,i,q,a,b,c:longint;
6 ch,blank:char;
7  procedure build(a,b:longint);
8  var x,mid:longint;
9  begin
10 inc(tt); x:=tt;
11 l[x]:=a; r[x]:=b;
12 v[x]:=0;
13  if b-a=1
14 thenbegin
15 s[x]:=m[b];
16 exit; end
17 elsebegin
18 mid:=(a+b)shr 1;
19 ls[x]:=tt+1; build(a,mid);
20 rs[x]:=tt+1; build(mid,b);
21 s[x]:=s[ls[x]]+s[rs[x]];
22 end;
23  end;
24  procedure clean(x:longint);
25  begin
26  if v[x]<>0
27 thenbegin
28 s[x]:=s[x]+(r[x]-l[x])*v[x];
29 if ls[x]<>0then v[ls[x]]:=v[ls[x]]+v[x];
30 if rs[x]<>0then v[rs[x]]:=v[rs[x]]+v[x];
31 v[x]:=0;
32 end;
33  end;
34  procedure insert(x,a,b,c:longint);
35  var mid:longint;
36  begin
37 clean(x);
38  if (a<=l[x])and(r[x]<=b)
39 thenbegin
40 v[x]:=v[x]+c;
41 exit;
42 end;
43 mid:=(l[x]+r[x])shr 1;
44  if a<mid then insert(ls[x],a,b,c);
45  if mid<b then insert(rs[x],a,b,c);
46 clean(ls[x]); clean(rs[x]);
47 s[x]:=s[ls[x]]+s[rs[x]];
48  end;
49  function query(x,a,b:longint):int64;
50  var mid:longint;
51 ans:int64;
52  begin
53 clean(x);
54  if (a<=l[x])and(r[x]<=b)
55 thenbegin
56 query:=s[x];
57 exit;
58 end;
59 ans:=0;
60 mid:=(l[x]+r[x])shr 1;
61  if a<mid then ans:=ans+query(ls[x],a,b);
62  if mid<b then ans:=ans+query(rs[x],a,b);
63 clean(ls[x]); clean(rs[x]);
64 s[x]:=s[ls[x]]+s[rs[x]];
65 query:=ans;
66  end;
67  begin
68 assign(input,'simple.in'); reset(input);
69 assign(output,'simple.out'); rewrite(output);
70 readln(n,q);
71 for i:=1to n do
72 read(m[i]);
73 tt:=0;
74 build(0,n);
75 readln;
76 for i:=1to q do
77 begin
78 read(ch); read(blank);
79 case ch of
80 'Q': begin
81 readln(a,b);
82 writeln(query(1,a-1,b));
83 end;
84 'C': begin
85 readln(a,b,c);
86 insert(1,a-1,b,c);
87 end;
88 end;
89 end;
90 close(input); close(output);
91 end.
92
複製代碼

SuperSImple

 

 

複製代碼
  1 const    maxn=100000;
2 var l,r,ls,rs:array[1..maxn shl 1-1]of longint;
3 u,v,s:array[1..maxn shl 1-1]of int64;
4 m:array[1..maxn]of longint;
5 n,q,tt,i,a,b,c:longint;
6 ch,blank:char;
7 procedure build(a,b:longint);
8 var mid,x:longint;
9 begin
10 inc(tt); x:=tt;
11 l[x]:=a; r[x]:=b;
12 u[x]:=0; v[x]:=1;
13 if b-a=1
14 then s[x]:=m[b]
15 elsebegin
16 mid:=(a+b)shr 1;
17 ls[x]:=tt+1; build(a,mid);
18 rs[x]:=tt+1; build(mid,b);
19 s[x]:=s[ls[x]]+s[rs[x]];
20 end;
21 end;
22 procedure clean(x:longint);
23 begin
24 if (u[x]<>0)or(v[x]<>1)
25 thenbegin
26 s[x]:=s[x]*v[x]+u[x]*(r[x]-l[x]);
27 if ls[x]<>0
28 thenbegin
29 v[ls[x]]:=v[ls[x]]*v[x];
30 u[ls[x]]:=u[ls[x]]*v[x]+u[x];
31 end;
32 if rs[x]<>0
33 thenbegin
34 v[rs[x]]:=v[rs[x]]*v[x];
35 u[rs[x]]:=u[rs[x]]*v[x]+u[x];
36 end;
37 v[x]:=1; u[x]:=0;
38 end;
39 end;
40 procedure mult(x,a,b,c:longint);
41 var mid:longint;
42 begin
43 clean(x);
44 if (a<=l[x])and(r[x]<=b)
45 thenbegin
46 u[x]:=u[x]*c;
47 v[x]:=v[x]*c;
48 exit; end;
49 mid:=(l[x]+r[x])shr 1;
50 if a<mid then mult(ls[x],a,b,c);
51 if mid<b then mult(rs[x],a,b,c);
52 clean(ls[x]); clean(rs[x]);
53 s[x]:=s[ls[x]]+s[rs[x]];
54 end;
55 procedure plus(x,a,b,c:longint);
56 var mid:longint;
57 begin
58 clean(x);
59 if (a<=l[x])and(r[x]<=b)
60 thenbegin
61 u[x]:=u[x]+c;
62 exit; end;
63 mid:=(l[x]+r[x])shr 1;
64 if a<mid then plus(ls[x],a,b,c);
65 if mid<b then plus(rs[x],a,b,c);
66 clean(ls[x]); clean(rs[x]);
67 s[x]:=s[ls[x]]+s[rs[x]];
68 end;
69 function query(x,a,b:longint):int64;
70 var mid:longint;
71 ans:int64;
72 begin
73 clean(x);
74 if (a<=l[x])and(r[x]<=b)
75 thenbegin
76 query:=s[x];
77 exit; end;
78 ans:=0;
79 mid:=(l[x]+r[x])shr 1;
80 if a<mid then ans:=ans+query(ls[x],a,b);
81 if mid<b then ans:=ans+query(rs[x],a,b);
82 query:=ans;
83 clean(ls[x]); clean(rs[x]);
84 s[x]:=s[ls[x]]+s[rs[x]];
85 end;
86 begin
87 assign(input,'ssimple.in'); reset(input);
88 assign(output,'ssimple.out'); rewrite(output);
89 readln(n,q);
90 for i:=1to n do
91 read(m[i]);
92 readln;
93 tt:=0;
94 build(0,n);
95 for i:=1to q do
96 begin
97 read(ch); read(blank);
98 case ch of
99 'Q': begin
100 readln(a,b);
101 writeln(query(1,a-1,b));
102 end;
103 'M': begin
104 readln(a,b,c);
105 mult(1,a-1,b,c);
106 end;
107 'P': begin
108 readln(a,b,c);
109 plus(1,a-1,b,c);
110 end;
111 end;
112 end;
113 close(input); close(output);
114 end.
115
複製代碼

Paint

 

 

複製代碼
  1 const    maxn=100000;
2 key=-219;
3 max=maxn*2;
4 var l,r,ls,rs,c,d,s:array[1..max]of longint;
5 v:array[1..maxn]of longint;
6 m,k,i,tt,x,y,z:longint;
7 ch,ignore:char;
8 procedure down(x:longint);
9 begin
10 if c[x]<>key
11 thenbegin
12 s[x]:=c[x]*(r[x]-l[x]);
13 if ls[x]<>0
14 thenbegin
15 c[ls[x]]:=c[x];
16 d[ls[x]]:=0;
17 end;
18 if rs[x]<>0
19 thenbegin
20 c[rs[x]]:=c[x];
21 d[rs[x]]:=0;
22 end;
23 c[x]:=key;
24 end
25 elseif d[x]<>0
26 thenbegin
27 s[x]:=s[x]+d[x]*(r[x]-l[x]);
28 if ls[x]<>0thenif c[ls[x]]<>key
29 then c[ls[x]]:=c[ls[x]]+d[x]
30 else d[ls[x]]:=d[ls[x]]+d[x];
31 if rs[x]<>0thenif c[rs[x]]<>key
32 then c[rs[x]]:=c[rs[x]]+d[x]
33 else d[rs[x]]:=d[rs[x]]+d[x];
34 d[x]:=0;
35 end;
36 end;
37 procedure update(x:longint);
38 begin
39 down(ls[x]); down(rs[x]);
40 s[x]:=s[ls[x]]+s[rs[x]];
41 end;
42 procedure build(a,b:longint);
43 var x,mid:longint;
44 begin
45 inc(tt); x:=tt;
46 l[x]:=a; r[x]:=b;
47 d[x]:=0; c[x]:=key;
48 if b-a=1
49 then s[x]:=v[b]
50 elsebegin
51 mid:=(a+b)shr 1;
52 ls[x]:=tt+1; build(a,mid);
53 rs[x]:=tt+1; build(mid,b);
54 s[x]:=s[ls[x]]+s[rs[x]];
55 end;
56 end;
57 procedure cover(x,a,b,v:longint);
58 var mid:longint;
59 begin
60 down(x);
61 if (a<=l[x])and(r[x]<=b)
62 thenbegin c[x]:=v; d[x]:=0; end
63 elsebegin
64 mid:=(l[x]+r[x])shr 1;
65 if a<mid then cover(ls[x],a,b,v);
66 if b>mid then cover(rs[x],a,b,v);
67 update(x);
68 end;
69 end;
70 procedure insert(x,a,b,v:longint);
71 var mid:longint;
72 begin
73 down(x);
74 if (a<=l[x])and(r[x]<=b)
75 thenif c[x]<>key
76 then c[x]:=c[x]+v
77 else d[x]:=d[x]+v
78 elsebegin
79 mid:=(l[x]+r[x])shr 1;
80 if a<mid then insert(ls[x],a,b,v);
81 if b>mid then insert(rs[x],a,b,v);
82 update(x);
83 end;
84 end;
85 function sum(x,a,b:longint):longint;
86 var mid:longint;
87 begin
88 down(x);
89 if (a<=l[x])and(r[x]<=b)
90 then sum:=s[x]
91 elsebegin
92 sum:=0;
93 mid:=(l[x]+r[x])shr 1;
94 if a<mid then sum:=sum+sum(ls[x],a,b);
95 if b>mid then sum:=sum+sum(rs[x],a,b);
96 update(x);
97 end;
98 end;
99 begin
100 assign(input,'paint.in'); reset(input);
101 assign(output,'paint.out'); rewrite(output);
102 readln(m,k);
103 for i:=1to m do
104 read(v[i]);
105 readln;
106 tt:=0;
107 build(0,m);
108 for i:=1to k do
109 begin
110 read(ch);
111 repeat read(ignore); until ignore='';
112 read(x,y);
113 if ch<>'S'then read(z);
114 readln;
115 case ch of
116 'C':cover(1,x-1,y,z);
117 'A':insert(1,x-1,y,z);
118 'S':writeln(sum(1,x-1,y));
119 end;
120 end;
121 close(input); close(output);
122 end.
123
複製代碼
相關文章
相關標籤/搜索