Skip to content

Commit

Permalink
GST January SRM
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Jan 20, 2018
1 parent 93d1dd9 commit fbf050f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CP/GST January SRM/problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for _ in range(int(input())):
r,a,b,c,d = map(int,raw_input().split())
if(r==a or r==b or r==c or r==d):
print("YES")
else :
print("NO")
8 changes: 8 additions & 0 deletions CP/GST January SRM/problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
for kandu in xrange(int(raw_input())):
sum = 0
n = int(input())
for i in range(n):
a = map(int, raw_input().split())
a = sorted(a)
sum+=abs(a[n-1]-a[0])
print(sum)
2 changes: 2 additions & 0 deletions CP/January Challenge 2018/maxscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for _ in range(int(input())):

5 changes: 0 additions & 5 deletions tree/morris_traversal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"""Morris Traversal of a Binary Tree.
Video
-----
* https://youtu.be/wGXB9OWhPTg
Analysis
--------
Expand Down
90 changes: 63 additions & 27 deletions tree/segmenttreesum.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,13 @@ def update_value_util(segment_tree, low, high, diff, index, pos):
update_value_util(segment_tree, low, mid, diff, index, 2*pos + 1)
update_value_util(segment_tree, mid + 1, high, diff, index, 2*pos + 2)

def updateRange(int n, int us, int ue, int diff):
updateRangeUtil(0, 0, n-1, us, ue, diff)
def updateSegmentTreeRangeLazy( input, segmentTree, lazy, startRange, endRange, delta):
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, 0, input.length - 1, 0)

lazy = [0]*1000

def updateRangeUtil( si, ss, se, us, ue, diff):
if (lazy[si] != 0):
tree[si] += (se-ss+1)*lazy[si]
if (ss != se):
lazy[si*2 + 1] += lazy[si]
lazy[si*2 + 2] += lazy[si]
def rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, leni):
return rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, 0, leni - 1, 0)

lazy[si] = 0

if (ss>se || ss>ue || se<us)
return

if (ss>=us && se<=ue):
tree[si] += (se-ss+1)*diff

if (ss != se):
lazy[si*2 + 1] += diff
lazy[si*2 + 2] += diff
return

int mid = (ss+se)/2
updateRangeUtil(si*2+1, ss, mid, us, ue, diff)
updateRangeUtil(si*2+2, mid+1, se, us, ue, diff)

tree[si] = tree[si*2+1] + tree[si*2+2]

def next_power_of_2(n):
if n == 0:
Expand All @@ -85,6 +62,57 @@ def next_power_of_2(n):
n &= (n - 1)
return n << 1

def updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, low, high, pos):
if(low > high):
return

if (lazy[pos] != 0):
segmentTree[pos] += lazy[pos]
if (low != high):
lazy[2 * pos + 1] += lazy[pos]
lazy[2 * pos + 2] += lazy[pos]
lazy[pos] = 0

if(startRange > high || endRange < low):
return

if(startRange <= low && endRange >= high):
segmentTree[pos] += delta
if(low != high):
lazy[2*pos + 1] += delta
lazy[2*pos + 2] += delta
return

int mid = (low + high)/2
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange,delta, low, mid, 2*pos+1)
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange,delta, mid+1, high, 2*pos+2)
segmentTree[pos] = Math.min(segmentTree[2*pos + 1], segmentTree[2*pos + 2])

def rangeMinimumQueryLazy(int segmentTree, int lazy[], int qlow, int qhigh, low, int high, int pos):
if(low > high):
return Integer.MAX_VALUE

if (lazy[pos] != 0)
segmentTree[pos] += lazy[pos]
if (low != high):
lazy[2 * pos + 1] += lazy[pos]
lazy[2 * pos + 2] += lazy[pos]
lazy[pos] = 0

if(qlow > high || qhigh < low):
return Integer.MAX_VALUE

if(qlow <= low && qhigh >= high):
return segmentTree[pos]

int mid = (low+high)/2
return Math.min(rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh,
low, mid, 2 * pos + 1),
rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh,
mid + 1, high, 2 * pos + 2))



if __name__ == '__main__':
input = [1,3,5,7,9,11]
segment_tree = create_segment_tree(input)
Expand All @@ -97,3 +125,11 @@ def next_power_of_2(n):
print(sum_range_query(segment_tree, 2, 5, len(input)))
print(sum_range_query(segment_tree, 1, 3, len(input)))
updateRange(len(input), 1, 5, 10)

#lazy propagation example
input1 = [-1,2,4,1,7,1,3,2]
segTree1 = create_segment_tree(input)
lazy1 = []
updateSegmentTreeRangeLazy(input1, segTree1, lazy1, 0, 3, 1)
updateSegmentTreeRangeLazy(input1, segTree1, lazy1, 0, 0, 2)
print(1 == rangeMinimumQueryLazy(segTree1, lazy1, 3, 5, input1.length))

0 comments on commit fbf050f

Please sign in to comment.