codevs1291 火车线路

题目描述 Description

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

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

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

输入描述 Input Description

       输入文件中的第一行为三个整数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 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]就可以了。我们只需要查询区间最小值就可以了,因为如果这个区间的最小值都可以满足的话,显然整个区间是可以满足的,区间修改、区间查询即可。

 

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int MAXN = 60011;
16 int n,s,m,ans,ql,qr,val;
17 struct node{
18     int lazy,_min;
19 }a[MAXN*4];
20 
21 inline int getint()
22 {
23        int w=0,q=0; char c=getchar();
24        while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
25        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
26 }
27 
28 inline void build(int root,int l,int r){//记录的是实际区间
29     a[root]._min=s; if(l==r) return ;
30     int mid=(l+r)/2,lc=root*2,rc=lc+1;
31     build(lc,l,mid); build(rc,mid+1,r);
32 }
33 
34 inline void pushdown(int root,int l,int r){
35     if(!a[root].lazy) return ; if(l==r) return ;
36     int lc=root*2,rc=lc+1;
37     a[lc].lazy+=a[root].lazy; a[rc].lazy+=a[root].lazy;
38     a[lc]._min-=a[root].lazy; a[rc]._min-=a[root].lazy;
39     a[root].lazy=0; a[root]._min=min(a[lc]._min,a[rc]._min);
40 }
41 
42 inline void query(int root,int l,int r){
43     pushdown(root,l,r);
44     if(ql<=l && r<=qr) { ans=min(ans,a[root]._min); return ; }
45     int mid=(l+r)/2; int lc=root*2,rc=lc+1;
46     if(ql<=mid) query(lc,l,mid); if(qr>mid) query(rc,mid+1,r);
47     a[root]._min=min(a[lc]._min,a[rc]._min);
48 }
49 
50 inline void update(int root,int l,int r){
51     pushdown(root,l,r);
52     if(ql<=l && r<=qr) { a[root]._min-=val; a[root].lazy+=val; return ; }
53     int mid=(l+r)/2; int lc=root*2,rc=lc+1;
54     if(ql<=mid) update(lc,l,mid); if(qr>mid) update(rc,mid+1,r);
55     a[root]._min=min(a[lc]._min,a[rc]._min);
56 }
57 
58 inline void work(){
59     n=getint(); s=getint(); m=getint();
60     build(1,1,n); int x,y,z;
61     for(int i=1;i<=m;i++) {
62     x=getint(); y=getint(); z=getint();
63     ans=s; ql=x; qr=y-1; if(ql<=qr) query(1,1,n);
64     if(ql>qr){ printf("N\n"); continue; }
65     if(ans>=z) { val=z;  if(ql<=qr)update(1,1,n); printf("T"); }
66     else printf("N");
67     printf("\n");
68     }
69 }
70 
71 int main()
72 {
73   work();
74   return 0;
75 }

 

posted @ 2016-09-03 21:13  ljh_2000  阅读(286)  评论(0编辑  收藏  举报