Submission #1014807

# Submission time Handle Problem Language Result Execution time Memory
1014807 2024-07-05T14:41:49 Z alexdd Bitaro's travel (JOI23_travel) C++17
15 / 100
3000 ms 12112 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[200005];
int aint[530005][2];
void build(int nod, int st, int dr)
{
    if(st==dr)
    {
        aint[nod][0] = 2*a[st]-a[st-1];
        aint[nod][1] = a[st]-2*a[st-1];
        return;
    }
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
    aint[nod][0] = max(aint[nod*2][0], aint[nod*2+1][0]);
    aint[nod][1] = max(aint[nod*2][1], aint[nod*2+1][1]);
}
int qry(int nod, int st, int dr, int le, int ri, int c)
{
    if(le>ri)
        return -1;
    if(le==st && dr==ri)
        return aint[nod][c];
    int mij=(st+dr)/2;
    return max(qry(nod*2,st,mij,le,min(mij,ri),c),
               qry(nod*2+1,mij+1,dr,max(mij+1,le),ri,c));
}

int solve(int le, int ri, int poz)
{
    if(le==1 && ri==n)
        return 0;
    if(le==1)
    {
        return a[n]-poz;
    }
    else if(ri==n)
    {
        return poz-a[1];
    }
    if(le>1 && (ri==n || poz-a[le-1] <= a[ri+1]-poz))///merge spre stanga
    {
        int st=2,dr=le,ans=le;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            if(qry(1,1,n,mij,le,0) <= a[ri+1])
            {
                ans=mij;
                dr=mij-1;
            }
            else
                st=mij+1;
        }
        return solve(ans-1,ri,a[ans-1]) + poz-a[ans-1];
    }
    else///merge spre dreapta
    {
        int st=ri+1,dr=n,ans=ri+1;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            if(qry(1,1,n,ri+1,mij,1) < -a[le-1])
            {
                ans=mij;
                st=mij+1;
            }
            else
                dr=mij-1;
        }
        return solve(le,ans,a[ans]) + a[ans]-poz;
    }
}
///a[x]-a[x-1] <= a[ri]-a[x]
///2*a[x]-a[x-1] <= a[ri]
///a[x]-a[x-1] <= a[x-1] - a[le]
///a[x]-2*a[x-1] <= -a[le]
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    build(1,1,n);
    int q,s;
    cin>>q;
    while(q--)
    {
        cin>>s;
        if(s<=a[1])
            cout<<solve(1,1,a[1])+(a[1]-s)<<"\n";
        else if(s>=a[n])
            cout<<solve(n,n,a[n])+(s-a[n])<<"\n";
        else
        {
            int st=1,dr=n-1,ans=n;
            while(st<=dr)
            {
                int mij=(st+dr)/2;
                if(a[mij]>=s)
                {
                    ans=mij;
                    dr=mij-1;
                }
                else
                    st=mij+1;
            }
            if(ans==1 || a[ans]-s < s-a[ans-1]) cout<<solve(ans,ans,a[ans])+(a[ans]-s)<<"\n";
            else cout<<solve(ans-1,ans-1,a[ans-1])+(s-a[ans-1])<<"\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 568 KB Output is correct
6 Correct 2 ms 568 KB Output is correct
7 Correct 3 ms 476 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 568 KB Output is correct
6 Correct 2 ms 568 KB Output is correct
7 Correct 3 ms 476 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
17 Correct 21 ms 11856 KB Output is correct
18 Correct 378 ms 12004 KB Output is correct
19 Correct 22 ms 11868 KB Output is correct
20 Correct 19 ms 11996 KB Output is correct
21 Correct 86 ms 11864 KB Output is correct
22 Correct 490 ms 12112 KB Output is correct
23 Correct 41 ms 11860 KB Output is correct
24 Correct 73 ms 11864 KB Output is correct
25 Correct 325 ms 12004 KB Output is correct
26 Correct 298 ms 11768 KB Output is correct
27 Correct 254 ms 12064 KB Output is correct
28 Correct 229 ms 11868 KB Output is correct
29 Correct 262 ms 11856 KB Output is correct
30 Correct 259 ms 11864 KB Output is correct
31 Correct 246 ms 11856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Execution timed out 3096 ms 11780 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 568 KB Output is correct
6 Correct 2 ms 568 KB Output is correct
7 Correct 3 ms 476 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
17 Correct 21 ms 11856 KB Output is correct
18 Correct 378 ms 12004 KB Output is correct
19 Correct 22 ms 11868 KB Output is correct
20 Correct 19 ms 11996 KB Output is correct
21 Correct 86 ms 11864 KB Output is correct
22 Correct 490 ms 12112 KB Output is correct
23 Correct 41 ms 11860 KB Output is correct
24 Correct 73 ms 11864 KB Output is correct
25 Correct 325 ms 12004 KB Output is correct
26 Correct 298 ms 11768 KB Output is correct
27 Correct 254 ms 12064 KB Output is correct
28 Correct 229 ms 11868 KB Output is correct
29 Correct 262 ms 11856 KB Output is correct
30 Correct 259 ms 11864 KB Output is correct
31 Correct 246 ms 11856 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Execution timed out 3096 ms 11780 KB Time limit exceeded
39 Halted 0 ms 0 KB -