Another Contest 3 Problem 4 - Range Updates and Range Queries

View as PDF

Submit solution

Points: 15
Time limit: 1.0s
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 \cdot (i-l+1).

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


1 \le N \le 10^6

1 \le Q \le 10^5

1 \le l_i \le r_i \le N

1 \le a_i \le 5

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, l_i, r_i, and a_i, indicating an INCREMENT operation.

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

Output Specification

For each SUM operation, output on its own line the result of the query. The data will be set such that this fits in a signed 32-bit integer.

Sample Input

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

Sample Output



  • 0
    myl  commented on March 22, 2021, 10:47 a.m.

    I am assuming the array starts at index 0?

    • 0
      csDude256  commented on March 22, 2021, 10:57 a.m.

       1 \le l_i \le r_i \le N so no, the array starts at index 1.

  • 1
    AlanCCCL2S18  commented on Feb. 6, 2020, 6: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.

    • 6
      richardzhang  commented on Feb. 7, 2020, 8:08 p.m.

      Your time complexity is currently O(NQ\log N), which is insufficient given the constraints (this is on the order of 10^{12} operations, give or take). If you are stumped, you can try reading the tutorial :)).