Skip to content

Commit

Permalink
Create SegTree.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravUppoor authored Mar 7, 2021
1 parent 7b2d4a6 commit 73f065b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Data Structures/SegTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
struct Tree {
typedef int T;
static constexpr T unit = INT_MIN;
T f(T a, T b) { return max(a,b); } // any associative function
vector<T> s; int n;
Tree (int n = 0, T def = unit) : s(2*n, def), n(n) {}
void update(int pos, T val)
{
for(s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos*2], s[pos*2 + 1]);
}
T query(int b, int e) // query [b,e)
{
T ra = unit; T rb = unit;
for(b += n, e += n; b < e; b /= 2, e /= 2)
{
if(b%2) ra = f(ra, s[b++]);
if(e%2) rb = f(s[--e], rb);
}
return f(ra,rb);
}
};

void solve() {
int n;
cin >> n;
Tree st(n,0);
for(int i = 0; i < n; ++i)
{
int x; cin >> x;
st.update(i,x);
}
int m;
cin >> m;
for(int i = 0; i < m; ++i)
{
int l,r; cin >> l >> r;
cout << st.query(--l,r) << '\n';
}
}

0 comments on commit 73f065b

Please sign in to comment.