-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsegment_tree_lazy.cpp
More file actions
48 lines (42 loc) · 995 Bytes
/
segment_tree_lazy.cpp
File metadata and controls
48 lines (42 loc) · 995 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 17;
int a[N],s[N << 1],lz[N << 1];
void pushlz(int l,int r,int idx)
{
if(!lz[idx]) return;
s[idx]+=lz[idx];
if(l!=r) lz[idx*2]+=lz[idx],lz[idx*2+1]+=lz[idx];
lz[idx] = 0;
}
void build(int l,int r,int idx)
{
if(l==r){ s[idx] = a[l]; return; }
int m = (l+r)/2;
build(l,m,idx*2);
build(m+1,r,idx*2+1);
s[idx] = min(s[idx*2],s[idx*2+1]);
}
void update(int l,int r,int idx,int x,int y,int val)
{
if(x>r or y<l) return;
pushlz(l,r,idx);
if(x<=l and y>=r)
{
lz[idx]+=val;
pushlz(l,r,idx);
return;
}
int m = (l+r)/2;
update(l,m,idx*2,x,y,val);
update(m+1,r,idx*2+1,x,y,val);
s[idx] = min(s[idx*2],s[idx*2+1]);
}
int query(int l,int r,int idx,int x,int y)
{
if(x>r or y<l) return INT_MAX;
pushlz(l,r,idx);
if(x<=l and y>=r) return s[idx];
int m = (l+r)/2;
return min(query(l,m,idx*2,x,y),query(m+1,r,idx*2+1,x,y));
}