Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikkukreja/cb3c3c2b36f9427d7eef to your computer and use it in GitHub Desktop.
Save kartikkukreja/cb3c3c2b36f9427d7eef to your computer and use it in GitHub Desktop.
HORRIBLE SegmentTreeNode
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