PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000022. Phủ một đoạn bằng ít đoạn nhất (Minimal Segment Cover)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phủ một đoạn bằng ít đoạn nhất (Minimal Segment Cover)

Minimal Segment Cover

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn closed intervals [li,ri][l_i,r_i] on the number line.

For every query (x,y)(x,y), choose as few intervals as possible from the given collection so that their union covers the entire interval [x,y][x,y]. Selected intervals may overlap, but no uncovered gap is permitted.

If coverage is impossible, print -1.

Input

  • The first line contains n,qn,q.
  • The next nn lines contain intervals li,ril_i,r_i.
  • The final qq lines contain query endpoints x,yx,y.

Output

Print the minimum number of intervals per query, or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; q≤60q\le60; ri,y≤60r_i,y\le60; 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 0≤li<ri≤5⋅1050\le l_i<r_i\le5\cdot10^5; 0≤x<y≤5⋅1050\le x<y\le5\cdot10^5.
  • Subtask 2 (30 points): n≤1000n\le1000; q≤3000q\le3000; ri,y≤1000r_i,y\le1000; 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 0≤li<ri≤5⋅1050\le l_i<r_i\le5\cdot10^5; 0≤x<y≤5⋅1050\le x<y\le5\cdot10^5.
  • Subtask 3 (50 points): 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 0≤li<ri≤5⋅1050\le l_i<r_i\le5\cdot10^5; 0≤x<y≤5⋅1050\le x<y\le5\cdot10^5.

Examples

Input

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

Output

1
2
1

Explanation

Interval [1,3][1,3] requires one given interval. Covering [1,4][1,4] requires both [1,3][1,3] and [2,4][2,4]. Interval [3,4][3,4] is covered by the second interval.