您的位置:首页 > 其它

codevs1291 火车线路

2016-09-03 21:13 211 查看
题目描述 Description

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

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

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

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

4 6 4

1 4 2

1 3 2

2 4 3

1 2 3

样例输出 Sample Output

T

T

N

N

正解:线段树

解题报告:

  想当年线段树入门的时候我就是看的这道题,当时居然没看懂。。。一直留着,都一年了。。。

  操作显然是可以线段树维护的,注意一个区间[l,r]中r并不需要计算,所以只需要操作[l,r-1]就可以了。我们只需要查询区间最小值就可以了,因为如果这个区间的最小值都可以满足的话,显然整个区间是可以满足的,区间修改、区间查询即可。

//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = 60011;
int n,s,m,ans,ql,qr,val;
struct node{
int lazy,_min;
}a[MAXN*4];

inline int getint()
{
int w=0,q=0; char c=getchar();
while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar();
while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
}

inline void build(int root,int l,int r){//记录的是实际区间
a[root]._min=s; if(l==r) return ;
int mid=(l+r)/2,lc=root*2,rc=lc+1;
build(lc,l,mid); build(rc,mid+1,r);
}

inline void pushdown(int root,int l,int r){
if(!a[root].lazy) return ; if(l==r) return ;
int lc=root*2,rc=lc+1;
a[lc].lazy+=a[root].lazy; a[rc].lazy+=a[root].lazy;
a[lc]._min-=a[root].lazy; a[rc]._min-=a[root].lazy;
a[root].lazy=0; a[root]._min=min(a[lc]._min,a[rc]._min);
}

inline void query(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) { ans=min(ans,a[root]._min); return ; }
int mid=(l+r)/2; int lc=root*2,rc=lc+1;
if(ql<=mid) query(lc,l,mid); if(qr>mid) query(rc,mid+1,r);
a[root]._min=min(a[lc]._min,a[rc]._min);
}

inline void update(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) { a[root]._min-=val; a[root].lazy+=val; return ; }
int mid=(l+r)/2; int lc=root*2,rc=lc+1;
if(ql<=mid) update(lc,l,mid); if(qr>mid) update(rc,mid+1,r);
a[root]._min=min(a[lc]._min,a[rc]._min);
}

inline void work(){
n=getint(); s=getint(); m=getint();
build(1,1,n); int x,y,z;
for(int i=1;i<=m;i++) {
x=getint(); y=getint(); z=getint();
ans=s; ql=x; qr=y-1; if(ql<=qr) query(1,1,n);
if(ql>qr){ printf("N\n"); continue; }
if(ans>=z) { val=z;  if(ql<=qr)update(1,1,n); printf("T"); }
else printf("N");
printf("\n");
}
}

int main()
{
work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: