Under construction

Still being built. Things may change

Classical Data Structure Problem

Time limit3sMemory limit128 MB

Summary
Apply n range updates to an array of length 2^m, where each update adds i to a range and x grows by each new value; output x mod 2^30.
Level

Hard9 of 10

Topics
Segment tree, Array, Simulation
Solved
No attempts yet

Problem

You have an integer array A=[a0,a1,…,a2m−1]A = [a_0, a_1, \ldots, a_{2^m-1}] of length 2m2^m. Initially, every element is zero.

You also have an integer variable xx. Initially, x=0x = 0.

For each i=1,2,…,ni = 1, 2, \ldots, n, you are given two integers pip_i and qiq_i, and you perform the following steps:

  • Let p′=(pi+x) mod 2mp' = (p_i + x) \bmod 2^m and q′=(qi+x) mod 2mq' = (q_i + x) \bmod 2^m.
  • Let l=min⁡(p′,q′)l = \min(p', q') and r=max⁡(p′,q′)r = \max(p', q').
  • For each j=l,l+1,…,rj = l, l+1, \ldots, r, increase aja_j by ii, then increase xx by aja_j.

Find the value of x mod 230x \bmod 2^{30} at the end of this process.

Input

The first line contains two integers nn and mm (1≤n≤500 0001 \le n \le 500\,000; 1≤m≤301 \le m \le 30).

The ii-th of the following nn lines contains two integers pip_i and qiq_i (0≤pi,qi<2m0 \le p_i, q_i < 2^m).

Output

Print the value of x mod 230x \bmod 2^{30}.

Hint

In the example test, initially A=[0,0,0,0]A = [0, 0, 0, 0] and x=0x = 0. Then:

  • For i=1i = 1, l=1l = 1 and r=2r = 2. Then A=[0,1,1,0]A = [0, 1, 1, 0] and x=2x = 2.
  • For i=2i = 2, l=1l = 1 and r=3r = 3. Then A=[0,3,3,2]A = [0, 3, 3, 2] and x=10x = 10.
  • For i=3i = 3, l=0l = 0 and r=1r = 1. Then A=[3,6,3,2]A = [3, 6, 3, 2] and x=19x = 19.
  • For i=4i = 4, l=0l = 0 and r=3r = 3. Then A=[7,10,7,6]A = [7, 10, 7, 6] and x=49x = 49.
  • For i=5i = 5, l=1l = 1 and r=3r = 3. Then A=[7,15,12,11]A = [7, 15, 12, 11] and x=87x = 87.

Examples1

  1. Example 1

    Input
    5 2
    2 1
    1 3
    3 2
    1 0
    0 2
    
    Expected output
    87