-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegtreeshort.cpp
60 lines (54 loc) · 1.14 KB
/
segtreeshort.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
struct segtree
{
vector<PLL>store;
ll n;
void init(ll x)
{
n=x;
store.resize(x*4,{0,-1});
}
void build(ll start,ll end,ll ind,vector<ll>vals)
{
if(start == end)
{
store[ind] = {vals[start],start};
return;
}
ll mid = start+ (end-start)/2;
build(start,mid,2*ind+1,vals);
build(mid+1,end,2*ind+2,vals);
store[ind]=Max(store[2*ind+1],store[2*ind+2]);
}
pair<long,long>query1(ll start,ll end,ll ind,ll l, ll r)
{
if((l < start && r<start ) || (l>end && r>end))
{
return {-9223372036854775807LL,0};
}
if(start>=l && end<=r)
{
return store[ind];
}
ll mid= start + (end-start)/2;
PLL OPS1= query1(start,mid,2*ind+1,l,r);
PLL OPS2= query1(mid+1,end,2*ind+2,l,r);
return Max(OPS1,OPS2);
}
void update(ll start,ll end,ll ind,ll need_ind,ll valAdd)
{
if(start == end)
{
store[ind].F += valAdd;
return;
}
ll mid = start+ (end-start)/2;
if(need_ind<=mid)
{
update(start,mid,2*ind+1,need_ind,valAdd);
}else
{
update(mid+1,end,2*ind+2,need_ind,valAdd);
}
store[ind]=Max(store[2*ind+1],store[2*ind+2]);
}
};