Travel by car

Travel by car

There are n number of east-west in a row the city from west to east numbered 1 ~ n, the height of the i-th city is \ (H_i \) , different heights, from the definition of the two cities i, j of \ (dis ( i, J) = | H_i-h_j | \) (compare the size of the priority value, when the values are the same, relatively high degree of size), there are now two people a, b from a city in the same car moving eastward, and take turns driving, the first day of a drive, select a drive to reach it from the next nearest city every day, and b to select the nearest city, there are two inquiries:

Asked one:

Given x, from which the city asked to make a total travel distance of no more than x, and a distance traveled and the ratio of the maximum distance traveled b (the denominator is 0 operator infinity, the same as the ratio of the maximum height of the selected city s).

Inquiry II:

There are m groups interrogation, a query each s, x, expressed as a departure city s, seeking with the case where the total distance does not exceed x, a, b, respectively, the distance traveled.

To 100% of the data, there are \ (1≤N≤100,000,1≤M≤100,000 \) .

solution

In fact, the biggest feature is to find a certain person in a certain position, you can reach the next city is fixed, which is double the mark.

For a city i, east of the nearest city is naturally highly closest to it, to maintain it is to take this position in the vicinity of the city after the east side of the city and the city sort, involving to maintain order, naturally balanced tree, because they have not learned, so set an alternative, to join the note number in the vicinity looking for their better implementation, nearly twice the same way.

Then set \ (Ag [i] \) represents a city in the i-th city can reach number, the same way there \ (Bg [i] \) , an appeal has been elaborated maintenance approach, which naturally requires a \ (go [ i] [j] [k] \) represents the i-th city, after the \ (2 ^ j \) days, people k (0A, 1B) after the first day of the departure and arrival cities, there is not difficult

\[go[i][0][0]=Ag[i],go[i][0][1]=Bg[i]\]

\[go[i][1][k]=go[go[i][0][k]][0][1-k]\]

\[go[i][j][k]=go[go[i][j-1][k]][j-1][k]\]

In addition to mentioned, all other 0

Then we want to maintain \ (A [i] [j ] [k], B [i] [j] [k] \) respectively starting at the i-th city, after (2 ^ j \) \ days, people k a day before starting the distance traveled, b distance traveled, first of all initialization infinity

There are

\[A[i][0][0]=dis(i,go[i][0][0]),A[i][0][1]=0\]

\[A[i][1][k]=A[i][0][k]+A[go[i][0][k]][0][1-k]\]

\[A[i][j][k]=A[i][j-1][k]+A[go[i][j-1][k]][j-1][k]\]

\[B[i][0][0]=0,B[i][0][1]=dis(i,go[i][0][1])\]

\[B[i][1][k]=B[i][0][k]+B[go[i][0][k]][0][1-k]\]

\[B[i][j][k]=B[i][j-1][k]+B[go[i][j-1][k]][j-1][k]\]

So we safeguard the multiplier array, now consider how to answer questions, ask first create a function, given s, x (second inquiry with variable meaning), answer a, b, respectively, the distance traveled, and for no more than x, we can use the binary split and, descending enumeration close answer, so the second inquiry will be solved, time complexity \ (the n-log_2 ^ \) .

For the first inquiry, not difficult to know, what city we can support enumeration, calculate the ratio of direct violence, one by one than you can.

So the problem is solved, this problem demonstrated the multiplication of alternate use might be called alternately doubled, also test a method common to the maintenance position doubled, and then doubled the distance maintenance, split binary answer queries, as well as pay attention to things on the stl use as set.

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define il inline
#define ri register
#define ll long long
#define swap(x,y) x^=y^=x^=y
#define Size 100000
#define intmax 0x7fffffff
using namespace std;
struct frac{
    int s,m;
    il void sort(){
        int d(gcd(s,m));
        if(d)s/=d,m/=d;
    }
    il int gcd(int a,int b){
        while(b)swap(a,b),b%=a;return a;
    }
    il bool operator==(frac&x){
        if(!x.m&&!m)return true;
        if(x.m==m&&s==x.s)return true;
        return false;
    }
    il bool operator<(frac&x){
        if(!m)return false;if(!x.m)return true;
        return (double)s/m<(double)x.s/x.m;
    }
}r1,r2;
struct pi{
    int x,y;
    il bool operator<(const pi&a)const{
        return x<a.x;
    }
}m,g[5];int gt;
set<pi>H;
set<pi>::iterator l,r;
ll A[Size+1][18][2],B[Size+1][18][2];
int Ag[Size+1],Bg[Size+1],go[Size+1][18][2],h[Size+1];
il pi ask(int,int);
il void prepare(int);
il bool comp(const pi&,const pi&);
template<class free>il free Abs(free);
template<class free>il void read(free&);
int main(){
    int n,s,x;
    read(n),prepare(n),read(x);
    for(int i(1);i<=n;++i){
        m=ask(i,x);
        r1=(frac){m.x,m.y},r1.sort();
        if(r1<r2)r2=r1,s=i;
        else if(r1==r2&&h[s]<h[i])s=i;
    }printf("%d\n",s),read(n);
    while(n--)
        read(s),read(x),m=ask(s,x),
            printf("%d %d\n",m.x,m.y);
    return 0;
}
il void prepare(int n){
    for(int i(1);i<=n;++i)read(h[i]);
    for(int i(n),j;i;--i){
        j=h[i],m=(pi){j,i},gt&=0;
        H.insert(m),l=r=H.find(m);
        if(l!=H.begin()){
            --l,g[++gt]=*l;
            if(l!=H.begin())--l,g[++gt]=*l;
        }
        if(++r,r!=H.end()){
            g[++gt]=*r;
            if(++r,r!=H.end())g[++gt]=*r;
        }sort(g+1,g+gt+1,comp);
        if(gt>=2)Ag[i]=g[2].y;
        if(gt>=1)Bg[i]=g[1].y;
    }h[0]=intmax;
    for(int i(n),j;i;--i){
        go[i][0][0]=Ag[i],go[i][0][1]=Bg[i];
        for(j=0;j<2;++j)go[i][1][j]=go[go[i][0][j]][0][1-j];
        for(j=2;j<18;++j)
            go[i][j][0]=go[go[i][j-1][0]][j-1][0],
                go[i][j][1]=go[go[i][j-1][1]][j-1][1];
    }memset(A,1,sizeof(A)),memset(B,1,sizeof(B));
    for(int i(n),j,k;i;--i){
        A[i][0][0]=Abs(h[go[i][0][0]]-h[i]),A[i][0][1]=0;
        B[i][0][0]=0,B[i][0][1]=Abs(h[go[i][0][1]]-h[i]);
        for(j=0;j<2;++j){
            A[i][1][j]=A[i][0][j]+A[go[i][0][j]][0][1-j];
            B[i][1][j]=B[i][0][j]+B[go[i][0][j]][0][1-j];
        }
        for(j=2;j<18;++j)
            for(k=0;k<2;++k){
                A[i][j][k]=A[i][j-1][k]+A[go[i][j-1][k]][j-1][k];
                B[i][j][k]=B[i][j-1][k]+B[go[i][j-1][k]][j-1][k];
            }
    }
}
il pi ask(int p,int x){
    int la(0),lb(0);
    for(int i(17);i>=0;--i)
        if(x>=A[p][i][0]+B[p][i][0])
            la+=A[p][i][0],lb+=B[p][i][0],
                x-=A[p][i][0]+B[p][i][0],p=go[p][i][0];
    return (pi){la,lb};
}
il bool comp(const pi&a,const pi&b){
    return Abs(a.x-m.x)==Abs(b.x-m.x)?
        a.x<b.x:Abs(a.x-m.x)<Abs(b.x-m.x);
}
template<class free>
il free Abs(free x){
    return x<0?-x:x;
}
template<class free>
il void read(free&x){
    x&=0;ri char c;while(c=getchar(),c==' '||c=='\r'||c=='\n');
    ri bool check(false);if(c=='-')check|=true,c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    if(check)x=-x;
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/10948935.html
car