XDOJ1045火車預訂請求系統

題目

線段樹區間加 線段樹區間找最大值

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e6+10;
int tree[maxn],lazy[maxn],ok[maxn],lar[maxn];
int c,s,r;

void paint(int pos,int x)
{
	tree[pos]+=x;
	lazy[pos]+=x;
}

void pushdown(int pos,int l,int r)
{
    int m = l+r>>1;
   	paint(pos<<1,lazy[pos]);
   	paint(pos<<1|1,lazy[pos]);
    lazy[pos]=0;
}

void add(int l,int r,int L,int R,int x,int pos)
{
    if(L<=l&&r<=R){
        paint(pos,x);
        return;
    }
    int m=l+r>>1;
    if(L<=m)add(l,m,L,R,x,pos<<1);
    if(R>m)add(m+1,r,L,R,x,pos<<1|1);
    tree[pos]=max(tree[pos<<1],tree[pos<<1|1]);
}

int qry(int l,int r,int L,int R,int val,int pos)
{
	//cout<<tree[pos]<<" "<<l<<" "<<r<<endl;
    if(l>=L && r<=R)
    	return tree[pos];
    if(lazy[pos])
    	pushdown(pos,l,r);
    int m=l+r>>1;
    int flag=-1;
    if(m>=L)
        flag=max(qry(l,m,L,R,val,pos<<1),flag);
    if(m<R)
        flag=max(qry(m+1,r,L,R,val,pos<<1|1),flag);
    return flag;
}

inline int read()
{
    int x=0;char ch = 0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x;
}

int main()
{
	//freopen("date.in","r",stdin);
    c=read(),s=read(),r=read();
    for(int i=0;i<r;i++)
    {
        int l,r,v;
        l=read(),r=read(),v=read();
        if(qry(1,c-1,l,r-1,v,1)+v<=s){
            printf("YES\n");
            add(1,c-1,l,r-1,v,1);
        }
        else 
            printf("NO\n");
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章