poj 1769 Minimizing maximizer 线段树维护dp

时间:2023-11-10 20:08:32

题目链接

给出m个区间, 按区间给出的顺序, 求出覆盖$ [1, n] $ 至少需要多少个区间。

如果先给出[10, 20], 在给出[1, 10], 那么相当于[10, 20]这一段没有被覆盖。

令dp[i]表示覆盖[1, i]需要的区间数量。 那么有状态转移方程dp[i] = $ min[dp[i], dp[j] (s_k <= j < t_k)] + 1 $

然后求 \([s_k, t_k]\) 的最小值可以用线段树来求。 复杂度 $ m\log{n} $

感觉这个题的难度在于理解题意....

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 5e4+5;
int minn[maxn<<2];
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
minn[rt] = min(val, minn[rt]);
return ;
}
int m = l+r>>1;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
minn[rt] = min(minn[rt<<1], minn[rt<<1|1]);
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return minn[rt];
}
int m = l+r>>1, ret = inf;
if(L<=m)
ret = min(ret, query(L, R, lson));
if(R>m)
ret = min(ret, query(L, R, rson));
return ret;
}
int main()
{
int n, m, a, b, x;
while(~scanf("%d%d", &n, &m)) {
mem2(minn);
update(1, 0, 1, n, 1);
for(int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
int x = query(a, b-1, 1, n, 1);
update(b, x+1, 1, n, 1);
}
printf("%d\n", query(n, n, 1, n, 1));
}
return 0;
}