Gym 100971D laying Cables monotonic stack

Source: Internet
Author: User

Description

One-dimensional country hasNCities, theI-th of which is located at the point x i  and has population  p i , and All  x I , as well as All  p i , is distinct. When one-dimensional country got the Internet, it is decided to place the main server in the largest city, and to connect Any other city  J  to the City  K  that has bigger population than  J  and was the closest to it (if there was M Any such cities, the largest one should be chosen). City  K  is called a parent of City  J  in this case.

Unfortunately, the Ministry of Communications got stuck in determining from where and to where the Internet cables should Be laid, and the population of the country are suffering. So you should solve the problem. For every city, find's parent city.

Input

The first line contains a single integer n(1≤ n ≤200000)-the number of cities.

Each of the next n lines contains II space-separated integers xi and pI(1≤ xi, pi ≤109)-the coordinate and T He population of theI-th city.

Output

Output n space-separated integers. The i-th number should is the parent of the I-th city, or -1, if the i-th cit Y doesn ' t has a parent. The cities is numbered from 1 to their order in the input.

Sample Input

4
1 1000
7 10
9 1
12 100
-1 4 2 1
Test Instructions:Give you n points on the x-axis position x and the weight pos for an I-point his father defined as and he recently and the weighted value is greater than p[i] for the point output of each point father, not satisfied with the point output of its father-1;The following:Monotonic Stack preprocessing out the first point of the left father, and the father on the right, to go recently, the weight of the largest one to do once RMQ, two points to the left of the father who, the best solution is the closest point
#include <bits/stdc++.h>using namespacestd;Const intN = 1e6+ -, M = 1e6+Ten, mod = 1e9+7, INF =2e9;typedefLong Longll;intN,ans[n];structss{Long LongX,p;intID;} A[n],lefts[n],rights[n];vector<ss>G;BOOLCMP (ss S1,ss S2) {returns1.x<s2.x;}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%i64d%i64d", &AMP;A[I].X,&AMP;A[I].P), a[i].id =i; Sort (a+1, a+n+1, CMP); a[0].x=-inf, a[0].P = inf; a[0].id =-1; G.push_back (a[0]);  for(intI=1; i<=n;i++) {         while(G.back (). p<=A[I].P)        G.pop_back (); Lefts[i]=G.back ();    G.push_back (A[i]);    } g.clear (); A[n+1] = (ss) {inf,inf,-1}; G.push_back (A[n+1]);  for(inti=n;i>=1; i--) {         while(G.back (). p<=A[I].P)        G.pop_back (); Rights[i]=G.back ();    G.push_back (A[i]); }     for(intI=1; i<=n;i++) {        if(ABS (lefts[i].x-a[i].x) ==abs (rights[i].x-a[i].x)) {            if(LEFTS[I].P&GT;RIGHTS[I].P) Ans[a[i].id] =lefts[i].id; ElseAns[a[i].id] =rights[i].id; }        Else if(ABS (lefts[i].x-a[i].x) <abs (rights[i].x-a[i].x)) {Ans[a[i].id]=lefts[i].id; }        ElseAns[a[i].id] =rights[i].id; }     for(intI=1; i<=n;i++) printf ("%d", Ans[i]); printf ("\ n");}
monotonic Stacks

#include <bits/stdc++.h>using namespacestd;Const intN = 2e5+ -, M = 1e6+Ten, mod = 1e9+7, INF =2e9;typedefLong Longll;intn;ll dp[n][ +];intAns[n];structss{Long LongX,p;intID;} A[n],lefts[n],rights[n];BOOLCMP (ss S1,ss S2) {returns1.x<s2.x;} ll Cal (intLintR) {    if(L==R)returnA[L].P; intK = (int) (Log (Double) r-l+1)/log (2.0)); returnMax (Dp[l][k], Dp[r-(1&LT;&LT;K) +1][k]);}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%i64d%i64d", &AMP;A[I].X,&AMP;A[I].P), a[i].id=i; Sort (a+1, a+n+1, CMP); a[0] = (ss) {-inf,inf,-1}; A[n+1] = (ss) {inf,inf,-1};  for(intI=0; i<=n+1; i++) dp[i][0] =A[I].P;  for(intj=1;(1&LT;&LT;J) <=n+1; j + +) {         for(intI=0; i + (1&LT;&LT;J)-1<= n+1; i++) {            if(dp[i][j-1] > dp[i+ (1<< (J-1))][j-1]) Dp[i][j]= dp[i][j-1]; Else{Dp[i][j]= dp[i+ (1<< (J-1))][j-1]; }        }    }    //cout<<cal (0,1) <<endl;     for(intI=1; i<=n;i++) {        intL =0, r = I1, a=0;  while(l<=r) {intMid = (l+r) >>1; if(Cal (mid,i-1) > A[I].P) L = mid+1, a=mid; Elser = mid-1; } Lefts[i]=A[a]; //cout<<a<< "";L = i+1, R = n+1; A = n+1;  while(l<=r) {intMid = (l+r) >>1; if(Cal (i+1, mid) > A[I].P) r=mid-1, A =mid; ElseL = mid+1; } Rights[i]=A[a]; //cout<<a<<endl;    }     for(intI=1; i<=n;i++) {        if(ABS (lefts[i].x-a[i].x) ==abs (rights[i].x-a[i].x)) {            if(LEFTS[I].P&GT;RIGHTS[I].P) Ans[a[i].id] =lefts[i].id; ElseAns[a[i].id] =rights[i].id; }        Else if(ABS (lefts[i].x-a[i].x) <abs (rights[i].x-a[i].x)) {Ans[a[i].id]=lefts[i].id; }        ElseAns[a[i].id] =rights[i].id; }     for(intI=1; i<=n;i++) printf ("%d", Ans[i]); printf ("\ n");}
rmq+ Two minutes

Gym 100971D laying Cables monotonic stack

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.