xdoj-1324 (区间离散化-线段树求区间最值)

时间:2023-12-17 12:55:26

思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间

2 核心:dp[i]  覆盖从1到i区间的最小花费

        dp[a[i].r]=min (dp[k])+a[i]s;  l-1=<k<=r-1

        (可我总是想着从后到前,从 前到后反而更好理解)

   3  离散区间---使用线段树求区间最值  时间复杂度O(nlogn)

ps (一定忍住不看别人的代码,继续加油)

 #include <bits/stdc++.h>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
using namespace std;
typedef long long LL;
struct node {
LL l,r,s;
bool operator < (const node& t) const {
return r<t.r;
}
};
const int N=2e5+;
const int MAX=0x3f3f3f3f;
node a[N];
LL b[N]; int cnt;
int tree[N*];
int n; LL M,E;
LL ans;
inline void pushup (int rt) {
tree[rt]=min (tree[rt*],tree[rt*+]);
}
inline int mapp (LL x) {
return lower_bound(b+,b++cnt,x)-b;
}
void update (int p,int k,int l,int r,int rt) {
if (l==r) {
tree[rt]=min (k,tree[rt]);
return ;
}
int m=(l+r)/;
if (p<=m) update (p,k,lson);
else update (p,k,rson);
pushup(rt);
return ;
}
int query (int L,int R,int l,int r,int rt) {
if (l>=L&&r<=R) return tree[rt];
if (l>R||r<L) return MAX;
int m=(l+r)/;
return min (query(L,R,lson),query(L,R,rson));
}
int main ()
{
while (~scanf ("%d %lld %lld",&n,&M,&E)) {
cnt=;
for (int i=;i<=n;i++) {
scanf ("%lld %lld %lld",&a[i].l,&a[i].r,&a[i].s);
a[i].l--;
b[++cnt]=a[i].l; b[++cnt]=a[i].r;
}
sort (b+,b++cnt); cnt=;
for (int i=;i<=*n;i++)
if (b[i]!=b[cnt])
b[++cnt]=b[i]; if (b[]!=M-||b[cnt]!=E) ans=-;
else {
memset (tree,0x3f,sizeof(tree));
sort (a+,a++n);
update (,,,cnt,);
for (int i=;i<=n;i++) {
int r=mapp(a[i].r);
int l=mapp(a[i].l);
int tmp=query (l,r,,cnt,);
update (r,tmp+a[i].s,,cnt,);
}
ans=query (cnt,cnt,,cnt,);
if (ans==MAX) ans=-;
}
printf("%d\n",ans);
}
return ;
}