P1607 [USACO09FEB] Fair Shuttle G

 

 :::pushup时 min/max 注意

:::add/query [l,r) -》 在 r 时刻 已经放下 可以继续上人

//:::pushup 写错了max->min 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;
const int maxn=5e5+10;

int n,k,c;
ll ans;

struct line{
    ll s,t,m;
}l[maxn];
bool cmp(line a,line b){
    if(a.t==b.t) return a.s<b.s;
    return a.t<b.t;
}

struct tree{
    int l,r;
    ll maxx,add;
}st[maxn<<2];

void build(int k,int l,int r)//
{
    st[k].l=l,st[k].r=r;
    if(st[k].l==st[k].r) return;
    int mid=st[k].l+st[k].r>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    st[k].maxx=max(st[k<<1].maxx,st[k<<1|1].maxx);
}

void pushdown(int k)
{
    if(st[k].add==0) return; 
    st[k<<1].maxx+= st[k].add;
    st[k<<1|1].maxx += st[k].add;
    st[k<<1].add+=st[k].add;
    st[k<<1|1].add+=st[k].add;
    st[k].add=0; 
}
void areaAdd(int k,int x,int y,int val)
{
    if(st[k].l>y||st[k].r<x) return;
    if(st[k].l>=x&&st[k].r<=y)
    {
        st[k].maxx+=val;
        st[k].add+=val;
        return;
    }
    int mid=st[k].l+st[k].r>>1;
    pushdown(k);
    areaAdd(k<<1,x,y,val);
    areaAdd(k<<1|1,x,y,val);
    st[k].maxx=max(st[k<<1].maxx,st[k<<1|1].maxx);
} 
ll query(int k,int x,int y)
{
    if(st[k].l>y||st[k].r<x) return 0;
    if(x<=st[k].l&&st[k].r<=y) return st[k].maxx;
    int mid=st[k].l+st[k].r>>1; ll res=0;
    pushdown(k);
    if(x<=mid) res=max(res,query(k<<1,x,y));
    if(mid<=y) res=max(res,query(k<<1|1,x,y));
    return res;
}

int main()
{
    ios::sync_with_stdio(false); 
    cin>>k>>n>>c;
    for(int i=1;i<=k;i++) cin>>l[i].s>>l[i].t>>l[i].m;
    sort(l+1,l+1+k,cmp);
    
    build(1,1,n);
    
    for(int i=1;i<=k;i++)
    {
        ll maxxx=query(1,l[i].s,l[i].t-1);
        ll tmp=min((c-maxxx),l[i].m);//不可以min(0,-,-) 
        ans+=tmp;
        areaAdd(1,l[i].s,l[i].t-1,tmp);
    }
    cout<<ans<<'\n';
    return 0;
}
View Code

 

 
 
 
posted @ 2023-08-19 01:23  JMXZ  阅读(12)  评论(0)    收藏  举报