HDU - 3577 Problem Description

HDU - 3577 Problem Description

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

Author
Louty (Special Thanks Nick Gu)

Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU

一趟火车能载k个人,会销售q种车票。如果当前车票所经过的路程中,已经存在满员的情况,就不能买当前这张车票。
转换成线段树

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int NUM = 1000000;

struct node{
	int data,left,right,lazy;//data存当前节点所代表的线段的最大值,即 这一段路程中乘客最多的一段路已经有多少名乘客 
	int mid(){return (right - left) / 2 + left; }
};

int n,m;

struct seg_tree{
	node tree[4*NUM];
	void bulid(int l,int r,int x)
	{
		tree[x].left = l;
		tree[x].right = r;
		tree[x].data = 0;
		tree[x].lazy = 0;
		if (tree[x].left == tree[x].right) return;
		int mid = tree[x].mid();
		bulid(l,mid,x * 2);
		bulid(mid + 1,r,x * 2 + 1);
	}
	void insert(int st,int ed,int x)
	{
		if (tree[x].left >= st && tree[x].right <= ed) //当前节点所代表的线段在要更新的范围内部 
		{
			tree[x].lazy++;
			return;
		}
		//如果当前节点所代表的的线段存在一部分在要更新的范围之外 
		if (tree[x].lazy != 0) //lazy标志下移 
		{
			tree[x * 2].lazy += tree[x].lazy;
			tree[x * 2 + 1].lazy += tree[x].lazy;
			tree[x].lazy = 0;
		}
		int mid = tree[x].mid();
		if (mid >= st) insert(st,ed,x * 2);
		if (mid < ed) insert(st,ed,x * 2 + 1);
		tree[x].data = max(tree[x * 2].data + tree[x * 2].lazy,tree[x * 2 + 1].data + tree[x * 2 + 1].lazy);//更新tree节点
	}
	int query(int st,int ed,int x)
	{
		if (tree[x].left >= st && tree[x].right <= ed) return tree[x].data + tree[x].lazy; 
		if (tree[x].lazy != 0)
		{
			tree[x * 2].lazy += tree[x].lazy;
			tree[x * 2 + 1].lazy += tree[x].lazy;
			tree[x].lazy = 0;
		}
		int mid = tree[x].mid();
		int ans = 0;
		if (mid >= st) ans = query(st,ed,x * 2);
		if (mid < ed) ans = max(ans,query(st,ed,x * 2 + 1));
		tree[x].data = max(tree[x * 2].data + tree[x * 2].lazy,tree[x * 2 + 1].data + tree[x * 2 + 1].lazy);
		return ans;
	}
}seg;

int main()
{
	int p,count = 0;
	cin>>p;
	while(p--)
	{
		printf("Case %d:\n",++count); 
		scanf("%d%d",&n,&m);
		seg.bulid(1,NUM,1) ;
		for (int i = 1,u,v;i<=m;i++)
		{
			scanf("%d%d",&u,&v);
			if (seg.query(u,v - 1,1) < n)//如果车票所经过的路程不存在满员的情况 
			{
				seg.insert(u,v - 1,1);
				printf("%d ",i);
			}
		}	
		printf("\n\n");
	}
	return 0;
}

蒟蒻…
(。_。)难过

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值