diff --git a/lua/custom/snippets/edhliicpp.lua b/lua/custom/snippets/edhliicpp.lua index 997bf4c7..a701a737 100644 --- a/lua/custom/snippets/edhliicpp.lua +++ b/lua/custom/snippets/edhliicpp.lua @@ -25,6 +25,50 @@ ls.add_snippets('cpp', { '}', }, }), + s('segtree', { + t { + 'struct SegTree {', + ' int n;', + ' vector tree;', + '', + ' inline int combine(int a, int b) {', + ' // TODO: chỉnh sửa hàm combine cho phù hợp (vd: min(a, b), max(a, b), a + b, ...)', + ' return a + b;', + ' }', + '', + ' SegTree(const vector &arr) {', + ' n = arr.size();', + ' tree.resize(n << 1);', + ' for (int i = 0; i < n; ++i)', + ' tree[i + n] = arr[i];', + ' for (int i = n - 1; i > 0; --i)', + ' tree[i] = combine(tree[i << 1], tree[i << 1 | 1]);', + ' }', + '', + ' inline void update(int pos, int val) {', + ' pos += n;', + ' tree[pos] = val;', + ' for (pos >>= 1; pos; pos >>= 1)', + ' tree[pos] = combine(tree[pos << 1], tree[pos << 1 | 1]);', + ' }', + '', + ' inline int query(int l, int r) {', + ' int resL = 0, resR = 0; // TODO: chỉnh phần tử đơn vị phù hợp', + ' l += n;', + ' r += n;', + ' while (l < r) {', + ' if (l & 1)', + ' resL = combine(resL, tree[l++]);', + ' if (r & 1)', + ' resR = combine(tree[--r], resR);', + ' l >>= 1;', + ' r >>= 1;', + ' }', + ' return combine(resL, resR);', + ' }', + '};', + }, + }), s('binpow', { t { 'll binpow(ll a, ll b, ll c) {',