ural 1987. Nested Segments【离散化+贪心+线段树】
2016-09-07 21:28
525 查看
1987. Nested Segments
Time limit: 1.0 secondMemory 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 thenext 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 theyare 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 |
/* 题意:给你n条线段和m次询问,每次询问要求输出包含该点的最短的线段的编号, 如果不存在则输出-1 类型:离散化+贪心+线段树 分析:考虑到端点的范围在1e9以内,开不了那么大的数组,但是n的范围在1e5,所 以我们要先进行离散化,然后用线段树把区间的值修改成-1,然后按照线段 的长度从大到小排序,依次修改线段区间的值为线段的编号,这样就可以得 到每个点对应的编号了 */ #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<iostream> using namespace std; #define int_inf ((1<<31)-1) #define ll_inf ((1ll<<63)-1) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=110000*4*4; using namespace std; int n,Q; int lazy[maxn<<2]; int sum[maxn<<2]; void PushUp(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void PushDown(int rt,int m){ if(lazy[rt]){ lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; sum[rt<<1]=(m-(m>>1))*lazy[rt]; sum[rt<<1|1]=((m>>1))*lazy[rt]; lazy[rt]=0; } } void update(int L,int R,int c,int l,int r,int rt){ if(L<=l && r<=R){ lazy[rt]=c; sum[rt]=c*(r-l+1); return ; } PushDown(rt,r-l+1); int m=(l+r)>>1; if(L<=m) update(L,R,c,lson); if(R>m) update(L,R,c,rson); PushUp(rt); } int query(int L,int R,int l,int r,int rt){ if(L<=l &&r<=R){ return sum[rt]; } PushDown(rt,r-l+1); int m=(r+l)>>1; int ret=0; if(L<=m) ret+=query(L,R,lson); if(m<R) ret+=query(L,R,rson); return ret; } int bit[maxn]; int ask[maxn]; struct arr{ int a,b,idx; }p[maxn]; bool comp(const arr &e1,const arr &e2){ return (e1.b -e1.a )>(e2.b -e2.a ); } int sz; int get(int x){ int l=1,r=sz; while(r>=l){ int mid=(l+r)>>1; if(bit[mid]>x) r=mid-1; else if(bit[mid]<x) l=mid+1; else return mid; } } int main(){ //freopen("D:\\in.txt","r",stdin); while(scanf("%d",&n)!=EOF){ int cnt=1; memset(lazy,0,sizeof(lazy)); memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++){ scanf("%d%d",&p[i].a,&p[i].b); bit[cnt++]=p[i].a ; bit[cnt++]=p[i].b ; p[i].idx= i; } sort(p+1,p+n+1,comp); scanf("%d",&Q); for(int j=0;j<Q;j++){ scanf("%d",&ask[j]); bit[cnt++]=ask[j]; } sort(bit+1,bit+cnt+1); sz= unique(bit+1,bit+1+cnt)-(bit+1); update(1,sz,-1,1,sz,1); for(int i=1;i<=n;i++){ int newa=get(p[i].a); int newb=get(p[i].b); update(newa,newb,p[i].idx,1,sz,1); } for(int j=0;j<Q;j++){ int newask=get(ask[j]); printf("%d\n",query(newask,newask,1,sz,1)); } } }
相关文章推荐
- URAL 1987. Nested Segments(数学 & 线段树)
- URAL-1987 Nested Segments 线段树简单区间覆盖
- bnu36905 Nested Segments 离散化+线段树
- 【BNU Summer Training 2014.08.11】 Nested Segments (线段树,离散化)
- Ural1987-Nested Segments
- Codeforces-652D : Nested Segments(线段树)
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
- codeforces 652D D. Nested Segments(离散化+sort+树状数组)
- Ural 1019 A Line painting(线段树,成段更新离散化)
- Nested Segments codeforces 652D 树状数组 +离散化
- URAL - 1019 - Line Painting(离散化+线段树)
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- Nested Segments CodeForces 652D 树状数组+离散化
- CodeForces-652D:Nested Segments(树状数组+离散化)
- Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- Nested Segments CodeForces - 652D (树状数组,离散化)
- ural 1019. Line Painting 线段树 离散化
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
- CF#310-E - Case of Chocolate-(set+贪心)/(线段树+离散化)/(隐式线段树)