CodeForces-1175E Minimal Segment Cover

Title Description

Existing \ (n-\) line segments, each segment of the left and right endpoint \ (L_i, R_i \) , to ensure \ (L_i \ Le R_i \) .

There \ (m \) a query, each query \ (X_i, Y_i \) all the points covered by the minimum required line interval.

Input

Input of the first line contains two integers \ (n-, m \) , the meanings indicated above.

Next, there are \ (n-\) rows, each row having two integers.

The first \ (i + 1 \) line contains \ (2 \) integers, respectively, \ (L_i, R_i \) .

Next \ (m \) line, indicates \ (m \) th interrogation.

\ (1 \ n, m \ the 2e5 \)

\ (0 \ the L_i \ the R_i \ The 5e5 \)

\ (0 \ X_i the \ y_i the \ the 5E5 \)

Output

Output an answer for each inquiry.

If you can not cover, output \ (--1 \) can be.

Sample Input

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

Sample Output

1
2
1
1
1
-1
-1

Using a multiplier to cover the maintenance segment interval.

First, we found that for only one endpoint we can cover it with care segment furthest to cover where to go.

For each treatment the left end point with the same segment furthest right point.

So, the process is left to cover the endpoint to jump the farthest point, \ (Ans ++ \) , Repeat this process until the cover to the right point.

If violent simulation, each jump from one segment to another end point, time complexity \ (O (n-) \) .

Think about it, we found that this process and (LCA \) \ process is like, so we can multiply maintained.

\ (Fa [i] [j ] \) is represented by \ (1 << j \) line segments, from \ (I \) starts furthest point can cover.

code show as below

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;

#define int long long
#define u64 unsigned long long
#define Raed Read
#define reg register
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(reg int i=(G).Head[x]; i; i=(G).Nxt[i])

inline int Read() {
    int res = 0, f = 1;
    char c;
    while (c = getchar(), c < 48 || c > 57)if (c == '-')f = 0;
    do res = (res << 3) + (res << 1) + (c ^ 48);
    while (c = getchar(), c >= 48 && c <= 57);
    return f ? res : -res;
}

template<class T>inline bool Min(T &a, T const&b) {
    return a > b ? a = b, 1 : 0;
}
template<class T>inline bool Max(T &a, T const&b) {
    return a < b ? a = b, 1 : 0;
}

const int N = 2e5 + 5, M = 5e5 + 5, mod = 1e6 + 7;
const int dx[5] = {1, -1, 0, 0, 0}, dy[5] = {0, 0, 0, 1, -1};
const  double eps = 1e-6;

int Fa[M][20], R[M];

inline void _main(void) {
    int n = Read(), m = Raed();
    rep(i, 1, n) {
        int x = Raed(), y = Read();
        Max(R[x], y);
    }
    Fa[0][0] = R[0];
    rep(i, 1, M - 5)Max(R[i], R[i - 1]), Fa[i][0] = R[i];
    rep(j, 1, 19)rep(i, 0, M - 5)Fa[i][j] = Fa[Fa[i][j - 1]][j - 1];
    while (m--) {
        int Ans = 0, l = Raed(), r = Raed();
        drep(i, 19, 0)if (Fa[l][i] < r)l = Fa[l][i], Ans |= 1 << i;
        l = Fa[l][0];
        if (l < r)Ans = -1;
        else Ans++;
        printf("%d\n", Ans);
    }
}

signed main() {
#define offline1
#ifdef offline
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
    _main();
    fclose(stdin); fclose(stdout);
#else
    _main();
#endif
    return 0;
}

Guess you like

Origin www.cnblogs.com/dsjkafdsaf/p/11259557.html