-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment_tree.cpp
More file actions
80 lines (78 loc) · 1.75 KB
/
Copy pathSegment_tree.cpp
File metadata and controls
80 lines (78 loc) · 1.75 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long ;
vl seg;
vl lazy;
void build(int idx,int low,int high)
{
if(low==high)
{
seg[idx]=arr[low];
return;
}
long long mid=(low+high)>>1;
build(2*idx+1,low,mid);
build(2*idx+2,mid+1,high);
seg[idx]=min(seg[2*idx+1],seg[2*idx+2]);
}
long long query(ll idx,ll low,ll high,ll l,ll r)
{
if(low>=l && high<=r)
{
return seg[ind];
}
if(high<l || low>l) return INT_MIN;
ll mid=(low+high)>>1;
ll left=query(2*idx+1,low,mid,l,r);
ll right=query(2*idx+2,mid+1,high,l,r);
return min(left,right);
}
void rangeupdate(ll ind,ll low,ll high,ll l,ll r,ll val)
{
if(lazy[idx]!=0)
{
seg[idx]+=(lazy[idx]*(high-low+1));
if(low!=high)
{
lazy[2*idx+1]=lazy[idx];
lazy[2*idx+2]=lazy[idx];
}
lazy[idx]=0;
return ;
}
if(l>high || r<low || low>high) return ;
if(low>=l && high<=r)
{
seg[idx]+=(high-low+1)*val;
if(low!=high)
{
lazy[2*idx+1]+=val;
lazy[2*idx+2]+=val;
}
return ;
}
ll mid=(low+high)>>1;
rangeupdate(2*idx+1,low,mid,l,r,val);
rangeupdate(2*idx+2,mid+1,high,l,r,val);
seg[idx]=min(seg[2*idx+1],seg[2*idx+2]);
}
ll query_Sum(ll ind,ll low,ll high,ll l,ll r)
{
if(lazy[idx]!=0)
{
seg[idx]+=(lazy[idx]*(high-low+1));
if(low!=high)
{
lazy[2*idx+1]=lazy[idx];
lazy[2*idx+2]=lazy[idx];
}
lazy[idx]=0;
return ;
}
if(l>high || r<low || low>high) return 0;
if(low>=l && high<=r)
{
return seg[idx];
}
return query_Sum(2*idx+1,low,mid,l,r)+query_Sum(2*idx+2,mid+1,high,l,r);
}