-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegtree.snippets
142 lines (127 loc) · 2.4 KB
/
segtree.snippets
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
snippet segment_tree
template<typename st_node>
struct segment_tree {
int size = 0;
vector<st_node> arr;
segment_tree(vector<st_node> v) {
size = v.size();
arr.resize(2*size);
for (int i = size; i < 2*size; ++i)
arr[i] = v[i-size];
for (int i = size-1; i >= 1; --i)
arr[i] = arr[2*i].comb(arr[2*i+1]);
}
segment_tree(int t, st_node x) : segment_tree(vector<st_node>(t, x)) { }
void upd(int pos, st_node val) {
pos += size; arr[pos] = val;
while (pos > 1) {
pos /= 2;
arr[pos] = arr[2*pos].comb(arr[2*pos+1]);
}
}
void refresh(int pos, st_node refr) {
upd(pos, arr[pos+size].comb(refr));
}
void add(int pos, ll delta) {
upd(pos, arr[pos+size].value+delta);
}
st_node get(int lo, int ri) {
lo += size;
ri += size+1;
st_node res;
while (lo < ri) {
if (lo & 1)
res = res.comb(arr[lo++]);
if (ri & 1)
res = res.comb(arr[--ri]);
lo /= 2; ri /= 2;
}
return res;
}
};
$0
endsnippet
snippet st_node
struct ${1:st_node} {
${2:datatype} value;
$1() : value(${3:be_careful}) {}
$1($2 x) : value(x) {}
operator $2() { return value; }
min32 comb(const min32 &oth) {
return ${4:OP};
}
};
$0
endsnippet
snippet min32
struct min32 {
int value;
min32() : value(1e9) {}
min32(int x) : value(x) {}
operator int() { return value; }
min32 comb(const min32 &oth) {
return min(value, oth.value);
}
};
$0
endsnippet
snippet max32
struct max32 {
int value;
max32() : value(-1e9) {}
max32(int x) : value(x) {}
operator int() { return value; }
max32 comb(const max32 &oth) {
return max(value, oth.value);
}
};
$0
endsnippet
snippet sum32
struct sum32 {
int value;
sum32() : value(0) {}
sum32(int x) : value(x) {}
operator int() { return value; }
sum32 comb(const sum32 &oth) {
return value + oth.value;
}
};
$0
endsnippet
snippet min64
struct min64 {
ll value;
min64() : value(2e18) {}
min64(ll x) : value(x) {}
operator ll() { return value; }
min64 comb(const min64 &oth) {
return min(value, oth.value);
}
};
$0
endsnippet
snippet max64
struct max64 {
ll value;
max64() : value(-2e18) {}
max64(ll x) : value(x) {}
operator ll() { return value; }
max64 comb(const max64 &oth) {
return max(value, oth.value);
}
};
$0
endsnippet
snippet sum64
struct sum64 {
ll value;
sum64() : value(0) {}
sum64(ll x) : value(x) {}
operator ll() { return value; }
sum64 comb(const sum64 &oth) {
return value + oth.value;
}
};
$0
endsnippet