def localmax(x,y,xval,lookwithin=1): """finds the local maximum within +/- lookwithin of the xval""" l,r = bl(x,xval-lookwithin),br(x,xval+lookwithin) try: return max(y[l:r]) except ValueError: raise ValueError("No maximum value found in range, perhaps wrong window region?")
def estimatedem(x,y,em,simmin,simmax,lookwithin=1): """estimates the exact mass of a peak""" l,r = bl(x,simmin-lookwithin),br(x,simmax+lookwithin) # narrow range to that of the isotope pattern locmax = max(y[l:r]) # find local max in that range for ind,val in enumerate(y): if val == locmax: # if the y-value equals the local max if ind >= l and ind <= r: # and if the index is in the range (avoids false locations) return x[ind] difleft = abs(em-simmin) difright = abs(em-simmax) return '>%.1f' %max(difleft,difright) # if no match is found, return maximum difference
def fallingSquares(self, positions: List[List[int]]) -> List[int]: ans, xs, hs = [], [0], [0] for (x, h) in positions: i, j = br(xs, x), bl(xs, x+h) H = max(hs[max(i-1, 0):j], default = 0) + h xs[i:j] = [x, x + h] hs[i:j] = [H, hs[j - 1]] ans.append(H) if len(ans) > 1: ans[-1] = max(ans[-1], ans[-2]) return ans
def removeRange(self, left, right): ivs = self.ivs ilo, ihi = bl(ivs, left), br(ivs, right) new = [] if ilo % 2 == 1: ilo -= 1 new += [ivs[ilo], left] if ihi % 2 == 1: new += [right, ivs[ihi]] ihi += 1 self.ivs = ivs[:ilo] + new + ivs[ihi:]
def addRange(self, left, right): # Get the left most index to insert left # And the right most index to insert right # This will include everyrange inside [left,right) # we replace everything form i:j with new range # If i is starting a new range, then we can insert left at i # if j is starting a new range, it is the closing range of i # if j in not starting a new range, use exiting closing range # if i is not starting a new range, but j is, then update range at j to right i, j = bl(self._X, left), br(self._X, right) self._X[i:j] = [left]*(i%2 == 0) + [right]*(j%2 == 0)
def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ idx1 = bl(nums, target) idx2 = br(nums, target) print idx1, idx2 idx1 = idx1 if idx1 < len(nums) and nums[idx1] == target else -1 idx2 = idx2 - 1 if idx2 and nums[idx2 - 1] == target else -1 return [idx1, idx2]
def get(self, index): nIndices = self.nIndices pos = br(self.indices, index) if pos == 0: return None pos = nIndices - 1 if pos > nIndices else pos - 1 offset = self.offsets[pos] if offset == 0: return None return self.values[offset:self.bounds[pos]].decode(UTF8)
def retrieve(self, s0: str, e0: str, gra: str) -> List[int]: s, e = parse(s0, gra), parse(e0, gra, roundUp=True) left = bl(self.logs, (s, 0)) right = br(self.logs, (e, float("inf"))) ans = [x[1] for x in self.logs[left:right]] return ans # Your LogSystem object will be instantiated and called as such: # obj = LogSystem() # obj.put(id,timestamp) # param_2 = obj.retrieve(s,e,gra)
def solve(a, n): ans = 0 ab = [] ef = [] for i in range(n): for j in range(n): for k in range(n): ab.append(a[i]*a[j]+a[k]) if a[k] != 0: ef.append((a[i]+a[j])*a[k]) # print(ab) # print(ef) ab.sort() ef.sort() i = 0 while i < n**3: lo = bl(ab, ab[i]) hi = br(ab, ab[i]) ans += (hi-lo)*(br(ef, ab[i])-bl(ef, ab[i])) i = hi return ans
def main(): read = stdin.readline l, n, q = map(int, read().split()) s = list(map(int, read().split())) d = bytes(map(int, read().split())) for i in range(n): s[i] -= 1 l -= 1 left = [] right = [] for i in range(n): if d[i]: right.append(s[i]) right.append(-2 * l + s[i]) right.append(2 * l + s[i]) else: right.append(-s[i]) right.append(-2 * l - s[i]) right.append(2 * l - s[i]) right.sort() #; left.sort () for i in range(len(right) - 1, -1, -1): left.append(-right[i]) setleft = set(left) for q_ in range(q): t, p = map(int, read().split()) t %= 2 * l t_sl = t in setleft tm = bl(left, 1 + t) + bl(right, 1 - t) lo = -1 hi = l while lo < hi: mid = (lo + hi) // 2 if br(left, mid + t) + br(right, mid - t) - tm + t_sl >= p: hi = mid else: lo = mid + 1 print(hi + 1)
def main(): read = stdin.readline n, m, q = map(int, read().split()) lev = [] for p in range(n): l = list(map(int, read().split())) lev.append(l) for q_ in range(q): strength = list(map(int, read().split())) ans = br(lev[0], strength[0]) - 1 for i in range(1, n): while lev[i][ans] > strength[i]: ans -= 1 #if not ans: break print(ans + 1)
def read_into_buckets(en_ids_path, fr_ids_path, print_every=100000): """ Generate dataset by reading data into buckets. """ data_set = map(lambda x: [], _BUCKETS) counter = 0 en_buckets, fr_buckets = zip(*_BUCKETS) with open(en_ids_path, 'r') as en: with open(fr_ids_path, 'r') as fr: en_line, fr_line = en.readline(), fr.readline() while en_line and fr_line: counter += 1 if counter % print_every == 0: print(" reading data line %d" % counter) sys.stdout.flush() en_sequence, fr_sequence = get_int_seq(en_line), get_int_seq( fr_line) en_seq_len, fr_seq_len = len(en_sequence), len(fr_sequence) if 0 < en_seq_len < _MAX_EN and 0 < fr_seq_len < _MAX_FR: b_id = max(br(en_buckets, en_seq_len), br(fr_buckets, fr_seq_len)) data_set[b_id].append([en_sequence, fr_sequence]) en_line, fr_line = en.readline(), fr.readline() return data_set
def makeArrayIncreasing(self, arr1: List[int], arr2: List[int]) -> int: dp = {-1: 0} # {val: ops} arr2.sort() for x in arr1: curr = defaultdict(lambda: float('inf')) for val, ops in dp.items(): if x > val: curr[x] = min(curr[x], ops) idx = br(arr2, val) if idx < len(arr2): v2 = arr2[idx] curr[v2] = min(curr[v2], ops + 1) dp = curr if dp: return min(dp.values()) return -1
def removeRange(self, left, right): """ :type left: int :type right: int :rtype: void """ l, r = bl(self._X, left), br(self._X, right) self._X[l:r] = [left] * (l % 2 == 1) + [right] * (r % 2 == 1) # Your RangeModule object will be instantiated and called as such: # obj = RangeModule() # obj.addRange(left,right) # param_2 = obj.queryRange(left,right) # obj.removeRange(left,right)
def solve(c, k): for i in range(1, len(c)): c[i] += c[i-1] res = 0 for i in range(len(c)): l = bl(c, c[i]+k, lo=i+1) r = br(c, c[i]+k) if c[i] == k: res += 1 if l != r: res += r - l print(res)
def main(): read = stdin.readline n, m, q = map(int, read().split()) lev = [] for p in range(n): l = list(map(int, read().split())) lev.append(l) for q_ in range(q): strength = list(map(int, read().split())) lvl = [] ans = maxsize for i in range(n): l = br(lev[i], strength[i]) if l < ans: ans = l print(ans)
def addRange(self, left, right): """ :type left: int :type right: int :rtype: void """ ivs = self.ivs ilo, ihi = bl(ivs, left), br(ivs, right) if ilo%2 == 1: ilo -= 1 left = ivs[ilo] if ihi%2 == 1: right = ivs[ihi] ihi += 1 self.ivs = ivs[:ilo] + [left, right] + ivs[ihi:]
def solution(pN): s = "" while 1: cnt1, cnt2 = 0, 0 l, u = map(int, input().split()) if l == u == -1: break ans = pN[bl(pN, l) : br(pN, u)] cnt1 = len(ans) for i in ans: if i % 4 == 1: cnt2 += 1 if l <= 2 and u >= 2: cnt2 += 1 # print(l, u, cnt1, cnt2) s += str(l) + " " + str(u) + " " + str(cnt1) + " " + str(cnt2) + "\n"
def removeRange(self, left, right): """ :type left: int :type right: int :rtype: void """ ivs = self.ivs ilo, ihi = bl(ivs, left), br(ivs, right) new = [] if ilo%2 == 1: ilo -= 1 new += [ivs[ilo], left] if ihi%2 == 1: new += [right, ivs[ihi]] ihi += 1 self.ivs = ivs[:ilo] + new + ivs[ihi:]
def book(self, start, end): """ :type start: int :type end: int :rtype: int """ from bisect import bisect_left as bl, bisect_right as br, insort_left as il from heapq import heappop, heappush i, j = bl(self.es, start), br(self.ss, end) il(self.ss, start, i, j) il(self.es, end, i, j) heap = [] for pp in range(i, j + 1): while heap and heap[0] <= self.ss[pp]: heappop(heap) heappush(heap, self.es[pp]) self.k = max(self.k, len(heap)) return self.k
def main(): read = stdin.readline n = int(read()) d = dict() i = 1 while i <= n: s = read().rstrip() if s in d: d[s].append(i) else: d[s] = [i] i += 1 q = int(read()) while q: l, r, s = read().split() ans = 0 if s in d: ans = (br(d[s], int(r)) - bl(d[s], int(l))) print(ans) q -= 1
def main(): read = stdin.readline ql = [] q = int(read()) rmax = 0 for q_ in range(q): l, r = map(int, read().split()) if r > rmax: rmax = r ql.append((l, r)) trian = [1] maxrt = min(int((rmax * 2)**.5 + 2), 1414215) for i in range(2, maxrt): trian.append(trian[-1] + i) #print (trian) for l, r in ql: """uix = br (trian, r) lo = 0; rct = uix while lo < rct: md = (lo + rct) // 2 if trian [0] + trian [md] > r: rct = md else: lo = md + 1 lo = 0; lct = uix while lo < lct: md = (lo + lct) // 2 if trian [0] + trian [md] >= l: lct = md else: lo = md + 1""" rct = br(trian, r - 1) uix = rct + 1 lct = bl(trian, l - 1) ct = rct - lct #print (rct, lct, ct) for i in range(1, uix): if trian[i] * 2 > r: break while rct > i and trian[i] + trian[rct] > r: rct -= 1 if lct > i: while lct > i and trian[i] + trian[lct - 1] >= l: lct -= 1 else: lct = i #if i > 809240: print (rct, lct, l, r, uix) ct += rct - lct + 1 print(ct)
def minWindowSubsequence(S, T): chars = set(T) indices = defaultdict(list) for idx, char in enumerate(S): if char in chars: indices[char].append(idx) res = "" for idx in indices[T[0]]: start = idx for i in range(1, len(T)): possibles = indices[T[i]] pos = br(possibles, start) if pos == len(possibles): break start = possibles[pos] else: if not res or start - idx + 1 < len(res): res = S[idx:start + 1] return res
def main(): read = stdin.readline ql = [] q = int(read()) rmax = 0 for q_ in range(q): l, r = map(int, read().split()) if r > rmax: rmax = r ql.append((l, r)) trian = [1] maxrt = min(int((rmax * 2)**.5 + 2), 1414215) for i in range(2, maxrt): trian.append(trian[-1] + i) #print (trian) for l, r in ql: ct = 0 uix = br(trian, r) for i in range(uix): if trian[i] * 2 > r: break lo = i rct = uix while lo < rct: md = (lo + rct) // 2 if trian[i] + trian[md] <= r: lo = md + 1 else: rct = md rct -= 1 lo = i lct = uix while lo < lct: md = (lo + lct) // 2 if trian[i] + trian[md] >= l: lct = md else: lo = md + 1 #if i > 809240: print (rct, lct, l, r, uix) ct += rct - lct + 1 print(ct)
def main(): read = stdin.readline t = int(read()) for t_ in range(t): n = int(read()) size = [] eat = [] for n_ in range(n): s, e = map(int, read().split()) size.append(s) eat.append(e) size.sort() eat.sort(reverse=True) eat = list(zip(eat, range(n - 1, -1, -1))) eatn = [eat[0]] for i in range(1, n): if eat[i][0] != eat[i - 1][0]: eatn.append(eat[i]) ans = 1 for e, n_ in eatn: ix = br(size, e) mx = n_ - ix + 1 if mx >= ans: ans = mx if not ix: break print(ans)
def gen_rand_codon(self): rnd = ri(0, self.total - 1) index = br(self.totals, rnd) return self.counts_list[index][0]
def removeRange(self, left, right): i, j = bl(self.ds, left), br(self.ds, right) self.ds[i:j] = [left] * (i % 2 == 1) + [right] * (j % 2 == 1)
def queryRange(self, left, right): i, j = br(self.ds, left), bl(self.ds, right) # print(i, j) return i == j and i % 2 == 1
def addRange(self, left, right): i, j = bl(self.ds, left), br(self.ds, right) # print(i, j) self.ds[i:j] = [left] * (i % 2 == 0) + [right] * (j % 2 == 0)
def addRange(self, left: int, right: int) -> None: i, j = bl(self.data, left), br(self.data, right) self.data[i:j] = [left] * (i % 2 == 0) + [right] * (j % 2 == 0)
def queryRange(self, left: int, right: int) -> bool: i, j = br(self.data, left), bl(self.data, right) return i == j and i % 2 == 1
def bs(num): if br(nums, num) == bl(nums, num): return 0 return 1
print(2 * 3**2) v = 1 w = 1 # while 1: # if v == w == 1: # print("yes!") # break # print(str(v) + " " + str(w)) # math.sqrt(v) # a = [1, 2, 3] # b = [1] + [2] + [3] # print(a) # print(b) print(int(5**0.5) + 1) from bisect import bisect_left as bl from bisect import bisect_right as br a = [2, 3, 5, 7, 11] b = a[bl(a, 3):br(a, 11)] print(b)
def trimspectrum(x,y,left,right): """trims a spectrum to the left and right bounds""" l,r = bl(x,left),br(x,right) # find indicies return x[l:r],y[l:r] # trim spectrum
from bisect import bisect_left as bl, bisect_right as br n, k = map(int, raw_input().split());a=sorted(map(int, raw_input().split())) print sum([br(a,a[i]+k)-bl(a,a[i]+k) for i in xrange(n)])
def solve1(c, x, y): res = c[br(c, y)-1] return res if res >= x else -1