-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment+lazy.cpp
executable file
·65 lines (55 loc) · 1.46 KB
/
segment+lazy.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
struct nodo{
ll sum;
ll add;// acumulado para el (LP)
nodo() { }
nodo(ll _sum, ll _add){
sum = _sum;
add = _add;
}
}T[MAXN * 4];
int a[MAXN];
void relax(int node, int b, int e){
T[node].sum += T[node].add*((e-b)+1);
if(b==e){
//T[node].add=T[b].add;
}else{
T[node+node+1].add += T[node].add;
T[node+node+2].add += T[node].add;
}
T[node].add = 0;
}
void init(int b, int e, int node){
if(b==e) T[node].sum = a[b];
int mid = (b+e)/2, le = 2*node + 1, ri = 2*node + 2;
init(b,mid,le);
init(mid+1,e,ri);
T[node].sum=T[le].sum+T[ri].sum;
}
void update(int b, int e, int node, int i,int j, int val)
{ relax(node,b,e);
if(j < b || i > e) return;
if(i <= b && e <= j){
T[node].add += val;
relax(node,b,e);
return;
}
int mid = (b + e)/2, le = 2*node + 1, ri = 2*node + 2;
update(b, mid, le, i,j, val);
update(mid + 1, e, ri, i,j, val);
T[node].sum = T[le].sum + T[ri].sum;
}
nodo query(int b, int e,int node, int i, int j)
{
relax(node,b,e);
if(i <= b && e <= j) return T[node];
int mid = (b + e) / 2, le = 2*node + 1, ri = 2*node + 2;
if(j<=mid) return query(b, mid, le, i, j);
else if(mid<i) return query(mid + 1, e, ri, i, j);
else{
nodo ret;
nodo ret1=query(b, mid, le, i, j);
nodo ret2=query(mid + 1, e, ri, i, j);
ret.sum=ret1.sum+ret2.sum;
return ret;
}
}