1291 火车线路(区间修改,区间最值)

1291 火车线路

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 

题目描述 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



分析

原思路:区间查询和,查询区间内有多少空座位与需要的座位比较(比较方法,a站到b站内剩余座位和与a站到b站需要的座位和),但这样做显然不对,如果座位都在最后面的站,而我们不能把在后面需要的座在前面的空缺处找到并使用这些,“一笔业务不能拆分,也就是起点和终点站不能更改”就是这句话。

正确思路:操作显然是可以线段树维护的,注意一个区间[l,r]中r并不需要计算,所以只需要操作[l,r-1]就可以了(到r站就下车了)。我们只需要查询区间的最小值就可以了,因为如果这个区间的最小值都可以满足的话,显然整个区间是可以满足的,线段树功能:区间维护最小值、区间查询最小值。

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 #define lson l,m,rt<<1
 5 #define rson m+1,r,rt<<1|1
 6 #define MAXN 60010
 7 
 8 int mn[MAXN<<2];
 9 int col[MAXN<<2];
10 int n,s,t,d;    //n城市,s座位,t订单 
11 void putup(int rt)
12 {
13     mn[rt] = min(mn[rt<<1],mn[rt<<1|1]);
14 }
15 void putdown(int rt)
16 {
17     if (col[rt])
18     {
19         col[rt<<1] += col[rt];
20         col[rt<<1|1] += col[rt];
21         mn[rt<<1] -= col[rt];
22         mn[rt<<1|1] -= col[rt];
23         col[rt] = 0;
24     }
25 }
26 void build(int l,int r,int rt)
27 {
28     if (l==r) 
29     {
30         mn[rt] = s;
31         return ;
32     }
33     int m = (l+r)>>1;
34     build(lson);
35     build(rson);
36     putup(rt);    
37 }
38 void update(int l,int r,int rt,int L,int R,int sc)
39 {
40     if (L<=l && r<=R)
41     {
42         col[rt] += sc;
43         mn[rt] -= sc;
44         return ;
45     }
46     putdown(rt);
47     int m = (l+r)>>1;
48     if (L<=m) update(lson,L,R,sc);
49     if (R>m)  update(rson,L,R,sc);
50     putup(rt);
51 }
52 int query(int l,int r,int rt,int L,int R)
53 {
54     if (L<=l && r<=R)
55     {
56         return mn[rt];
57     }
58     putdown(rt);
59     int ret = s;
60     int m = (l+r)>>1;
61     if (L<=m) ret = min(ret,query(lson,L,R));
62     if (R>m)  ret = min(ret,query(rson,L,R));
63     return ret;
64 }
65 int main()
66 {
67     scanf("%d%d%d",&n,&s,&t);
68     build(1,n,1);
69     for (int a,b,c,i=1; i<=t; ++i)
70     {
71         scanf("%d%d%d",&a,&b,&c);
72         d = query(1,n,1,a,b-1);
73         if (d>=c)
74         {
75             printf("T
");
76             update(1,n,1,a,b-1,c);
77         }
78         else printf("N
");
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/mjtcn/p/7074564.html