Last active
August 29, 2015 14:13
-
-
Save kartikkukreja/cb3c3c2b36f9427d7eef to your computer and use it in GitHub Desktop.
HORRIBLE SegmentTreeNode
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
struct SegmentTreeNode { | |
int start, end; // this node is responsible for the segment [start...end] | |
ll total, pendingUpdate; | |
SegmentTreeNode() : total(0), pendingUpdate(0) {} | |
void assignLeaf(ll value) { | |
total = value; | |
} | |
void merge(SegmentTreeNode& left, SegmentTreeNode& right) { | |
total = left.total + right.total; | |
if (left.pendingUpdate > 0) | |
total += left.pendingUpdate * (left.end - left.start + 1); | |
if (right.pendingUpdate > 0) | |
total += right.pendingUpdate * (right.end - right.start + 1); | |
} | |
ll query() { | |
return total; | |
} | |
bool hasPendingUpdate() { | |
return pendingUpdate != 0; | |
} | |
void applyPendingUpdate() { | |
total += (end - start + 1) * pendingUpdate; | |
pendingUpdate = 0; | |
} | |
void addUpdate(ll value) { | |
pendingUpdate += value; | |
} | |
ll getPendingUpdate() { | |
return pendingUpdate; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment