hdu3577 Fast Arrangement
来源:互联网 发布:全民淘宝客下载 编辑:程序博客网 时间:2024/04/29 14:26
题目:
Chinese always have the railway tickets problem because of its’ huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.
Input
The input contains servel test cases. The first line is the case number. In each test case:
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.
Output
For each test case, output three lines:
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.
Sample Input
1
3 6
1 6
1 6
3 4
1 5
1 2
2 4
Sample Output
Case 1:
1 2 3 5
题意:
一个火车只能载k个乘客,并且每个乘客仅仅只能从a->b买一张票,在任何时间每辆火车都不能载更多的乘客。一个人提前买的票将是有效的
思路:
也就是一个区间问题,某个乘客买a->b的票,需要询问a->b之间已有的最大乘客数量是否大于或等于k.如果不是,该乘客可以上车并且将a->b这个区间的所有乘客数+1.用线段树做,区间查询最大值,区间更新,注意乘客的实际乘车区间为[a,b);并且此题输出格式有毒,具体看代码。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define ls (rt<<1)#define rs (rt<<1|1)#define lson L,mid,ls#define rson mid+1,R,rs#define def_mid int mid=(L+R)>>1#define ll long long#define INF 0x3f3f3f3fusing namespace std;const int maxn = 1111111;int lazy[maxn<<2],maxs[maxn<<2];void pushup(int rt){ maxs[rt]=max(maxs[ls],maxs[rs]);}void pushlazy(int lz,int len,int rt){ maxs[rt]+=lz; lazy[rt]+=lz;}void pushdown(int L,int R,int rt){ if(lazy[rt]!=0) { def_mid; pushlazy(lazy[rt],mid-L+1,ls); pushlazy(lazy[rt],R-mid,rs); lazy[rt]=0; } return;}void build(int L,int R,int rt){ lazy[rt]=0; if(L==R) { maxs[rt]=0; return; } def_mid; build(lson); build(rson); pushup(rt);}void update(int l,int r,int L,int R,int rt,int k){ if(l<=L&&r>=R) { maxs[rt]+=k; lazy[rt]+=k; return; } pushdown(L,R,rt); def_mid; if(l<=mid) update(l,r,lson,k); if(r>mid) update(l,r,rson,k); pushup(rt);}ll querymax(int l,int r,int L,int R,int rt){ if(l<=L&&r>=R) return maxs[rt]; pushdown(L,R,rt); def_mid; ll ret=0; if(l<=mid) ret=max(ret,querymax(l,r,lson)); if(r>mid) ret=max(ret,querymax(l,r,rson)); return ret;}int main(){ int q,k,T; int ans[123456]; int top; int no=0; scanf("%d",&T); while(T--) { while(scanf("%d%d",&k,&q)!=EOF) { memset(lazy,0,sizeof(lazy)); memset(maxs,0,sizeof(maxs)); int l,r; build(1,maxn,1); top=0; for(int i=1;i<=q;i++) { scanf("%d%d",&l,&r); r--; if(querymax(l,r,1,maxn,1)<k) { ans[top++]=i; update(l,r,1,maxn,1,1); } } printf("Case %d:\n",++no); for(int i=0;i<top;i++) printf("%d ",ans[i]); printf("\n\n"); } } return 0;}
- hdu3577 Fast Arrangement
- hdu3577 Fast Arrangement
- hdu3577-Fast Arrangement 线段树区间覆盖
- HDU3577:Fast Arrangement(线段树区间更新+lazy)
- HDU 3577 Fast Arrangement
- HDU 3577 Fast Arrangement
- HDU 3577 Fast Arrangement
- 线段树 HDU 3577 Fast Arrangement
- hdu 3577 Fast Arrangement(线段树)
- HDU 3577 Fast Arrangement(线段树)
- HDU-3577-Fast Arrangement-区间更新
- Fast Arrangement-3577-线段树成段更新
- hdu 3577 Fast Arrangement(区间更新)
- hdu 3577 Fast Arrangement(线段树+lazy)
- HDU 3577 Fast Arrangement 线段树 区间更新
- HDU 3577 Fast Arrangement(区间覆盖+单点查询)
- Arrangement
- HDU 3577 Fast Arrangement(线段树功能:区间更新,查询区间的最大覆盖次数)
- 常用代码段
- JQuery DOM
- CSS 学习笔记——插入样式表
- ECharts, PHP, MySQL, Ajax, JQuery 实现前后端数据可视化时出现的问题
- 测试用例实例
- hdu3577 Fast Arrangement
- 【题解】NOIP普及组 金明的预算方案
- Android 学习资料收集
- HDFS日志json转存成parquet方案
- 多个Catch的异常处理
- 2017全球 C++ 及系统软件技术大会:一场大师智慧+实践干货的技术盛宴!
- 前端无处不在的事件
- setup_irq()与request_irq()
- websocket实例之多人聊天