其他分享
首页 > 其他分享> > BZOJ 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 线段树_贪心

BZOJ 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 线段树_贪心

作者:互联网

Code:

#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 10000000 
#define ls (x<<1) 
#define rs ((x<<1)|1)
#define mid ((l+r)>>1) 
using namespace std;
struct Q
{
	int l,r,num; 
	bool operator<(Q e)const 
	{
		return r==e.r?l>e.l:r<e.r;  
	}
}q[maxn]; 
int k,n,c; 
int maxv[maxn],lazy[maxn]; 
void pushup(int x) 
{ 
	maxv[x]=max(maxv[ls],maxv[rs]);  
}       
void mark(int x,int delta)
{ 
	lazy[x]+=delta,maxv[x]+=delta; 
}
void pushdown(int l,int r,int x)
{
	if(lazy[x]) 
	{
		if(mid>=l)  mark(ls, lazy[x]); 
		if(mid+1<=r)mark(rs, lazy[x]);
		lazy[x]=0; 
	}
}
int query(int l,int r,int x,int L,int R)
{
	if(l>=L&&r<=R) return maxv[x]; 
	pushdown(l,r,x); 
	int t=0;
	if(L<=mid) t = query(l,mid,ls,L,R); 
	if(R>mid)  t = max(t,query(mid+1,r,rs,L,R)); 
	return t; 
} 
void update(int l,int r,int x,int L,int R,int delta)
{
	pushdown(l,r,x); 
	if(l>=L&&r<=R) 
	{
		mark(x, delta); 
		return; 
	}	
	if(L<=mid) update(l,mid,ls,L,R,delta); 
	if(R>mid) update(mid+1,r,rs,L,R,delta); 
	pushup(x); 
}
int main()
{
	//setIO("input"); 
	scanf("%d%d%d",&k,&n,&c); 
	for(int i=1;i<=k;++i) scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].num); 
	sort(q+1,q+1+k);  
	int ans=0;    
    for(int i=1;i<=k;++i) 
    {      
    	int tmp=query(1,n,1,q[i].l,q[i].r-1);      
    	ans+=min(q[i].num, c - tmp);             
    	update(1,n,1,q[i].l,q[i].r-1,min(q[i].num,c - tmp));  
    }
    printf("%d\n",ans); 
    return 0; 
}

  

标签:Feb,Fair,rs,int,d%,mid,rmid,捷运,define
来源: https://www.cnblogs.com/guangheli/p/10975370.html