BZOJ1577 [Usaco2009 Feb]庙会捷运Fair Shuttle

按结束点排序,然后能塞多少塞多少即可

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAXN 50010
#define MAXM 20010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
struct cw{
	int s;
	int t;
	int v;
	friend bool operator <(cw x,cw y){
		return x.t>1;
	if(r<=mid){
		return ask(x<<1,y,mid,l,r);
	}else if(l>mid){
		return ask(x<<1|1,mid+1,z,l,r);
	}else{
		return max(ask(x<<1,y,mid,l,mid),ask(x<<1|1,mid+1,z,mid+1,r));
	}
}
void change(int x,int y,int z,int l,int r,int cv){
	if(y==l&&z==r){
		toch(x,cv);
		return ;
	}
	pd(x);
	int mid=y+z>>1;
	if(r<=mid){
		change(x<<1,y,mid,l,r,cv);
	}else if(l>mid){
		change(x<<1|1,mid+1,z,l,r,cv);
	}else{
		change(x<<1,y,mid,l,mid,cv);
		change(x<<1|1,mid+1,z,mid+1,r,cv);
	}
	ud(x);
}
int main(){
	int i;
	scanf("%d%d%d",&n,&m,&c);
	for(i=1;i<=n;i++){
		scanf("%d%d%d",&a[i].s,&a[i].t,&a[i].v);
		a[i].t--;
	}
	
	sort(a+1,a+n+1);
	for(i=1;i<=n;i++){
		int t=min(a[i].v,c-ask(1,1,m,a[i].s,a[i].t));
		ans+=t;
		change(1,1,m,a[i].s,a[i].t,t);
	}
	printf("%lld\n",ans);
	return 0;
}

/*
8 15 3
1 5 2
13 14 1
5 8 3
8 14 2
14 15 1
9 12 1
12 15 2
4 6 1

*/


你可能感兴趣的:(BZOJ,贪心,线段树)