卡n^2,用线段树降到nlogn

记录每个点上所覆盖线段的次小值,保证能有两条路径能走

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root 1,n,1
#define mid ((l+r)>>1)
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int N=;
int n,m,tt;
int a[N<<],b[N<<];
void pushdown(int rt)
{
//printf("pd:%d %d %d\n",a[rt],b[rt],rt);
if(b[rt]!=)
{
if(b[rt]>=a[rt<<]&&b[rt]<b[rt<<])
{
b[rt<<]=b[rt];
}
if(b[rt]<a[rt<<])
{
a[rt<<]=b[rt<<];
a[rt<<]=b[rt];
}
if(b[rt]>=a[rt<<|]&&b[rt]<b[rt<<|])
{
b[rt<<|]=b[rt];
}
if(b[rt]<a[rt<<|])
{
b[rt<<|]=a[rt<<|];
a[rt<<|]=b[rt];
}
b[rt]=;
}
/*if(rt==4)
{
printf("%d %d\n",a[rt<<1],b[rt<<1]);
}*/
if(a[rt]!=)
{
if(a[rt]>=a[rt<<]&&a[rt]<b[rt<<])
{
b[rt<<]=a[rt];
}
if(a[rt]<a[rt<<])
{
b[rt<<]=a[rt<<];
a[rt<<]=a[rt];
}
if(a[rt]>=a[rt<<|]&&a[rt]<b[rt<<|])
{
b[rt<<|]=a[rt];
}
if(a[rt]<a[rt<<|])
{
b[rt<<|]=a[rt<<|];
a[rt<<|]=a[rt];
}
a[rt]=;
}
//printf("%d %d ++%d++\n",a[rt<<1],b[rt<<1],rt<<1);
}
void build(int l,int r,int rt)
{
a[rt]=b[rt]=;
if(l==r)
{
a[rt]=b[rt]=l;
return;
}
build(lson);
build(rson);
}
void update(int L,int R,int val,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
int x=val;
if(b[rt]>x&&x>a[rt])
{
b[rt]=x;
}
if(x<b[rt])
{
b[rt]=a[rt];
a[rt]=x;
}
return;
}
pushdown(rt);
if(L<=mid) update(L,R,val,lson);
if(R>mid) update(L,R,val,rson);
}
int query(int pos,int l,int r,int rt)
{
//printf("%d %d %d %d %d\n",a[rt],b[rt],l,r,rt);
if(l==r)
{
return b[rt];
}
pushdown(rt);
if(pos<=mid) return query(pos,lson);
else return query(pos,rson);
}
int main()
{
int i,j,k,ca=,q;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
int x,y;
build(root);
for(i=;i<m;i++)
{
scanf("%d%d",&y,&x);
update(x,y,x,root); //记录次小x
}
while(q--)
{
scanf("%d",&x);
printf("%d\n",x-query(x,root));
}
}
}

zoj 3888 线段树 ***的更多相关文章

  1. ZOJ 3279-Ants(线段树)

    传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds      Memory Limit: 32768 KB echo is a curious and cleve ...

  2. Prime Query (ZOJ 3911 线段树)

    Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a sequen ...

  3. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  4. ZOJ 3911 线段树

    题意:有N个数字,M个操作,然后回答每个Q开头的询问 操作形式: A val pos:在pos位置上+val Q l r:询问l~r之间有多少个质数 R val l r:把l~r之间的数字替换成val ...

  5. HDU 1199 &amp;&amp; ZOJ 2301 线段树离散化

    一段长度未知的线段.一种操作:a b c ,表示区间[a,b]涂为颜色C,w代表白色,b代表黑色,问终于的最长连续白色段,输出起始位置和终止位置 离散化处理.和寻常的离散化不同,须要把点化成线段.左闭 ...

  6. zoj 2706 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1706 trick:关于正数和负数的整除问题,正数整除是自动向下取整的 ...

  7. ZOJ 3299 线段树 离散化

    本来是个很简单的题目,难住我的主要是这么几点 1.它所有的点都是坐标,不是实际的砖块,1,3指的是1-2 2-3的砖块...后来就是用1 代表1-2 ,2代表2-3.....,这样的话,每次读入的数据 ...

  8. 群赛 ZOJ3741(dp) ZOJ3911(线段树)

    zoj3741 简单dp.wa了两个小时,中间改了好多细节.后来还是不对,参考了别人的代码,发现一个致命问题,初始化的时候,不是每种状态都能直接达到的.初始化成-1. (题目有个小坑,0<=L& ...

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

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

随机推荐

  1. 虚函数列表: 取出方法 // 虚函数工作原理和(虚)继承类的内存占用大小计算 32位机器上 sizeof(void *) // 4byte

    #include <iostream> using namespace std; class A { public: A(){} virtual void geta(){ cout < ...

  2. Unity3d《Shader篇》法线贴图

    效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...

  3. ios xib 中的 size class

    需要阅读UITraitCollection的说明文档,先截图如下: 今天说说xib中的size class的简单设置,先看图 一共有9个小块,水平方向代表width,垂直方向代表height. 对于w ...

  4. iOS 关于UIWindow的理解

    Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...

  5. js中修改标签的hidden属性

    hidden属性在html5中,只要存在,就是隐藏效果,而不论值为多少 要显示元素,要删除hidden属性,而不是设置为false <script type="text/javascr ...

  6. 用C#读取txt文件的方法

    1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System ...

  7. 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation

    查看网址:http://www.open-open.com/lib/view/open1411443332703.html

  8. HTML5基本标签、样式

    感觉在Sublime Text3中写起来比较方便~~ 将HTML5中要用到的基本标签全部放在了一起,没有好好的整理,为了自己记忆的方便,就先这样写下来了~~ <!DOCTYPE html> ...

  9. Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案

    比如,当我要添加一个信用卡的时候,我需要信用卡的CardType, 这些数据是存在数据库中的,我要先通过action 的一个 create方法,取到信用卡的类型: public String crea ...

  10. 分页Bean终极封装

    package org.guangsoft.vo; import java.util.List; public class Page { private Integer pageNum; privat ...