segment-tree

Perimeter of union of N rectangles

戏子无情 提交于 2020-08-09 09:19:10
问题 I want to know the efficient way to solve this problem: Given N rectangles that given a top-left and bottom-right corner, please find the perimeter of union of N rectangles. I only have O(N^2) algorithm and it's too slow, so please find more efficient algorithm. You can assume that coordinate value is positive integer and less than 100000. EDIT: For example, in this case, the perimeter is 30. An O(n^2) algorithm: for x=0 to maxx for i=0 to N if lowx[i] = x for j=lowy[i] to highy[i] d[j]++ if

Is it possible to query number of distinct integers in a range in O(lg N)?

落花浮王杯 提交于 2020-06-22 06:14:08
问题 I have read through some tutorials about two common data structure which can achieve range update and query in O(lg N): Segment tree and Binary Indexed Tree (BIT / Fenwick Tree). Most of the examples I have found is about some associative and commutative operation like "Sum of integers in a range", "XOR integers in a range", etc. I wonder if these two data structures (or any other data structures / algorithm, please propose) can achieve the below query in O(lg N)? (If no, how about O(sqrt N))

How can we calculate weighted cumulative sum of squares with prefix range updates by one?

不问归期 提交于 2020-05-10 06:45:32
问题 Given an array A with n elements which starts with all 0 s and another array W also with n elements (all greater than 0 ), we want to perform the following operation repeatedly; For a given k, increment A[0], A[1], .... A[k] each by 1, and report the value of A[0]^2 * W[0] + A[1]^2 * W[1] + ... + A[n-1]^2 * W[n-1] . Looking for an O(log n) solution (per query), or faster. 来源: https://stackoverflow.com/questions/61122170/how-can-we-calculate-weighted-cumulative-sum-of-squares-with-prefix-range

How can we calculate weighted cumulative sum of squares with prefix range updates by one?

被刻印的时光 ゝ 提交于 2020-05-10 06:45:11
问题 Given an array A with n elements which starts with all 0 s and another array W also with n elements (all greater than 0 ), we want to perform the following operation repeatedly; For a given k, increment A[0], A[1], .... A[k] each by 1, and report the value of A[0]^2 * W[0] + A[1]^2 * W[1] + ... + A[n-1]^2 * W[n-1] . Looking for an O(log n) solution (per query), or faster. 来源: https://stackoverflow.com/questions/61122170/how-can-we-calculate-weighted-cumulative-sum-of-squares-with-prefix-range

Range update and querying in a 2D matrix

一世执手 提交于 2020-01-31 00:06:10
问题 I don't have a scenario, but here goes the problem. This is one is just driving me crazy. There is a nxn boolean matrix initially all elements are 0, n <= 10^6 and given as input. Next there will be up to 10^5 queries. Each query can be either set all elements of column c to 0 or 1, or set all elements of row r to 0 or 1. There can be another type of query, printing the total number of 1's in column c or row r. I have no idea how to solve this and any help would be appreciated. Obviously a O

How to effectively answer range queries in an array of integers?

自古美人都是妖i 提交于 2019-12-23 23:32:24
问题 How to effectively and range queries in an array of integers? Queries are of one type only , which is, given a range [a,b] , find the sum of elements that are less than x (here x is a part of each query, say of the form a b x ). Initially, I tried to literally go from a to b and check if current element is less than x and adding up. But, this way is very inefficient as complexity is O(n). Now I am trying with segment trees and sort the numbers while merging. But now my challenge is if I sort,

Optimize sum(i,j) and update(i,value) for an arryas of integers

流过昼夜 提交于 2019-12-23 06:27:11
问题 Given a huge array of integers, optimize the functions sum(i,j) and update(i,value), so that both the functions take less than O(n). Update Its an interview question. I have tried O(n) sum(i,j) and O(1) update(i, value). Other solution is preprocess the input array into 2-d array to give O(1) answer for sum(i,j). But that makes the update function of O(n). For example, given an array: A[] = {4,5,4,3,6,8,9,1,23,34,45,56,67,8,9,898,64,34,67,67,56,...} Operations are to be defined are sum(i,j)

Optimize sum(i,j) and update(i,value) for an arryas of integers

笑着哭i 提交于 2019-12-23 06:27:06
问题 Given a huge array of integers, optimize the functions sum(i,j) and update(i,value), so that both the functions take less than O(n). Update Its an interview question. I have tried O(n) sum(i,j) and O(1) update(i, value). Other solution is preprocess the input array into 2-d array to give O(1) answer for sum(i,j). But that makes the update function of O(n). For example, given an array: A[] = {4,5,4,3,6,8,9,1,23,34,45,56,67,8,9,898,64,34,67,67,56,...} Operations are to be defined are sum(i,j)

How is the memory of the array of segment tree 2 * 2 ^(ceil(log(n))) - 1?

↘锁芯ラ 提交于 2019-12-20 08:19:19
问题 The link: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/. This is the quoted text: We start with a segment arr[0 . . . n-1]. and every time we divide the current segment into two halves(if it has not yet become a segment of length 1), and then call the same procedure on both halves, and for each such segment we store the sum in corresponding node. All levels of the constructed segment tree will be completely filled except the last level. Also, the tree will be a Full