-
Notifications
You must be signed in to change notification settings - Fork 12
/
segmet tree.cpp
74 lines (62 loc) · 1.82 KB
/
segmet tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// *** Segment Tree [log(total array size)*Query]
/** [ulow,uhigh] Query Range
* [low,high] total range of root
* [qlow,qhigh] Query Range
* Currrent position = pos
* 0 based Index And Root is also 0
*/
int ara[MX],seg[4*MX],lazy[4*MX];
void creat(int low,int high,int pos)
{
if(low==high){
seg[pos] = ara[low]; // reached leaf and update
return ;
}
int mid = (high+low)/2;
creat(low,mid,pos*2+1);
creat(mid+1,high,pos*2+2);
seg[pos] += seg[pos*2+1] + seg[pos*2+2];
}
void update(int low,int high,int ulow,int uhigh,int val,int pos)
{
if(low>high) return ;
if(lazy[pos]!=0){ /// is not propagated yet
seg[pos] += (high-low+1)*lazy[pos];
if(low!=high){ ///if not leaf node
lazy[pos*2+1] += lazy[pos];
lazy[pos*2+2] += lazy[pos];
}
lazy[pos] = 0;
}
if(ulow>high||uhigh<low) return; ///No overlap
if(ulow<=low&&uhigh>=high){ /// Total Overlap
seg[pos] += (high-low+1)*val;
if(low!=high){
lazy[pos*2+1] += val;
lazy[pos*2+2] += val;
}
return;
}
/// Partial overlap
int mid = (high+low)/2;
update(low,mid,ulow,uhigh,val,pos*2+1);
update(mid+1,high,ulow,uhigh,val,pos*2+2);
seg[pos] = seg[pos*2+1] + seg[pos*2+2]; /// Updating the intermediate node
}
int query(int low,int high,int qlow,int qhigh,int pos)
{
if(low>high) return 0;
if(lazy[pos]!=0){
seg[pos] += (high-low+1)*lazy[pos];
if(low!=high){
lazy[pos*2+1] += lazy[pos];
lazy[pos*2+2] += lazy[pos];
}
lazy[pos] = 0;
}
if(qlow>high||qhigh<low) return 0;
if(qlow<=low&&qhigh>=high)
return seg[pos];
int mid = (high+low)/2;
return query(low,mid,qlow,qhigh,pos*2+1) + query(mid+1,high,qlow,qhigh,pos*2+2);
}