Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from argparse import _ActionsContainer
- from math import floor
- from numpy.testing import rand
- coord = []
- weight = []
- for p in range(1000000):
- coord.append(getRand)
- weight.append(int(random.random()))
- """
- points = int(input())
- for i in range(points):
- c, w = map(int,input().split())
- coord.append(c)
- weight.append(w)
- print("Points added, now how many requests?")
- """
- leftBound = min(coord)
- rightBound = max(coord) + 1
- offset = - leftBound
- P = [-1] * (rightBound - leftBound)
- W = [0] * (rightBound - leftBound)
- class SegmentTree:
- def sol(self, s, e):
- if self.start == self.end:
- return self.w, self.p
- elif s == self.start and e == self.end:
- return self.w, self.p
- elif s > self.center:
- return self.right.sol(s, e)
- elif e <= self.center:
- return self.left.sol(s, e)
- else:
- # start is before center and end is after center
- l_w, l_p = self.left.sol(s, self.center)
- r_w, r_p = self.right.sol(self.center + 1, e)
- if l_w >= r_w:
- return l_w, l_p
- else:
- return r_w, r_p
- def __init__(self, s, e):
- global offset, W, P
- self.start = s
- self.end = e
- self.center = floor((s + e) / 2)
- self.left = None
- self.right = None
- if s >= e:
- self.p = P[s + offset]
- self.w = W[s + offset]
- else:
- self.left = SegmentTree(s, self.center)
- self.right = SegmentTree(self.center + 1, e)
- if self.right.w > self.left.w:
- self.w = self.right.w
- self.p = self.right.p
- else:
- self.w = self.left.w
- self.p = self.left.p
- for i in range(len(coord)):
- j = coord[i] + offset
- if weight[i] > W[j] or P[j] == -1:
- W[j] = weight[i]
- P[j] = i
- rightBound -= 1
- data = SegmentTree(leftBound, rightBound)
- reqs = int(input())
- for i in range(reqs):
- start, end = map(int, input().split())
- if start > rightBound or end < leftBound: "No intersection with P"
- end = min(end, rightBound)
- start = max(start, leftBound)
- w, p = data.sol(start, end)
- print("Best point is p_" + str(p) + " with weight " + str(w))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement