ural1987 Nested Segments

Nested Segments

Time limit: 1.0 second
Memory limit: 64 MB
You are given n segments on a straight line. For each pair of segments it is known that they either have no common points or all points of one segment belong to the second segment.
Then m queries follow. Each query represents a point on the line. For each query, your task is to find the segment of the minimum length, to which this point belongs.

Input

The first line contains an integer n that is the number of segments (1 ≤ n ≤ 105). i’th of the next n lines contains integers ai and bi that are the coordinates of endpoints of the i’th segment (1 ≤ ai < bi ≤ 109). The segments are ordered by non-decreasing ai, and when ai = aj they are ordered by decreasing length. All segments are distinct. The next line contains an integer m that is the number of queries (1 ≤ m ≤ 105). j’th of the next m lines contains an integer cj that is the coordinate of the point (1 ≤ cj ≤ 109). The queries are ordered by non-decreasing cj.

Output

For each query output the number of the corresponding segment on a single line. If the point does not belong to any segment, output “-1”. The segments are numbered from 1 to n in order they are given in the input.

Sample

inputoutput
3
2 10
2 3
5 7
11
1
2
3
4
5
6
7
8
9
10
11
-1
2
2
1
3
3
3
1
1
1
-1

 

分析:离散化+线段树;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=3e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,q,l[maxn>>1],r[maxn>>1],val[maxn>>1],p[maxn],query[maxn];
struct Node
{
    pii Min , lazy;
} T[maxn<<2];

void PushUp(int rt)
{
    T[rt].Min = min(T[rt<<1].Min, T[rt<<1|1].Min);
}

void PushDown(int L, int R, int rt)
{
    int mid = (L + R) >> 1;
    pii t = T[rt].lazy;
    T[rt<<1].Min = T[rt<<1|1].Min = t;
    T[rt<<1].lazy = T[rt<<1|1].lazy = t;
    T[rt].lazy = mp(0,-1);
}

void Build(int L, int R, int rt)
{
    if(L == R)
    {
        T[rt].Min = mp(inf,-1);
        return ;
    }
    int mid = (L + R) >> 1;
    Build(Lson);
    Build(Rson);
    PushUp(rt);
}

void Update(int l, int r, pii v, int L, int R, int rt)
{
    if(l==L && r==R)
    {
        T[rt].lazy = T[rt].Min =  v;
        return ;
    }
    int mid = (L + R) >> 1;
    if(T[rt].lazy.fi) PushDown(L, R, rt);
    if(r <= mid) Update(l, r, v, Lson);
    else if(l > mid) Update(l, r, v, Rson);
    else
    {
        Update(l, mid, v, Lson);
        Update(mid+1, r, v, Rson);
    }
    PushUp(rt);
}

pii Query(int l, int r, int L, int R, int rt)
{
    if(l==L && r== R)
    {
        return T[rt].Min;
    }
    int mid = (L + R) >> 1;
    if(T[rt].lazy.fi) PushDown(L, R, rt);
    if(r <= mid) return Query(l, r, Lson);
    else if(l > mid) return Query(l, r, Rson);
    return min(Query(l, mid, Lson) , Query(mid + 1, r, Rson));
}

int main()
{
    int i,j;
    scanf("%d",&n);
    j=1;
    rep(i,1,n)scanf("%d%d",&l[i],&r[i]),p[j++]=l[i],p[j++]=r[i],val[i]=r[i]-l[i]+1;
    scanf("%d",&q);
    rep(i,1,q)
    {
        scanf("%d",&query[i]);
        p[j++]=query[i];
    }
    sort(p+1,p+j+1);
    int num=unique(p+1,p+j+1)-p-1;
    rep(i,1,n)
    {
        l[i]=lower_bound(p+1,p+num+1,l[i])-p-1;
        r[i]=lower_bound(p+1,p+num+1,r[i])-p-1;
    }
    rep(i,1,q)
        query[i]=lower_bound(p+1,p+num+1,query[i])-p-1;
    Build(1,num,1);
    rep(i,1,n)Update(l[i],r[i],mp(val[i],i),1,num,1);
    rep(i,1,q)
    {
        printf("%d\n",Query(query[i],query[i],1,num,1).se);
    }
    //system("Pause");
    return 0;
}
posted @ 2016-08-19 11:20  mxzf0213  阅读(395)  评论(0编辑  收藏  举报