Subarray queries

后端 未结 2 1169
青春惊慌失措
青春惊慌失措 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)

    0 讨论(0)
  • 2021-02-09 22:20

    You can decompose it the other way around. Namely, build a tree of half-arrays (this is

    0 讨论(0)
提交回复
热议问题