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个查询,每个查询给出当前所在的年份q,判断它最多能向前穿越多少年。他能穿越的条件是最少有2个航线可达那里。
解题思路:
我们可以简化一下,其实就是求它可以到达的第二小的年数,于是我们预处理下就可以了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=50000+1000;
const int inf=0x7fffffff;
int a[maxn];
int b[maxn];
vector<int> ve[maxn];
int main()
{
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q))
{
memset(a,0,sizeof(a));
int u,v;
for(int i=0; i<=n; i++)
ve[i].clear();
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
a[u]++;
ve[u].push_back(v);
}
int cur=0;
int ans=inf;
int minx=inf;
for(int i=n; i>0; i--)
{
cur+=a[i];
a[i]=cur;
for(int j=0;j<(int)ve[i].size();j++)
{
if(ve[i][j]<=minx)
{
ans=minx;
minx=ve[i][j];
}
else
{
ans=min(ans,ve[i][j]);
}
}
b[i]=ans;
}
int x;
ans=0;
for(int i=0;i<q;i++)
{
ans=0;
scanf("%d",&x);
if(a[x]>=2)
{
if(b[x]<=x)
ans=x-b[x];
}
printf("%d\n",ans);
}
}
return 0;
}