1- #include " ../common/common.hpp"
1+ #include " ../0- common/common.hpp"
22
33// what: point update + range sum on a fixed-size array using a tree.
44// time: build O(n), update/query O(log n); memory: O(n)
55// constraint: 1-indexed [1, n]; a[0] unused.
66// usage: seg_tree st; st.build(a); st.set(p, v); st.query(l, r);
77struct seg_tree {
88 int flag;
9- vector<ll> t;
10- void build (const vector<ll> &a) {
9+ vl t;
10+ void build (const vl &a) {
1111 // goal: build tree from 1-indexed array.
1212 int n = sz (a) - 1 ;
1313 flag = 1 ;
@@ -36,8 +36,8 @@ struct seg_tree {
3636// usage: seg_tree_it st; st.build(a); st.set(p, v); st.query(l, r);
3737struct seg_tree_it { // 0-indexed
3838 int n;
39- vector<ll> t;
40- void build (const vector<ll> &a) {
39+ vl t;
40+ void build (const vl &a) {
4141 // goal: build tree from 0-indexed array.
4242 n = sz (a);
4343 t.assign (2 * n, 0 );
@@ -65,7 +65,7 @@ struct seg_tree_it { // 0-indexed
6565// usage: seg_tree_kth st; st.init(n); st.add(p, v); st.kth(k);
6666struct seg_tree_kth {
6767 int flag;
68- vector<ll> t;
68+ vl t;
6969 void init (int n) {
7070 // goal: allocate tree for size n.
7171 flag = 1 ;
@@ -91,8 +91,8 @@ struct seg_tree_kth {
9191// usage: seg_tree_lz st; st.build(a); st.add(l, r, v); st.query(l, r);
9292struct seg_tree_lz {
9393 int flag;
94- vector<ll> t, lz;
95- void build (const vector<ll> &a) {
94+ vl t, lz;
95+ void build (const vl &a) {
9696 // goal: build tree and clear lazy tags.
9797 int n = sz (a) - 1 ;
9898 flag = 1 ;
@@ -149,19 +149,19 @@ struct seg_pst {
149149 };
150150 int n;
151151 vector<node> t;
152- vector< int > root;
152+ vi root;
153153
154- void newnd () { t.push_back ({-1 , -1 , 0 }); }
155- void build (int n_, const vector<ll> &a) {
154+ void newnd () { t.pb ({-1 , -1 , 0 }); }
155+ void build (int n_, const vl &a) {
156156 // goal: build initial version.
157157 n = n_;
158158 t.clear ();
159159 root.clear ();
160160 newnd ();
161- root.push_back (0 );
161+ root.pb (0 );
162162 build (1 , n, root[0 ], a);
163163 }
164- void build (int l, int r, int v, const vector<ll> &a) {
164+ void build (int l, int r, int v, const vl &a) {
165165 // goal: build node v for range [l, r].
166166 if (l == r) {
167167 t[v].val = a[l];
@@ -179,7 +179,7 @@ struct seg_pst {
179179 void set (int p, ll val) {
180180 // goal: create new version with a[p] = val.
181181 newnd ();
182- root.push_back (sz (t) - 1 );
182+ root.pb (sz (t) - 1 );
183183 set (p, val, 1 , n, root[sz (root) - 2 ], root.back ());
184184 }
185185 void set (int p, ll val, int l, int r, int v1, int v2) {
@@ -246,13 +246,13 @@ struct seg_sparse {
246246 if (p <= mid) {
247247 if (t[v].l == -1 ) {
248248 t[v].l = sz (t);
249- t.push_back ({0 , -1 , -1 });
249+ t.pb ({0 , -1 , -1 });
250250 }
251251 add (p, x, t[v].l , nl, mid);
252252 } else {
253253 if (t[v].r == -1 ) {
254254 t[v].r = sz (t);
255- t.push_back ({0 , -1 , -1 });
255+ t.pb ({0 , -1 , -1 });
256256 }
257257 add (p, x, t[v].r , mid + 1 , nr);
258258 }
@@ -275,11 +275,11 @@ struct seg_sparse {
275275// usage: seg_2d st; st.build(a); st.set(x, y, v); st.query(x1, x2, y1, y2);
276276struct seg_2d { // 0-indexed
277277 int n;
278- vector<vector<ll>> t;
279- void build (const vector<vector<ll>> &a) {
278+ vvl t;
279+ void build (const vvl &a) {
280280 // goal: build 2D tree from initial grid.
281281 n = sz (a);
282- t.assign (2 * n, vector<ll> (2 * n, 0 ));
282+ t.assign (2 * n, vl (2 * n, 0 ));
283283 for (int i = 0 ; i < n; i++)
284284 for (int j = 0 ; j < n; j++)
285285 t[i + n][j + n] = a[i][j];
@@ -325,24 +325,24 @@ struct seg_2d { // 0-indexed
325325// usage: seg2d_comp st(n); st.mark_set(x, y); st.mark_qry(x1, x2, y1, y2); st.prep(); st.set(x, y, v); st.query(x1, x2, y1, y2);
326326struct seg2d_comp { // 0-indexed
327327 int n;
328- vector<vector<ll>> a;
329- vector<vector< int >> used;
328+ vvl a;
329+ vvi used;
330330 unordered_map<ll, ll> mp;
331331 seg2d_comp (int n) : n(n), a(2 * n), used(2 * n) {}
332332 void mark_set (int x, int y) {
333333 // goal: record y-coordinates that will be updated.
334- for (x += n; x >= 1 ; x >>= 1 ) used[x].push_back (y);
334+ for (x += n; x >= 1 ; x >>= 1 ) used[x].pb (y);
335335 }
336336 void mark_qry (int x1, int x2, int y1, int y2) {
337337 // goal: record y-coordinates needed for queries.
338338 for (x1 += n, x2 += n + 1 ; x1 < x2; x1 >>= 1 , x2 >>= 1 ) {
339339 if (x1 & 1 ) {
340- used[x1].push_back (y1);
341- used[x1++].push_back (y2);
340+ used[x1].pb (y1);
341+ used[x1++].pb (y2);
342342 }
343343 if (x2 & 1 ) {
344- used[--x2].push_back (y1);
345- used[x2].push_back (y2);
344+ used[--x2].pb (y1);
345+ used[x2].pb (y2);
346346 }
347347 }
348348 }
0 commit comments