Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. Оно применяется начиная с раунда 972. ×

Блог пользователя ___Abc123___

Автор ___Abc123___, история, 2 месяца назад, По-английски

Hi, Here is the Question You are given an array A of length N and q queries where each query is one of the following types:

1 i val: Update the value at ith index to val (arr[i] = val)

   2 L R: find the sum of all the subarray of arr[L....R]

Determine the value of query 2 Ex: N = 5

q 2;

arr = [2, 1, 4, 3, 1]

query = [(1, 2, 2), (2, 1, 3)]

Output = 26

How to approach this question

I have to find sum of every subarray of arr[L...R] ans will be arr[L] + arr[L...(L + 1)] + arr[L...(L +2)] +.....arr[L....R] + arr[L + 1] + (arr(L + 1)..(L + 2)] +.....(arr[(L + 1)...R])......

constraint 1 <= N, q <= 100000

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by ___Abc123___ (previous revision, new revision, compare).

»
2 месяца назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

is this not just a standard seg/fenwick tree problem try learning one of these datastructures, then it will be a template problem

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Check https://leetcode.com/problems/range-sum-query-mutable/description/.

People there have given great explanations for this problem already.

»
2 месяца назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I have to find sum of every subarray of arr[L...R] ans will be arr[L] + arr[L...(L + 1)] + arr[L...(L +2)] +.....arr[L....R] + arr[L + 1] + (arr(L + 1)..(L + 2)] +.....(arr[(L + 1)...R])......

  • »
    »
    2 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    interestingly i have actually seen this problem before

    try thinking about it as how many times each element in the range contributes to the final sum

    • »
      »
      »
      2 месяца назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Yes I have done that final expression will be (r — l + 1)arr[l] + 2*(r — l)arr[l + 1] + 3*(r — l — 1)*arr[l + 2]+.....+(r — l + 1)arr[r].

      still cant figure out what to do from here

      • »
        »
        »
        »
        2 месяца назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Another way to look at it would be to only check the contribution of each individual element in $$$[L,R]$$$. Here the $$$ith$$$ elements contribution would be $$$arr[i]*(r-i+1)*(i-l+1)$$$ as these many subarrays in $$$[L,R]$$$ contain it. Now you can see that you only need the sum of $$$arr[i]*i, arr[i]*i*i, arr[i]$$$ to find the contribution of all elements which you can find using a segment tree.

      • »
        »
        »
        »
        2 месяца назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        heres the soltuion

        you precompute an array containing the values arr[I] = nums[I] * (n-i)

        now build a segtree on arr, for each query you would just query the sum between l and r and the sum from r+1 to the end

        subtract the sum between r+1 to n from l and r and you get the answer to your query this works cuz math

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by ___Abc123___ (previous revision, new revision, compare).

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Try learning about Fenwick Tree(Binary Indexed Tree) or Segment trees! :)

»
2 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

this can be solved using Segment trees in a fast time Click on this and go learn it