ZOJ 3888

来源:互联网 发布:正在识别无网络访问 编辑:程序博客网 时间:2024/06/06 09:35
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 yearx[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 yearCole can go.

Sample Input

9 3 39 16 14 1672

Sample Output

501

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. 



解释一个案例吧,给你n,m,q。n是编号,下面m行,每行两个数,代表可以从第9年回到第1年,第6年会到第一年。...类推。。  在下面q行,每行一个数,问从这年回到过去的有两种方法最多经过的年份是多少年,比如6,可以等三年,从第9年回到1,2,3,4,5, 也可以直接回到6 1,2,3,4,5。1,2,3,4,5都出现了两次。所以输出5.

做法:线段树维护次小值,区间更新。


#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#define clc(arr, val)    memset(arr, val, sizeof(arr))#define FOR(i,a,b)  for(int i=a;i<=b;i++)#define IN   freopen ("in.txt" , "r" , stdin);#define OUT  freopen ("out.txt" , "w" , stdout);typedef long long  LL;typedef unsigned long long  ULL;const int MAXN = 60127;const int MAXM = 311231;const int N = 60127;const int M = 200000;const int INF = 0x3f3f3f3f;const LL mod = (LL)1<<32;const double eps= 1e-8;const double pi=acos(-1.0);#define lson l,m, rt<<1#define rson m+1,r,rt<<1|1int ans[MAXN<<2],kao[MAXN<<2][2];int lll,rrr;struct node{    int x,y;};int push(int rt){    ans[rt]=ans[rt<<1]+ans[rt<<1|1]; //某一年被更新过几次累加到上一个节点    int a[4];    a[0]=kao[rt<<1][0];    a[1]=kao[rt<<1][1];    a[2]=kao[rt<<1|1][0];//rt是最小,那么rt<<1|1必然是次小    a[3]=kao[rt<<1|1][1];    sort(a,a+4);    kao[rt][0]=a[0];//返回年数最小值    kao[rt][1]=a[1];//返回年数次小值}node gao(node xx,node yy) //每次不断的找最小值和次小值,赋给res,并且xx.y为次小值。{    int a[4];    a[0]=xx.x;    a[1]=xx.y;    a[2]=yy.x;    a[3]=yy.y;    sort(a,a+4);    xx.x=a[0];//    xx.y=a[1];    return xx;}int update(int R,int a,int l,int r,int rt)//更新rrr到1的区间,{    if(l==r)    {        ans[rt]++;   //更新的时候,rrr到1的区间内下标加1.        if(kao[rt][0]>=lll)        {            swap(kao[rt][0],kao[rt][1]);            kao[rt][0]=lll;   //这边是查找所能返回的最小的年数。        }        else if(kao[rt][1]>lll)            kao[rt][1]=lll;//这边是查找所能返回的最小的年数。        return 0;    }    int m=(l+r)>>1;    if(R<=m) update(R,a,lson);    else update(R,a,rson);    push(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)        return ans[rt]; //返回某一年被更新过几次。    int m=(l+r)>>1;    int res=0;    if(L<=m) res=res+query(L,R,lson);    if(R>m) res=res+query(L,R,rson);    return res;}node query1(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        node c;        c.x=kao[rt][0];        c.y=kao[rt][1];        return c;   //找到返回年数的最小值和次小值赋给res。    }    int m=(l+r)>>1;    node res,ree;    res.x=INF,res.y=INF;    ree.x=INF,ree.y=INF;    if(L<=m) res=query1(L,R,lson);    if(R>m) ree=query1(L,R,rson);    res=gao(res,ree);//再次赋值。    return res;}int build(int l,int r,int rt){    kao[rt][0]=kao[rt][1]=INF; //一维二维初始为无穷大。    ans[rt]=0;  //所有节点初始为0    if(l==r)    {        return 0; //线段树的所有值初始为0    }    int m=(l+r)>>1;    build(lson);    build(rson);    return 0;}int main(){    int n,m,q;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        build(1,n,1);        for(int i=0; i<m; i++)        {            scanf("%d%d",&rrr,&lll);            update(rrr,1,1,n,1);//更新rrr到1的区间        }        while(q--)        {            int x;            scanf("%d",&x);            int c=query(x,n,1,n,1); //查询x到n的区间            if(c<2)                printf("0\n");            else            {                node aa=query1(x,n,1,n,1); //将次小值的结果赋给aa,并且aa.y为次小值                int ccc=max(x-aa.y,0);                printf("%d\n",ccc);            }        }    }    return 0;}


0 0
原创粉丝点击