CF1175E Minimal Segment Cover

Topic Link

The meaning of problems

Gives the n line segments. m times to ask, Ask every time a given interval \ ([l, r] \ ) asked how many segments requires a minimum in order to cover a broad range \ ([l, r] \ ) .
All coordinates \ (\ Le 5 \ Times 10 ^ 5 \) . \ (n, m \ le 2 \ times 10 ^ 5 \)

Thinking

In fact, it is more classic line coverage problems.

\ (f [i] [j ] \) represents the start away from i \ (2 ^ j \) position of reaching as far as the line segments.

Then for each inquiry to have to go again.

Code

/*
* @Author: wxyww
* @Date:   2019-06-06 10:55:48
* @Last Modified time: 2019-06-06 14:54:02
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000000 + 100,logN = 23;
ll read() {
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
int f[N][logN + 1];
int query(int l,int r) {
    ll ans = 0;
    for(int i = logN - 1;i >= 0;--i) { 
        if(f[l][i] < r) {
            l = f[l][i];
            ans += (1 << i);
        }
    }
    l = f[l][0];ans++;
    if(l < r) return -1;
    return ans;

}
int main() {
    int n = read(),m = read();
    int mx = 0;
    for(int i = 1;i <= n;++i) {
        int l = read() + 1,r = read() + 1;
        f[l][0] = max(f[l][0],r);
        mx = max(mx,r);
    }

    for(int i = 1;i <= mx;++i) f[i][0] = max(f[i][0],max(i,f[i - 1][0]));

    for(int j = 1;j < logN;++j) 
        for(int i = 1;i <= mx;++i) 
            f[i][j] = f[f[i][j - 1]][j - 1];

    while(m--) {
        int l = read() + 1,r = read() + 1;
        printf("%d\n",query(l,r));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wxyww/p/CF1175E.html