【CODEVS1291】火车线路

Description

某列火车行使在C个城市之间(出发的城市编号为1,结束达到的城市的编号为C),假设该列火车有S个座位,现在有R笔预订票的业务。现在想对这R笔业务进行处理,看哪些预定能满足,哪些不能满足。

一笔预定由O、D、N三个整数组成,表示从起点站O到目标站D需要预定N个座位。一笔预定能满足是指该笔业务在行程范围内有能满足的空座位,否则就不能满足。一笔业务不能拆分,也就是起点和终点站不能更改,预定的座位数目也不能更改。所有的预定需求按给出先后顺序进行处理。

请你编写程序,看那些预定业务能满足,那些不能满足。

Input

       输入文件中的第一行为三个整数CSR(1<=c<=60 000, 1<=s<=60 000, 1<=r<=60 000)他们之间用空隔分开。接下来的R行每行为三个整数O、D、N,(1<=o<d<=c, 1<=n<=s),分别表示每一笔预定业务。

Output

       对第I笔业务,如果能满足,则在输出文件的第I行输出“T”,否则输出“N”

Sample Input

4 6 4
1 3 2
2 4 3

1 2 3

1 4 2

Sample Output

T
T
N

N
/*
线段树维护区间最小值 
区间查询、区间修改,需要引入延迟修改标志
订票业务st,en,cnt对应的区间是[st,en) 
*/

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,s,tot=0,ans=0;
const int maxn=60010; 
int cnt,st,en; 
struct treety{ 
    int Left,Right; //区间[ll,rr) 
    int lptr,rptr;
    int bj,dat; //dat:区间内能提供的座位数(最小值);bj:延迟修改标志 
}t[maxn*2];
int min(int a,int b)
{
    if (a>b) return b; else return a;
}
void buildtree(int ll,int rr)
{
    int cur=++tot;
    t[cur].Left=ll; t[cur].Right=rr; t[cur].dat=s;
    if (ll!=rr-1)
    {
        t[cur].lptr=tot+1; buildtree(ll,(ll+rr)/2);
        t[cur].rptr=tot+1; buildtree((ll+rr)/2,rr);
        t[cur].dat=s;
    }
}

void update(int k)
{
    t[t[k].lptr].dat-=t[k].bj;    t[t[k].rptr].dat-=t[k].bj;
    t[t[k].lptr].bj+=t[k].bj;    t[t[k].rptr].bj+=t[k].bj;
    t[k].bj=0;
}

void change(int k,int ll,int rr,int delta)
{
    if (ll<=t[k].Left && rr>=t[k].Right)
    {
        t[k].dat-=delta;
        t[k].bj+=delta;
    }
        else
        {
            if (t[k].bj) update (k);
            if (ll<(t[k].Left+t[k].Right)/2) change(t[k].lptr,ll,rr,delta);
            if (rr>(t[k].Left+t[k].Right)/2) change(t[k].rptr,ll,rr,delta);
            t[k].dat=min(t[t[k].lptr].dat,t[t[k].rptr].dat);
        }
}

int qurey(int k,int ll,int rr)
{
    if (t[k].Right<=rr && t[k].Left>=ll) return t[k].dat;
    if (t[k].bj) update(k);
    int ans=s; //取最小,初值赋为理论最大值 
    if (ll<(t[k].Left+t[k].Right)/2) ans=min(ans,qurey(t[k].lptr,ll,rr));
    if (rr>(t[k].Left+t[k].Right)/2) ans=min(ans,qurey(t[k].rptr,ll,rr));
    return ans;
}

int main()
{
    scanf("%d%d%d",&n,&s,&m);
    buildtree(1,n); //[n,n+1)段不需要 
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&st,&en,&cnt);
        if (qurey(1,st,en)>=cnt)
        {
            printf("T\n");
            change(1,st,en,cnt);
        }
        else printf("N\n");
    }
    return 0;
}
posted @ 2016-01-10 09:20  mengyue  阅读(234)  评论(0编辑  收藏  举报