Created
February 24, 2017 05:46
-
-
Save asi1024/fe04fcefcc5581d60da6150a621d549e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
class SegmentTree { | |
const int n; | |
vector<T> data; | |
public: | |
SegmentTree(const int s) : n(1 << s), data(n * 2, T()) {} | |
void update(int p, T val) { | |
data[p += n] = val; | |
while (p /= 2) { | |
data[p] = merge(data[p * 2], data[p * 2 + 1]); | |
} | |
} | |
T query(int l, int r) const { | |
l += n; r += n; | |
T res1 = T(), res2 = T(); | |
while (l != r) { | |
if (l % 2) res1 = merge(res1, data[l++]); | |
if (r % 2) res2 = merge(data[--r], res2); | |
l /= 2; r /= 2; | |
} | |
return merge(res1, res2); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment