diff --git a/c++/segment_tree.cpp b/c++/segment_tree.cpp new file mode 100644 index 0000000..eb6a3a3 --- /dev/null +++ b/c++/segment_tree.cpp @@ -0,0 +1,116 @@ +#include // ॐ +#include // Har Har Mahadev +#include + +using namespace std; +using namespace __gnu_pbds; + +template +using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; + + +#define rep(i, zz, n) for (int i = zz; i < (n); ++i) +#define all(x) x.begin(), x.end() +#define pb push_back +#define F first +#define S second +#define ll long long +#define ld long double +#define pll pair +#define PI 3.14159265358979323846 +#define el '\n' +#define sp " " +#define vl vector +#define dbg(x) cout << #x << ": " << x << " "; + +const ll mod = 1e9 + 7; +const ll inf = 1e18; +const ll N = 1e6 + 5; + +#define debug(x) cout << #x << " "; _print(x); cout << '\n'; +#define _debug(a,b) cout << #a << " [ "; array_debug(a,b); cout << "]\n"; +template void array_debug(T *a, V n){ rep(i,0,n) cout << a[i] << ' ';} +template void _print(T t) {cout << t;} +template void _print(pair p) {cout << "{"; _print(p.ff); cout << ","; _print(p.ss); cout << "}";} +template void _print(vector v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(set v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(multiset v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";} +template void _print(map v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";} + +//greedy/brute force/DP/Graph/binary search/? + +template +struct SegmentTree{ + vector st; + void assign(vector &v1) { + int n = (v1.size()); + st = vector(4 * n + 1); + build(v1, 1, 0, n - 1); + } + T combine(T x, T y) { + // T z; + // return z; + return { x.sum + y.sum }; // check which opeartion is to be performed + } + void build(vector& v1, int v, int tl, int tr) { + if (tl == tr) { + st[v].sum = v1[tl]; // check + } + else{ + int mid = (tl + tr) / 2; + build(v1, 2 * v, tl, mid); + build(v1, 2 * v + 1, mid + 1, tr); + st[v] = combine(st[2 * v], st[2 * v + 1]); + } + } + // tl, tr : the current segment/range whose node/vertex is v + // l, r : the segment/range on which the 'range query' is to be performed + T query(int v, int tl, int tr, int l, int r) { + if (tr < l || r < tl) { + return { 0 }; // check which opeartion is to be performed + } + else if (tl == l && tr == r) { + return st[v]; + } + int mid = (tl + tr) / 2; + return combine(query(2 * v, tl, mid, l, min(r, mid)), query(2 * v + 1, mid + 1, tr, max(l, mid + 1), r)); + } + void update_one(int v, int tl, int tr, int pos, int newVal) { + if (tl == tr) { + st[v].sum = newVal; + // check whether the newVal is added or updated + return; + } + int mid = (tl + tr) / 2; + if (pos <= mid) { + update_one(2 * v, tl, mid, pos, newVal); + } + else{ + update_one(2 * v + 1, mid + 1, tr, pos, newVal); + } + st[v] = combine(st[2 * v], st[2 * v + 1]); + } +}; +struct Node{ + int sum; +}; +SegmentTree seg; + + +void __roOTs(){ + // main code here + +} +signed main(){ + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + int tt = 1; cin >> tt; + for (int i = 1; i <= tt; i++){ + // cout << "Case #" << i << ": "; + __roOTs(); cout << '\n'; + + } + return 0; +} \ No newline at end of file