Advertisement
Heruberuto

Untitled

Feb 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. import random
  2. from argparse import _ActionsContainer
  3. from math import floor
  4.  
  5. from numpy.testing import rand
  6.  
  7. coord = []
  8. weight = []
  9.  
  10. for p in range(1000000):
  11.     coord.append(getRand)
  12.     weight.append(int(random.random()))
  13. """
  14. points = int(input())
  15. for i in range(points):
  16.    c, w = map(int,input().split())
  17.    coord.append(c)
  18.    weight.append(w)
  19. print("Points added, now how many requests?")
  20. """
  21. leftBound = min(coord)
  22. rightBound = max(coord) + 1
  23. offset = - leftBound
  24. P = [-1] * (rightBound - leftBound)
  25. W = [0] * (rightBound - leftBound)
  26.  
  27.  
  28. class SegmentTree:
  29.     def sol(self, s, e):
  30.         if self.start == self.end:
  31.             return self.w, self.p
  32.         elif s == self.start and e == self.end:
  33.             return self.w, self.p
  34.         elif s > self.center:
  35.             return self.right.sol(s, e)
  36.         elif e <= self.center:
  37.             return self.left.sol(s, e)
  38.         else:
  39.             # start is before center and end is after center
  40.             l_w, l_p = self.left.sol(s, self.center)
  41.             r_w, r_p = self.right.sol(self.center + 1, e)
  42.             if l_w >= r_w:
  43.                 return l_w, l_p
  44.             else:
  45.                 return r_w, r_p
  46.  
  47.     def __init__(self, s, e):
  48.         global offset, W, P
  49.         self.start = s
  50.         self.end = e
  51.         self.center = floor((s + e) / 2)
  52.         self.left = None
  53.         self.right = None
  54.  
  55.         if s >= e:
  56.             self.p = P[s + offset]
  57.             self.w = W[s + offset]
  58.         else:
  59.             self.left = SegmentTree(s, self.center)
  60.             self.right = SegmentTree(self.center + 1, e)
  61.             if self.right.w > self.left.w:
  62.                 self.w = self.right.w
  63.                 self.p = self.right.p
  64.             else:
  65.                 self.w = self.left.w
  66.                 self.p = self.left.p
  67.  
  68.  
  69. for i in range(len(coord)):
  70.     j = coord[i] + offset
  71.     if weight[i] > W[j] or P[j] == -1:
  72.         W[j] = weight[i]
  73.         P[j] = i
  74.  
  75. rightBound -= 1
  76. data = SegmentTree(leftBound, rightBound)
  77.  
  78. reqs = int(input())
  79.  
  80. for i in range(reqs):
  81.     start, end = map(int, input().split())
  82.     if start > rightBound or end < leftBound: "No intersection with P"
  83.     end = min(end, rightBound)
  84.     start = max(start, leftBound)
  85.     w, p = data.sol(start, end)
  86.     print("Best point is p_" + str(p) + " with weight " + str(w))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement