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;
}

蒟蒻…
(。_。)难过

posted @ 2019-07-16 20:35  Un-Defined  阅读(5)  评论(0编辑  收藏  举报  来源