Codeforces 1175E Minimal Segment Cover

題意
\(n\)條線段,區間爲\([l_i, r_i]\),每次詢問\([x_i, y_i]\),問要被覆蓋最少要用多少條線段。c++

思路
\(f[i][j]\)表示以\(i\)爲左端點,用了\(2^j\)條線段,最遠到哪裏。
而後從大到小貪心便可,相似於倍增找LCA的過程。spa

代碼code

#include <bits/stdc++.h>
using namespace std;

#define N 200010
#define M 500010
#define D 20
int n, q;
int l[N], r[N];
int f[M][D];

int work(int x, int y) {
    int ans = 0;
    for (int i = D - 1; i >= 0; --i) {
        if (f[x][i] < y) {
            x = f[x][i];
            ans |= (1 << i);
        }
    }
    x = f[x][0]; ++ans;
    if (x < y) ans = -1;
    return ans;
}

int main() {
    while (scanf("%d%d", &n, &q) != EOF) {
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", l + i, r + i);
            ++l[i], ++r[i];
        }
        memset(f, 0, sizeof f);
        for (int i = 1; i <= n; ++i) {
            f[l[i]][0] = max(f[l[i]][0], r[i]);
        }
        for (int i = 1; i < M; ++i) {
            f[i][0] = max(f[i][0], max(i, f[i - 1][0]));
            for (int j = 1; j < D; ++j) {  
                f[i][j] = max(f[i][j], max(f[i][j - 1], f[i - 1][j]));
            }
        }
    
        for (int j = 1; j < D; ++j) {
            for (int i = 1; i < M; ++i) {
                f[i][j] = max(f[i][j], f[f[i][j - 1]][j - 1]);
            }
        }   
        int x, y;
        while (q--) {
            scanf("%d%d", &x, &y);
            ++x, ++y;
            printf("%d\n", work(x, y));
        }
    }
    return 0;
}
相關文章
相關標籤/搜索