Another Contest 3 Problem 4 - Range Updates and Range Queries

View as PDF

Submit solution


Points: 15
Time limit: 0.25s
Memory limit: 256M

Problem types

You have an array of N integers, initialized all to zero to begin with. Support two operations.

INCREMENT l r a - For each index i between l and r, increase the ith element of the array by a(il+1).

SUM l r - Compute the sum of the integers between indices l and r, inclusive.

Constraints

1N106

1Q105

1liriN

1ai5

Input Specification

The first line contains two positive integers, N and Q.

The next Q lines each contain a sequence of positive integers. If the first integer in the line is 1, then three integers follow, li, ri, and ai, indicating an INCREMENT operation.

Otherwise, the first integer in the line is 2, and then two integers follow, li and ri, indicating a SUM operation.

Output Specification

For each SUM operation, output on its own line the result of the query.

Sample Input

Copy
3 4
1 2 3 2
2 1 1
2 2 2
2 3 3

Sample Output

Copy
0
2
4

Comments


  • 0
    myl  commented on March 22, 2021, 2:47 p.m.

    I am assuming the array starts at index 0?


    • 2
      BalintR  commented on March 22, 2021, 2:57 p.m. edited

      1liriN so no, the array starts at index 1.


  • 0
    AlanCCCL2S18  commented on Feb. 6, 2020, 11:51 p.m.

    Can anyone tell me why I'm TLEing on testcase 5? I know there's some sort of optimization but I don't know the specifics.


    • 8
      richardzhang  commented on Feb. 8, 2020, 1:08 a.m. edited

      Your time complexity is currently O(NQlogN), which is insufficient given the constraints (this is on the order of 1012 operations, give or take). If you are stumped, you can try reading the tutorial :)).