Subarray queries

后端 未结 2 1167
青春惊慌失措
青春惊慌失措 2021-02-09 21:49

So I was trying to solve this programming problem.

Given an array of numbers and some queries. Each query gives you three numbers a,b,c and asks you to answer sum of all

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 22:14

    1. sort queries according to c

    2. sort array values in non-decreasing order

    3. maintain (bit|segment tree) for storing range sum

    4. before answering the current query, update index of all the array element whose value <= c

    5. answer the range-query sum problem.

    Time Complexity: (n+q)*log(n), space complexity: O(n)

提交回复
热议问题