zoj 3888 Twelves Monkeys 二分+线段树维护次小值
链接: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 二分+线段树维护次小值的更多相关文章
- 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台时光机, ...
- 【uoj#164】[清华集训2015]V 线段树维护历史最值
题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...
- 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值
题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- CF213E Two Permutations 线段树维护哈希值
当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...
- [CSP-S模拟测试]:椎(线段树维护区间最值和单调栈)
题目描述 虽不能至,心向往之. $Treap=Tree+Heap$ 椎$=$树$+$堆 小$\pi$学习了计算机科学中的数据结构$Treap$. 小$\pi$知道$Treap$指的是一种树. 小$\p ...
- SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...
- ZOJ 3888 Twelves Monkeys
Twelves Monkeys Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- LOJ 164 【清华集训2015】V——线段树维护历史最值
题目:http://uoj.ac/problem/164 把操作改成形如 ( a,b ) 表示加上 a 之后对 b 取 max 的意思. 每个点维护当前的 a , b ,还有历史最大的 a , b 即 ...
随机推荐
- 《c程序设计语言》读书笔记-4.12-递归整数转字符串
#include <stdio.h> #include <math.h> #include <stdlib.h> void itoa_num(int n, char ...
- Java代码实现真分页
在JavaWeb项目中,分页是一个非常常见且重要的一个小方面.本次作为记载和学习,记录项目中出现的分页并做好学习记录.在这里,用的是SSH框架.框架可以理解如下图: 在JSP页面,描写的代码如下: & ...
- es6+最佳入门实践(2)
2.解构赋值 2.1.什么是解构赋值? 什么是解构赋值?这里的关键字还是赋值,这是说如何去赋值的问题,这里说的解构可以理解为解散重新构造,所以解构赋值可以理解为解散重新构造后进行赋值,通常是左边一种结 ...
- git隐藏文件复制
从网上down的开源项目,如何添加到自己的github上呢? 问题:直接复制老项目到自己的目录,隐藏的.git文件不会被复制过去,就算是执行cp命令,也不会复制!导致项目运行会出错!! 解决: ...
- C#操作windows事件日志项
/// <summary> /// 指定事件日志项的事件类型 /// </summary> public enum EventLogLevel { /// <summar ...
- CSS技巧----DIV+CSS规范命名大全集合
网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DIV CSS命名规则CSS命名大全内容篇. 常用DIV+CSS命名大全集合,即CSS命名规则 D ...
- 莫队 [洛谷2709] 小B的询问[洛谷1903]【模板】分块/带修改莫队(数颜色)
莫队--------一个优雅的暴力 莫队是一个可以在O(n√n)内求出绝大部分无修改的离线的区间问题的答案(只要问题满足转移是O(1)的)即你已知区间[l,r]的解,能在O(1)的时间内求出[l-1, ...
- UVA 104 Arbitrage
动态规划类似FLOYD dp[i][j][k] 表示第i个点经过K次到达j点能获得的最大利润 #include <map> #include <set> #include &l ...
- android的百度地图开发(一)
1,注册百度开发者账号 2,申请key ,注意开发版SH和发布版的SH 获取开发版SHA1: 输入命令:keytool -list -v -keystore debug.keystore,回车输入 ...
- spring3.2事物配置异常
异常如下: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b4c1d7: defining beans [ ...