HDU - 3577 Fast Arrangement

本文介绍了一种针对中国铁路庞大乘客量及站点数量设计的新型火车票查询系统。该系统利用线段树数据结构实现高效的区间操作,确保每位乘客仅能购买一张从A站到B站的票,并且每趟列车的乘客数不超过限定值k。

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.
InputThe 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.OutputFor 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,注意乘车区间是【l,r-1】.
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define ls o<<1
 5 #define rs o<<1|1
 6 #define lson L,mid,ls
 7 #define rson mid+1,R,rs
 8 #define middnf int mid=(L+R)>>1;
 9 #define ll long long
10 
11 using namespace std;
12 
13 const int maxn=1000005; 
14 int mx[maxn<<2],lazy[maxn<<2],ou[100005];
15 int n,m,x,y,z;
16 
17 int max(int a,int b)
18 {
19     return a>b?a:b;
20 }
21 
22 void pushup(int o)
23 {
24     mx[o]=max(mx[ls],mx[rs]);
25 }
26 
27 void pushdown(int o)
28 {
29     if(!lazy[o]) return;
30     lazy[ls] += lazy[o];
31     lazy[rs] += lazy[o];
32     mx[ls]+=lazy[o];
33     mx[rs]+=lazy[o];
34     lazy[o] = 0;
35 }
36 
37 void update(int l,int r,int L,int R,int o,int v){//区间更新
38     if(l<=L&&R<=r)
39     {
40         mx[o] += v;
41         lazy[o] += v;
42         return;
43     }
44     pushdown(o);
45     middnf;
46     if(l<=mid) update(l,r,lson,v);
47     if(r>mid) update(l,r,rson,v);
48     pushup(o);
49 }
50 
51 int query(int l,int r,int L,int R,int o)
52 {
53     if(l<=L&&R<=r) 
54         return mx[o];
55     pushdown(o);
56     middnf;
57     int ret = 0;
58     if(l<=mid) 
59         ret = max(ret,query(l,r,lson));
60     if(r>mid) 
61         ret = max(ret,query(l,r,rson));
62     return ret;
63 }
64 
65 int main()
66 {
67     char c[5];
68     int T,s=0;
69     scanf("%d",&T);
70     while(T--)
71     {  
72         int ss=0;
73         memset(mx,0,sizeof(mx));
74         memset(lazy,0,sizeof(lazy));
75         scanf("%d%d",&n,&m);
76         for(int i=1;i<=m;i++)
77         {
78             scanf("%d%d",&x,&y);
79             int num=query(x,y-1,1,1000005,1);
80             if(num<n)
81             {
82                 ou[ss++]=i;
83                 update(x,y-1,1,1000005,1,1);
84             }
85         }
86         printf("Case %d:\n",++s);
87         for(int i=0;i<ss;i++)
88         {
89             printf("%d ",ou[i]);
90         }
91         printf("\n\n");
92     }    
93     
94     
95     return 0;
96 }

 

转载于:https://www.cnblogs.com/xibeiw/p/7409788.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值