Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save kartikkukreja/13325ae1b1859566d083 to your computer and use it in GitHub Desktop.

Select an option

Save kartikkukreja/13325ae1b1859566d083 to your computer and use it in GitHub Desktop.
FLIPCOIN SegmentTreeNode
struct SegmentTreeNode {
int start, end; // this node is responsible for the segment [start...end]
int count;
bool pendingUpdate;
SegmentTreeNode() : count(0), pendingUpdate(false) {}
void assignLeaf(bool value) {}
void merge(SegmentTreeNode& left, SegmentTreeNode& right) {
count = (left.pendingUpdate ? (left.end - left.start + 1 - left.count) : left.count)
+ (right.pendingUpdate ? (right.end - right.start + 1 - right.count) : right.count);
}
int query() {
return count;
}
bool hasPendingUpdate() {
return pendingUpdate;
}
void applyPendingUpdate() {
count = (end - start + 1) - count;
pendingUpdate = false;
}
void addUpdate(bool value) {
pendingUpdate = !pendingUpdate;
}
bool getPendingUpdate() {
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment