URAL1987. Nested Segments Segment Tree

Source: Internet
Author: User

1987. Nested Segmentstime limit:1.0 Second
Memory limit:64 MB
You are given NSegments on a straight line. For each pair of segments it's known that they either has no common points or all points of one segment belong to the SE Cond segment. Then mQueries follow. Each query represents a at the line. For each query, your task was to find the segment of the minimum length, and to which this point belongs. Inputthe first line contains an integer NThat's the number of segments (1≤ N≤105). I' Th of the next NLines contains integers a Iand b IThat is the coordinates of endpoints of the I' TH segment (1≤ a I< b I≤109). The segments is ordered by non-decreasing a I, and when a I= a JThey is ordered by decreasing length. All segments is distinct. The next line contains an integer mThat's the number of queries (1≤ m≤105). J' Th of the next mLines contains an integer C JThat's the coordinate of the point (1≤ C J≤109). The queries is ordered by non-decreasing C J. Outputfor each query output the number of the corresponding segment on a. If the point does not belong to any segment, output "-1". The segments is numbered from 1 to NIn order they is given in the input. Sample
input Output
32 102 35 7111234567891011
-1221333111-1




Test instructions: There are n segments, numbered 1-n, and each segment occupies a-B. m query, query C in the paragraph, the shortest length is the number of paragraphs. If no segment occupies, output-1.
First, all the numbers exist in the array, to disperse. The ID is then updated to the tree by the length of the segment, from long to short. Then just query the fixed point in the tree for the ID of the line.

#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1#define LL intconst int maxn = 811111; LL id[maxn<<2];//records which section of void Pushup (int rt) {if (id[rt<<1] = = id[rt<<1|1]) The point belongs to id[rt]=id[rt<<1]; else ID[RT] =-1;} void pushdown (int rt,int m) {if (id[rt]!=-1) {id[rt<<1] = id[rt];id[rt<<1|1] = Id[rt];}} void build (int l,int R,int RT) {if (L = = r) {Id[rt]=-1;return;} int m = (L + R) >> 1;build (Lson); build (Rson); Pushup (RT);} void update (int l,int r,int c,int l,int r,int RT) {if (L <= l && R <= R) {Id[rt] = C; return;}  Pushdown (RT, r-l + 1), int m = (L + R) >> 1;if (l <= m) update (L, R, C, Lson), if (M < R) Update (L, R, C , Rson); Pushup (RT);} LL query (int l,int r,int l,int r,int RT) {if (L <= l && R <= R) {return ID[RT];} Pushdown (RT, r-l + 1); int m = (L + r) >> 1;  LL ret = 0;if (l <= m) ret + = query (l, R, Lson), if (M < r) ret + = query (l, R, Rson); return ret;} Map<int, inT >my;struct bian{int l,r,len;int ID;}; int num[800010];//is used to record all the inputs for de-discretization bian edge[100010];int qus[100010];//record Inquiry int cmp (Bian A,bian b) {return a.len< B.len;} int Arr[800010];int Main () {int i,a,b;int t,cas=1;int n;int ji;while (scanf ("%d", &n)!=eof) {my.clear (); Ji=0;for (i=0 ; i<n;i++) {scanf ("%d%d", &a,&b); edge[i].l=a;edge[i].r=b;edge[i].len=b-a;edge[i].id=i+1;num[ji++]=a; Num[ji++]=b;} int m;scanf ("%d", &m), for (i=0;i<m;i++) {scanf ("%d", &qus[i]); num[ji++]=qus[i];} Sort (edge,edge+n,cmp); sort (num,num+ji); int ji2=1;for (i=1;i<ji;i++) {if (num[i]!=num[i-1]) num[ji2++]=num[i];} Ji=ji2;for (i=0;i<ji;i++) my[num[i]]=i+1; Build (1,ji,1); for (i=n-1;i>=0;i--)//side length order from large to small, so from the back forward, the length of the edge will overwrite {int l=my[edge[i].l];int r=my[edge[i].r];update (l,r,edge[i].id,1 , ji,1);} for (i=0;i<m;i++) printf ("%d\n", Query (my[qus[i]],my[qus[i]],1,ji,1)); }return 0;}







URAL1987. Nested Segments Segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.