Fast Arrangement--线段树,lazy标记

题目   HDU--3577

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个人,给你一系列人的买票情况,问你那些乘客可以购买成功。注意,先买的人优先。

分析

每输入一组l,r,就查询一下,在l--r区间内,承载人数是否小于k,如果小于k,就可以承载,并且更新,否则,不可以承载。值得注意的是,在进行查询和更新之前,要进行r--操作。那么,为什么要r--呢?我是这样理解的:假如一列火车只可以搭载1个人,给了两个人的买票情况,分别是1->3,3->5,如果不进行r--操作,那么,这两个区间有重复的地方,所以只能第一个人买票,而第二个人不能买。但实际情况是,第一个人在3号站下去了,而第二个人在3号站上车,这完全是可以的。所以,在进行查询和更新之前,要进行r--操作。

知识点

线段树的建立、更新、查询以及lazy标记。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1e6;
int ans[N],cnt;//存放可以购买票的人的序号  
struct node{
	int left,right;
	int sum,lazy;
}tree[N<<2];
void pushup(int m)
{
	tree[m].sum=max(tree[m<<1].sum,tree[m<<1|1].sum);//取最大值  
}
void build(int m,int l,int r)
{
	tree[m].left=l;
	tree[m].right=r;
	tree[m].lazy=0;
	tree[m].sum=0;
	if(l==r)
		return ;
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void pushdown(int m)
{
	if(tree[m].lazy)
	{
		tree[m<<1].lazy+=tree[m].lazy;
		tree[m<<1|1].lazy+=tree[m].lazy;
		tree[m<<1].sum+=tree[m].lazy;
		tree[m<<1|1].sum+=tree[m].lazy;
		tree[m].lazy=0;
	}
}
void updata(int m,int l,int r,int val)
{
	if(tree[m].left==l&&tree[m].right==r)
	{
		tree[m].sum+=val;
		tree[m].lazy+=val;
		return ;
	}
	pushdown(m);//勿忘  
	int mid=(tree[m].left+tree[m].right)>>1;
	if(r<=mid)
		updata(m<<1,l,r,val);
	else if(l>mid)
		updata(m<<1|1,l,r,val);
	else
	{
		updata(m<<1,l,mid,val);
		updata(m<<1|1,mid+1,r,val);
	}
	pushup(m);
}
int query(int m,int l,int r)
{
	if(tree[m].left==l&&tree[m].right==r)
		return tree[m].sum;
	pushdown(m);	//勿忘  
	int mid=(tree[m].left+tree[m].right)>>1;
	if(r<=mid)
		return query(m<<1,l,r);
	else if(l>mid)
		return query(m<<1|1,l,r);
	else
		return max(query(m<<1,l,mid),query(m<<1|1,mid+1,r));
}
int main()
{
	int t,k,q,cas=1;
	scanf("%d",&t);
	while(t--)
	{
		memset(ans,0,sizeof(ans));
		cnt=0;
		scanf("%d%d",&k,&q);
		build(1,1,N+5);
		for(int i=1;i<=q;i++)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			r--;	//看分析  
			int sum=query(1,l,r);
			if(sum<k)
			{
				updata(1,l,r,1);
				ans[cnt++]=i;
			}
		}
		printf("Case %d:\n",cas++);
		for(int i=0;i<cnt;i++)
			printf("%d ",ans[i]);
		printf("\n\n");
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值