Twelves Monkeys

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 3888
64-bit integer IO format: %lld      Java class name: Main

 

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.

 

Source

Author

GAN, Tiansheng
 
解题:离线二分BIT
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct QU {
int id,year;
bool operator<(const QU &t)const {
if(year == t.year) return id < t.id;
return year > t.year;
}
} Q[maxn];
int c[maxn];
vector<int>g[maxn];
void add(int i,int val) {
while(i < maxn) {
c[i] += val;
i += i&-i;
}
}
int sum(int i,int ret = ) {
while(i > ) {
ret += c[i];
i -= i&-i;
}
return ret;
}
int query(int low = ,int high = maxn-,int ret = -) {
while(low <= high) {
int mid = (low + high)>>;
if(sum(mid) >= ) {
ret = mid;
high = mid-;
} else low = mid + ;
}
return ret;
}
int ans[maxn];
int main() {
int n,m,q,x,y;
while(~scanf("%d%d%d",&n,&m,&q)) {
for(int i = ; i < maxn; ++i) g[i].clear();
memset(c,,sizeof c);
memset(ans,,sizeof ans);
for(int i = ; i < m; ++i) {
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
for(int i = ; i < q; ++i) {
scanf("%d",&Q[i].year);
Q[i].id = i;
}
sort(Q,Q+q);
int now = ;
for(int i = n; i >= ; --i) {
for(int j = g[i].size()-; j >= ; --j)
add(g[i][j],);
if(Q[now].year == i) {
int idx = query(,i-);
if(idx == -) ans[Q[now].id] = ;
else ans[Q[now].id] = i - idx;
if(++now == q) break;
}
}
for(int i = ; i < q; ++i)
printf("%d\n",ans[i]);
}
return ;
}

ZOJ 3888 Twelves Monkeys的更多相关文章

  1. ZOJ 3888 Twelves Monkeys (预处理+优先队列)

    题目链接:ZOJ 3888 Twelves Monkeys 题意:题目描写叙述起来比較绕,直接讲案例 9 3 3 9 1 6 1 4 1 6 7 2 输入n,m,q.n限制了你询问的年份,m台时光机, ...

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

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...

  3. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...

  4. Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)

    Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal ...

  5. [主席树 强制在线]ZOJ3888 Twelves Monkeys

    题意:有n年,其中m年可以乘时光机回到过去,q个询问 下面m行,x,y 表示可以在y年穿越回x年, 保证y>x 下面q个询问, 每个询问有个年份k 问的是k年前面 有多少年可以通过一种以上($\ ...

  6. zoj 3888 线段树 ***

    卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...

  7. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. 新手git: ssh: connect to host localhost port 22: Connection refused

    由于gitlab上要git pull或者git clone,可是每次都出现这个问题.之前偶尔出现这个问题.可是仅仅是偶尔.这是为什么呢?然后就開始搜索网上的解决方式了. 这个问题搜索网上非常多答案.可 ...

  2. Linux命令(五)——磁盘操作及文件系统的管理

    文件系统是所有文件夹和文件的基础,磁盘是文件系统的基础,文件系统以磁盘为基础存储文件. 一.linux文件系统类型 1.ext扩展文件系统/ext2二级扩展文件系统/ext3日志式文件系统(默认) 2 ...

  3. Redis各种数据类型的使用场景

    Redis的六种特性 l Strings l Hashs l Lists l Sets l Sorted Sets l Pub/Sub Redis各特性的应用场景 Strings Strings 数据 ...

  4. luogu1908 逆序对 树状数组

    题目大意:对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对.求一段序列的逆序对数. 对于一个数组T,其一个点的值为值与该点下标相等的A序列中点的个数.对T维护一个树状数 ...

  5. js实现原生Ajax的封装及ajax原理详解

    原理及概念 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是一种用于创建快速动态网页的技术. 动态网页:是指可以通过服务器语言结合数 ...

  6. cdev_init和register_chrdev区别

    --- 01:include/linux/fs.h static inline int register_chrdev(unsigned int major, const char *name, co ...

  7. C#调用SMS短信接口,轻松搞定发送短信的任务。。。。

    首先我们需要去这里http://sms.webchinese.cn/申请一个账号和短信接口秘钥,在该网址下有许多语言的demo介绍,下面我主要为大家贴一个C#中的Helper类: using Syst ...

  8. 装饰模式(Decorator)C++实现

    装饰模式 层层包装,增强功能.这就是装饰模式的要旨!装饰器模式就是基于对象组合的方式,可以很灵活的给对象添加所需要的功能.它把需要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象. 意图: 动 ...

  9. 抽象工厂模式(AbsFactory)C++实现

    模式意图:提供一个创建一系列相关或相互依赖对象的接口,二无需指定他们具体的类. 效果: 分离了具体的类.     使  a.客户与类的实现分离  b.客户通过抽象接口操纵实例  c.产品的类名在实现中 ...

  10. Dynamic编程

    Dynamic Binding 动态绑定 Binding:解析Type,member,operation的过程. 动态绑定将Binding从编译时延迟到运行时进行. 场景 编译时,程序员知道特定的fu ...