码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3888 Twelves Monkeys 二分+线段树维护次小值

时间:2015-07-27 23:01:03      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:二分查找   线段树   

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3888


Twelves Monkeys

Time Limit: 5 Seconds      Memory Limit: 32768 KB

James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth‘s surface had been contaminated by a virus so deadly that it forced the survivors to move underground. In the years that followed, scientists had engineered an imprecise form of time travel. To earn a pardon, Cole allows scientists to send him on dangerous missions to the past to collect information on the virus, thought to have been released by a terrorist organization known as the Army of the Twelve Monkeys.

The time travel is powerful so that sicentists can send Cole from year x[i] back to year y[i]. Eventually, Cole finds that Goines is the founder of the Army of the Twelve Monkeys, and set out in search of him. When they find and confront him, however, Goines denies any involvement with the viruscan. After that, Cole goes back and tells scientists what he knew. He wants to quit the mission to enjoy life. He wants to go back to the any year before current year, but scientists only allow him to use time travel once. In case of failure, Cole will find at least one route for backup. Please help him to calculate how many years he can go with at least two routes.

Input

The input file contains multiple test cases.

The first line contains three integers n,m,q(1≤ n ≤ 50000, 1≤ m ≤ 50000, 1≤ q ≤ 50000), indicating the maximum year, the number of time travel path and the number of queries.

The following m lines contains two integers x,y(1≤ y  x ≤ 50000) indicating Cole can travel from year x to year y.

The following q lines contains one integers p(1≤ p ≤ n) indicating the year Cole is at now

Output

For each test case, you should output one line, contain a number which is the total number of the year Cole can go.

Sample Input

9 3 3
9 1
6 1
4 1
6
7
2

Sample Output

5
0
1

Hint

6 can go back to 1 for two route. One is 6-1, the other is 6-7-8-9-1. 6 can go back to 2 for two route. One is 6-1-2, the other is 6-7-8-9-1-2.


题意:

有m个可以穿越回过去的机器,但是只能用一个,还有q个询问。

输出,当在p点时, 1到p-1  这些点有多少点可以通过两种方式穿越回去。


做法:

首先要穿越,必须要机器的r 大于等于p。这个是二分排序后去找的。

然后用线段树找出这些r大于等于p的机器的l的次小值。

很明显 次小l 后的所有点都可以用  最小l的机器,和次小l的机器 穿越回那些点。

所以 点的总数是  max(p-l,0)。


数据有问题有m==0 的输入,所以多次SF了,所以加了 m==0的特判。

也可以按n来建树就不存在这个问题了。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

struct point 
{
	int l,r;
	bool operator <(const point &b)const
	{
		return r<b.r;
	}
};
 point pp[50111]; 

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL int
#define inf 1000000
const int maxn = 50010;
pair<int,int> max2(pair<int,int> a,pair<int,int> b)
{
	pair<int,int> tem;
	if(a.first==b.first)
	{
		tem.first=tem.second=a.first;
	}
	else if(a.first>b.first)
	{
		tem.first=b.first;
		tem.second=min(a.first,b.second);
	}
	else if(a.first<b.first)
	{
		tem.first=a.first;
		tem.second=min(b.first,a.second);
	}

	return tem;
}


pair<int,int>  big[maxn<<2]; 
void PushUp(int rt) {
	big[rt] = max2(big[rt<<1] ,big[rt<<1|1]);
} 
int kk;
void build(int l,int r,int rt) { 
	big[rt].second=inf; 
	if (l == r) { 
		big[rt].first=pp[kk++].l;
		return ;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUp(rt);
} 

pair<int,int> query(int L,int R,int l,int r,int rt) {
	if (L <= l && r <= R) {
		return  big[rt];
	}

	int m = (l + r) >> 1;

	if(L<=m&&m<R)
		return max2(query(L , R , lson),query(L , R , rson));
	if (L <= m)  
		return query(L , R , lson); 
	if (m < R)  
		return query(L , R , rson); 
}
 

int cmp(point a,point b)
{
	return a.r<b.r;
}
  
int main() 
{
	int i,a,b; 
	int m,q;
	int n; 
	while(scanf("%d%d%d",&n,&m,&q)!=EOF)
	{  
		//if(m==0) 加了就超时 感觉有m==0的案例  
			//while(1);
		kk=1; 
		int ge=0;
		for(i=1;i<=m;i++) 
			scanf("%d%d",&pp[i].r,&pp[i].l);      
		sort(pp+1,pp+m+1); 
		if(m)//之前m等于0 也建树 被坑死了
		build(1,m,1); 
		
		
		while(q--)
		{
			int p;
			scanf("%d",&p); 

			if(!m)
			{
				puts("0");
				continue;
			}
			point tt;
			tt.r=p;
			int wei=lower_bound(pp+1,pp+m+1,tt)-pp; 
			if(wei==m+1)//找不到
			{
				puts("0");
				continue;
			} 
			pair<int,int> tem,t2;
			tem=query(wei,m,1,m,1);  
			if(tem.second==inf)
				puts("0");
			else
				printf("%d\n",max(0,p-tem.second));
		}  
	}
	return 0;
}








版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 3888 Twelves Monkeys 二分+线段树维护次小值

标签:二分查找   线段树   

原文地址:http://blog.csdn.net/u013532224/article/details/47092047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!