Under construction

Still being built. Things may change

Time-Traveling Vitaro

Time limit3sMemory limit512 MB

Summary
Cities are joined in a line by roads that open only in given time windows; the task answers minimum time-rewind uses for trips while windows change.
Level

Hard8 of 10

Topics
Segment tree, Intervals, Greedy
Solved
No attempts yet

Problem

Beverland has NN cities numbered 1 through NN. The ii-th road (1≤i≤N−11 \le i \le N-1) connects city ii and city i+1i+1 in both directions. A day in Beverland is divided into 1 000 000 000 unit times, called cho. The moment xx cho after the start of a day is called time xx. Crossing a road takes 1 cho. The ii-th road can be crossed only between time LiL_i and time RiR_i. To be exact, to cross the ii-th road, one must leave city ii or city i+1i+1 at a time xx with Li≤x≤Ri−1L_i \le x \le R_i - 1 and arrive at the other city at time x+1x+1.

Vitaro is an ordinary beaver who lives in Beverland. He was often late, and while trying to fix that, he gained the ability to go back in time. Each use of the ability sends him back 1 cho. He cannot go back to yesterday. If he uses the ability between time 0 and time 1, he returns to time 0. He can use this ability only while he is in a city. His position does not change when he uses the ability.

Using the ability makes Vitaro tired. To find a way to move with the fewest uses of the ability, Vitaro ran QQ thought experiments. In step jj of the experiments, he does one of the following:

  • Changes the times at which the PjP_j-th road can be crossed. After the change, the PjP_j-th road can be crossed only between time SjS_j and time EjE_j.
  • Starts in city AjA_j at time BjB_j and finds the minimum number of uses of the ability needed to reach city CjC_j at time DjD_j.

Input

The first line contains NN and QQ. Each of the next N−1N-1 lines contains LiL_i and RiR_i. Then QQ queries follow, one per line. Each query has one of two forms.

  • If Tj=1T_j = 1, the query consists of four integers TjT_j, PjP_j, SjS_j, EjE_j.
  • If Tj=2T_j = 2, the query consists of five integers TjT_j, AjA_j, BjB_j, CjC_j, DjD_j.

Output

For each query with Tj=2T_j = 2, print the minimum number of uses of the ability, one per line, in the order the queries appear.

Constraints

  • 1≤N≤300 0001 \le N \le 300\ 000.
  • 1≤Q≤300 0001 \le Q \le 300\ 000.
  • 0≤Li<Ri≤999 999 9990 \le L_i < R_i \le 999\ 999\ 999 (1≤i≤N−11 \le i \le N-1).
  • 1≤Tj≤21 \le T_j \le 2 (1≤j≤Q1 \le j \le Q).
  • 1≤Pj≤N−11 \le P_j \le N-1 (1≤j≤Q1 \le j \le Q, Tj=1T_j = 1).
  • 1≤Sj≤Ej≤999 999 9991 \le S_j \le E_j \le 999\ 999\ 999 (1≤j≤Q1 \le j \le Q, Tj=1T_j = 1).
  • 1≤Aj≤N1 \le A_j \le N (1≤j≤Q1 \le j \le Q, Tj=2T_j = 2).
  • 1≤Bj≤999 999 9991 \le B_j \le 999\ 999\ 999 (1≤j≤Q1 \le j \le Q, Tj=2T_j = 2).
  • 1≤Cj≤N1 \le C_j \le N (1≤j≤Q1 \le j \le Q, Tj=2T_j = 2).
  • 1≤Dj≤999 999 9991 \le D_j \le 999\ 999\ 999 (1≤j≤Q1 \le j \le Q, Tj=2T_j = 2).

Examples4

  1. Example 1

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

    Input
    5 5
    3 5
    4 8
    2 6
    5 10
    2 5 3 1 10
    2 2 6 5 6
    1 3 4 6
    2 3 3 4 3
    2 4 5 1 5
    
    Expected output
    4
    3
    2
    3
    
  3. Example 3

    Input
    7 7
    112103440 659752416
    86280800 902409187
    104535475 965602300
    198700180 945132880
    137957976 501365807
    257419446 565237610
    2 4 646977260 7 915994878
    2 1 221570340 6 606208433
    2 7 948545948 4 604273995
    2 7 247791098 5 944822313
    2 7 250362511 2 50167280
    2 3 364109400 4 555412865
    2 7 33882587 7 186961394
    
    Expected output
    145611455
    0
    447180143
    0
    207252171
    0
    0
    
  4. Example 4

    Input
    7 7
    535825574 705426142
    964175291 996597835
    481817391 649559926
    4519006 410772613
    74521477 274584126
    256535565 899389890
    1 6 511428966 602601933
    1 1 69986642 201421232
    2 3 636443425 4 625975977
    1 6 235225515 405336399
    2 3 866680458 3 701821857
    1 6 180606048 900533151
    1 6 612564160 720179605
    
    Expected output
    10467449
    164858601