forked from abhiy13/Code-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segtreemine.sublime-snippet
executable file
·82 lines (73 loc) · 1.77 KB
/
segtreemine.sublime-snippet
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
<snippet>
<content><![CDATA[
template <typename T>
class SegTree{
private:
vector < T > tree;
ll N , n;
T combine(T A , T B){
return max(A , B);
}
ll build(vector < T > ar , ll s , ll e , ll idx){
if(s==e){
tree[idx] = ar[s];
return ar[s];
}
ll mid = (s+e) >> 1;
ll lf = idx << 1;
ll rf = lf|1;
tree[idx] = combine(build(ar,s,mid,lf) , build(ar,mid+1,e,rf));
return tree[idx];
}
T query(ll s , ll e , ll lf , ll rf , ll idx){
if(rf < s or e < lf) return -inf;
if(lf <= s and e <= rf) return tree[idx];
ll mid = (s+e) >> 1;
ll l = idx << 1;
ll r = lf|1;
if(lf <= mid){
if(rf <= mid){
return query(s,mid,lf,rf,l);
}else{
return combine(query(s,mid,lf,rf,l),query(mid+1,e,lf,rf,r));
}
}else{
return query(mid+1,e,lf,rf,r);
}
}
void update(ll s , ll e , ll i , T val , ll idx){
if(s > i or e < i) return;
if(s==e){
tree[idx] = val;
return;
}
ll mid = (s+e) >> 1;
ll lf = idx << 1;
ll rf = lf|1;
update(s,mid,i,val,lf);
update(mid+1,e,i,val,rf);
tree[idx] = combine(tree[lf] , tree[rf]);
return;
}
public:
SegTree(vector < T > ar){
n = ar.size();
N = 4*((int) ar.size()) + 1;
tree.resize(N, {});
build(ar , 0 , n - 1 , 1);
}
T query(ll l ,ll r){
return query(0 , n - 1 , l , r , 1);
}
void update(ll i , T val){
update(0 , n - 1 , i , val , 1);
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>segmine</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.cpp, source.c++, source.c</scope>
<!-- Optional: Description to show in the menu -->
<description>Segtree Mine</description>
</snippet>