【CODEVS1291】火车线路
2016-01-10 09:20
441 查看
Description
某列火车行使在C个城市之间(出发的城市编号为1,结束达到的城市的编号为C),假设该列火车有S个座位,现在有R笔预订票的业务。现在想对这R笔业务进行处理,看哪些预定能满足,哪些不能满足。一笔预定由O、D、N三个整数组成,表示从起点站O到目标站D需要预定N个座位。一笔预定能满足是指该笔业务在行程范围内有能满足的空座位,否则就不能满足。一笔业务不能拆分,也就是起点和终点站不能更改,预定的座位数目也不能更改。所有的预定需求按给出先后顺序进行处理。
请你编写程序,看那些预定业务能满足,那些不能满足。
Input
输入文件中的第一行为三个整数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
对第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; }
相关文章推荐
- 我的“家用投影机无屏电视”初体验
- 【NCRE】---登录窗体应用“单例模式”
- 大规模程序设计笔记(一)
- LeetCode - Same Tree
- Win10 Mobile版和PC版系统修复内容有什么区别?微软告诉你答案
- 浅析计算机网络(5)-选路算法
- php的调试工具xdebug
- EL表达式(1)
- 分享有关jQuery中animate、slide、fade等动画的连续触发、滞后反复执行的bug
- No-Touch Integration 在SharePoint中使用社区支持的Silverlight应用程序
- activity_main.xml: java.lang.NullPointerException
- 理解Angular数据双向绑定
- (转)你了解System.out.println()的真正含义吗?
- 微软下周(1月12日)将停止支持Win8 请速升级Win8.1或Win10
- 一起talk C栗子吧(第九十五回:C语言实例--使用共享内存进行进程间通信一)
- C#EasyHook例子C# Hook 指定进程C#注入指定进程 z
- BZOJ 3365 Distance Statistics 点分治
- SpriteKit 1-SKScene
- 从此妈妈再也不用担心我的View冲突了
- Android USB Connections Explained: MTP, PTP, and USB Mass Storage