ural1987 Nested Segments

时间:2023-03-09 06:55:11
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

input output
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+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,q,l[maxn>>],r[maxn>>],val[maxn>>],p[maxn],query[maxn];
struct Node
{
pii Min , lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].Min = min(T[rt<<].Min, T[rt<<|].Min);
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
pii t = T[rt].lazy;
T[rt<<].Min = T[rt<<|].Min = t;
T[rt<<].lazy = T[rt<<|].lazy = t;
T[rt].lazy = mp(,-);
} void Build(int L, int R, int rt)
{
if(L == R)
{
T[rt].Min = mp(inf,-);
return ;
}
int mid = (L + R) >> ;
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) >> ;
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+, 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) >> ;
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 + , r, Rson));
} int main()
{
int i,j;
scanf("%d",&n);
j=;
rep(i,,n)scanf("%d%d",&l[i],&r[i]),p[j++]=l[i],p[j++]=r[i],val[i]=r[i]-l[i]+;
scanf("%d",&q);
rep(i,,q)
{
scanf("%d",&query[i]);
p[j++]=query[i];
}
sort(p+,p+j+);
int num=unique(p+,p+j+)-p-;
rep(i,,n)
{
l[i]=lower_bound(p+,p+num+,l[i])-p-;
r[i]=lower_bound(p+,p+num+,r[i])-p-;
}
rep(i,,q)
query[i]=lower_bound(p+,p+num+,query[i])-p-;
Build(,num,);
rep(i,,n)Update(l[i],r[i],mp(val[i],i),,num,);
rep(i,,q)
{
printf("%d\n",Query(query[i],query[i],,num,).se);
}
//system("Pause");
return ;
}