-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_segment_tree.cpp
85 lines (63 loc) · 1.52 KB
/
dynamic_segment_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
74
75
76
77
78
79
80
81
82
83
84
85
class DynamicSegmentTree {
private:
struct node {
node *l,*r;
ll sum,lazy;
node() {
l = r = nullptr;
sum = lazy = 0;
}
};
node* root;
ll left_lim, right_lim;
void process(node *n,ll st,ll en,ll val) {
n->sum = val * (en-st+1);
n->lazy = 0;
if(st != en) {
if( n->l == nullptr) n->l = new node();
if( n->r == nullptr) n->r = new node();
n->l->lazy = val;
n->r->lazy = val;
}
}
void update(node *n,ll st,ll en,ll l,ll r,ll val) {
if ( n->lazy)
process(n,st,en,n->lazy);
if( en < l || r < st) return;
if(l <= st && en <=r) {
process(n,st,en,val);
return;
}
ll mid = (st+en)/2;
if( n->l == nullptr) n->l = new node();
if( n->r == nullptr) n->r = new node();
update(n->l,st,mid,l,r,val);
update(n->r,mid+1,en,l,r,val);
n->sum = n->l->sum + n->r->sum;
}
ll query(node *n,ll st,ll en,ll l,ll r) {
if ( n->lazy)
process(n,st,en,n->lazy);
if( en < l || r < st) return 0LL;
if(l <= st && en <=r)
return n->sum;
ll mid = (st+en)/2;
if( n->l == nullptr) n->l = new node();
if( n->r == nullptr) n->r = new node();
ll s1 = query(n->l,st,mid,l,r);
ll s2 = query(n->r,mid+1,en,l,r);
return s1 + s2;
}
public:
DynamicSegmentTree() {
root = new node();
left_lim = 1LL;
right_lim = 500000000LL;
}
void update(ll l,ll r,ll val) {
update(root, left_lim, right_lim, l, r, val);
}
ll query(ll l,ll r) {
return query(root, left_lim, right_lim, l, r);
}
};