Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / HORRIBLE query() and update().cpp
Last active August 29, 2015 14:13
HORRIBLE query() and update()
SegmentTreeNode query(int stIndex, int start, int end) {
if (nodes[stIndex].start == start && nodes[stIndex].end == end) {
SegmentTreeNode result = nodes[stIndex];
if (result.hasPendingUpdate())
result.applyPendingUpdate();
return result;
}
int mid = (nodes[stIndex].start + nodes[stIndex].end) >> 1,
leftChildIndex = stIndex << 1,
@kartikkukreja
kartikkukreja / GSS4 SegmentTreeNode.cpp
Last active August 29, 2015 14:13
GSS4 SegmentTreeNode
struct SegmentTreeNode {
int start, end; // this node is responsible for the segment [start...end]
ll total;
bool pendingUpdate;
SegmentTreeNode() : total(0), pendingUpdate(false) {}
void assignLeaf(ll value) {
total = value;
}
@kartikkukreja
kartikkukreja / GSS4 update().cpp
Last active August 29, 2015 14:13
GSS4 update()
void update(int stIndex, int start, int end, UpdateType value) {
if (nodes[stIndex].start == start && nodes[stIndex].end == end) {
lazyPropagatePendingUpdateToSubtree(stIndex, value);
return;
}
int mid = (nodes[stIndex].start + nodes[stIndex].end) >> 1,
leftChildIndex = stIndex << 1,
rightChildIndex = leftChildIndex + 1;
@kartikkukreja
kartikkukreja / Range update example node.cpp
Last active August 29, 2015 14:13
Range update example node
struct SegmentTreeNode {
int start, end; // this node is responsible for the segment [start...end]
double total;
void assignLeaf(double value) {
total = value;
}
void merge(SegmentTreeNode& left, SegmentTreeNode& right) {
total = left.total + right.total;
@kartikkukreja
kartikkukreja / Range update.cpp
Last active August 29, 2015 14:13
Range update
void update(int stIndex, int start, int end, UpdateType value) {
if (start == end) {
nodes[stIndex].applyUpdate(value);
return;
}
int mid = (nodes[stIndex].start + nodes[stIndex].end) / 2,
leftChildIndex = 2 * stIndex,
rightChildIndex = leftChildIndex + 1;
@kartikkukreja
kartikkukreja / Partial Segment Tree Template.cpp
Last active August 29, 2015 14:13
Partial Segment Tree Template
template<class InputType, class UpdateType, class OutputType>
class SegmentTree {
SegmentTreeNode* nodes;
int N;
public:
SegmentTree(InputType arr[], int N) {
this->N = N;
nodes = new SegmentTreeNode[getSegmentTreeSize(N)];
buildTree(arr, 1, 0, N-1);
@kartikkukreja
kartikkukreja / Partial Segment Tree Node.cpp
Last active August 29, 2015 14:13
Partial Segment Tree Node
struct SegmentTreeNode {
int start, end; // this node is responsible for the segment [start...end]
// variables to store aggregate statistics and
// any other information required to merge these
// aggregate statistics to form parent nodes
void assignLeaf(InputType value) {
// InputType is the type of input array element
// Given the value of an input array element,
@kartikkukreja
kartikkukreja / Test Oracle.py
Last active August 29, 2015 14:11
Test Oracle
import random, os
# define constants
correct_program = "correct.exe"
incorrect_program = "incorrect.exe"
input_file = "input.txt"
correct_output = "correct_output.txt"
incorrect_output = "incorrect_output.txt"
num_testcases = 10
@kartikkukreja
kartikkukreja / KGSS segment tree node.cpp
Last active August 29, 2015 14:09
KGSS segment tree node
struct SegmentTreeNode {
int maxNum, secondMaxNum;
void assignLeaf(int num) {
maxNum = num;
secondMaxNum = -1;
}
void merge(SegmentTreeNode& left, SegmentTreeNode& right) {
maxNum = max(left.maxNum, right.maxNum);
@kartikkukreja
kartikkukreja / BRCKTS segment tree node.cpp
Last active August 29, 2015 14:09
BRCKTS segment tree node
struct SegmentTreeNode {
int unmatchedOpenParans, unmatchedClosedParans;
void assignLeaf(char paranthesis) {
if (paranthesis == '(')
unmatchedOpenParans = 1, unmatchedClosedParans = 0;
else
unmatchedOpenParans = 0, unmatchedClosedParans = 1;
}