Mysterious Object

Time limit8sMemory limit128 MB

Summary
Support range updates that overwrite box X in [L,R] with ((X-L+1)*A) mod B, and answer range-sum queries over up to 10^9 boxes with 50000 operations.
Level

Hard8 of 10

Topics
Segment tree, Math, Number theory
Solved
No attempts yet

Problem

A person finds a mysterious object while walking. To the left of the object, there are N boxes in a row, and every box is initially empty.

The object accepts four integers L, R, A, and B. When its button is pressed, it changes the number of stones in boxes L through R by the following rule.

Box L receives A mod B stones. Box L+1 receives (2*A) mod B stones. In general, for every X such that L <= X <= R, box X is set to ((X-L+1)*A) mod B stones.

Given several commands, answer every command that asks for the total number of stones in a range.

Input

The first line contains the number of boxes N and the number of queries Q.

  • 1 <= N <= 1,000,000,000
  • 1 <= Q <= 50,000

Each of the next Q lines contains one command.

  • 1 L R A B: set the number of stones in boxes L through R by the rule above. (1 <= L <= R <= N, 1 <= A, B <= 1,000,000)
  • 2 L R: ask for the total number of stones in boxes L through R. (1 <= L <= R <= N)

All ranges include both endpoints.

Output

For each command that starts with 2, print the total number of stones in the requested range on its own line.

Examples3

  1. Example 1

    Input
    6 3
    2 1 6
    1 1 5 1 2
    2 1 6
    
    Expected output
    0
    3
    
  2. Example 2

    Input
    4 5
    1 1 4 3 4
    2 1 1
    2 2 2
    2 3 3
    2 4 4
    
    Expected output
    3
    2
    1
    0
    
  3. Example 3

    Input
    4 4
    1 1 4 7 9
    2 1 4
    1 1 4 1 1
    2 1 4
    
    Expected output
    16
    0