p1607 Temple Fair Bus Section Greedy Plus Line Section Tree

If there is only one shuttle bus, it is the greedy greedy and greedy maintenance end of the typical ordinary district.
But there are many buses, in fact, the same can be greedy at the end of the line, and then line segment tree maintenance of a certain section of the shuttle bus can be used much, and then there is his interval is left and right open, so the need to add cattle, the right endpoint -1;
The following gives the line segment tree ac code, it is said that data water, seemingly violent can pass without the line segment tree;

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;
const int maxn=2e4+7,maxm=5e4+7;
struct node{
    int l,r,ma,lazy;
}tree[maxn<<2];
struct cow{
    int s,t,c;
}cows[maxm];
int n,m,max_c;

bool cmp(cow a,cow b){
    return a.t<b.t;
}
void push_down(int p){
    int d=tree[p].lazy;
    if(!d)return;
    tree[p<<1].lazy+=d,tree[p<<1].ma+=d;
    tree[p<<1|1].lazy+=d,tree[p<<1|1].ma+=d;
    tree[p].lazy=0;
}
void push_up(int p){
    tree[p].ma=max(tree[p<<1].ma,tree[p<<1|1].ma);
}
void build(int p,int L,int R){
    tree[p].l=L,tree[p].r=R;
    if(L==R)return;
    int mid=L+R>>1;
    build(p<<1,L,mid);build(p<<1|1,mid+1,R);
}
void add(int p,int l,int r,int d){
    int L=tree[p].l,R=tree[p].r,mid=L+R>>1;
    if(l<=L&&R<=r){tree[p].ma+=d,tree[p].lazy+=d;return;}
    push_down(p);
    if(l<=mid)add(p<<1,l,r,d);
    if(mid+1<=r)add(p<<1|1,l,r,d);
    push_up(p);
}
int query(int p,int l,int r){
    int L=tree[p].l,R=tree[p].r,mid=L+R>>1,tem=0;
    if(l<=L&&R<=r)return tree[p].ma;
    push_down(p);
    if(l<=mid)tem=max(tem,query(p<<1,l,r));
    if(mid+1<=r)tem=max(tem,query(p<<1|1,l,r));
    return tem;
}
int main()
{
    scanf("%d%d%d",&m,&n,&max_c);int a,b,c;
    for(int i=1;i<=m;i++)scanf("%d%d%d",&a,&b,&c),cows[i]={a,b-1,c};
    sort(cows+1,cows+1+m,cmp);build(1,1,n);
    int ans=0;
    for(int i=1;i<=m;i++){
        int x=max_c-query(1,cows[i].s,cows[i].t);
        int tem=min(x,cows[i].c);
        ans+=tem;add(1,cows[i].s,cows[i].t,tem);
    }
    printf("%d\n",ans);
    return 0;
}

Posted by TheLostGuru on Mon, 30 Sep 2019 07:59:21 -0700