Submission #1103848

# Submission time Handle Problem Language Result Execution time Memory
1103848 2024-10-22T02:40:09 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
100 / 100
145 ms 112312 KB
#include <bits/stdc++.h>

#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair

#ifdef NCGM
#include<C:/NCGM/headers/debug.h>
#else
#define debug(...) "fr";
#endif

using namespace std;


const int N=1e6+13;

int n;
ll a[N],b[N],st[20][N],d[N];

void pre() {
    For(i,1,n) st[0][i]=a[i+1]*2-a[i];
    For(j,1,19)
        for(int i=1;i+(1<<j)-1<=n;i++) st[j][i]=max(st[j-1][i],st[j-1][i+(1<<j-1)]);
}
void pre1() {
    For(i,1,n) st[0][i]=a[i]-a[i-1]*2;
    For(j,1,19)
        for(int i=1;i+(1<<j)-1<=n;i++) st[j][i]=max(st[j-1][i],st[j-1][i+(1<<j-1)]);
}
ll query(int l,int r) {
    int s=__lg(r-l+1);
    return max(st[s][l],st[s][r-(1<<s)+1]);
}
vector<pair<int,ll>> g[N];

void dfs(int u,int p=0) {
    for(auto v: g[u])
    if (v.ff!=p) {
        d[v.ff]=d[u]+v.ss;
        dfs(v.ff,u);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n;
    For(i,1,n) cin >> a[i];
    pre();
    For(i,1,n) b[i]=a[i]-a[i-1];
    For(i,1,n) b[i]+=b[i-1];
    For(i,2,n-1) {
        if (a[i]-a[i-1]<=a[i+1]-a[i]) {
            int l=1,r=i-1;
            while(l<r) {
                int mid=l+(r-l)/2;
                if (query(mid,i-1)<=a[i+1]) r=mid;
                else l=mid+1;
            }
            g[r].pb({i,a[i]-a[r]});
            g[i].pb({r,a[i]-a[r]});
        }
    }
    pre1();
    For(i,2,n-1) {
        if (a[i]-a[i-1]>a[i+1]-a[i])  {
            int l=i+1,r=n;
            while(l<r) {
                int mid=l+(r-l+1)/2;
                if (query(i+1,mid) < -a[i-1]) l=mid;
                else r=mid-1;
            }
            g[l].pb({i,a[l]-a[i]});
            g[i].pb({l,a[l]-a[i]});
        }
    }
    d[1]=d[n]=a[n]-a[1];
    dfs(1);
    dfs(n);
    int q;
    cin >> q;
    while(q--) {
        int x;
        cin >> x;
        int idx=lower_bound(a+1,a+1+n,x)-a;
        if (idx>1 && idx<=n) {
            if (x-a[idx-1]<=a[idx]-x) cout << x-a[idx-1]+d[idx-1] << endl;
            else cout << a[idx]-x+d[idx] << endl;
        } else if (idx==1) cout << a[idx]-x+d[idx] << endl;
        else cout << x-a[idx-1]+d[idx-1] << endl;
    }


    return 0;
}

Compilation message

travel.cpp: In function 'void pre()':
travel.cpp:37:79: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   37 |         for(int i=1;i+(1<<j)-1<=n;i++) st[j][i]=max(st[j-1][i],st[j-1][i+(1<<j-1)]);
      |                                                                              ~^~
travel.cpp: In function 'void pre1()':
travel.cpp:42:79: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   42 |         for(int i=1;i+(1<<j)-1<=n;i++) st[j][i]=max(st[j-1][i],st[j-1][i+(1<<j-1)]);
      |                                                                              ~^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35408 KB Output is correct
2 Correct 8 ms 51936 KB Output is correct
3 Correct 8 ms 52048 KB Output is correct
4 Correct 7 ms 52060 KB Output is correct
5 Correct 7 ms 52048 KB Output is correct
6 Correct 9 ms 52048 KB Output is correct
7 Correct 7 ms 52048 KB Output is correct
8 Correct 5 ms 31312 KB Output is correct
9 Correct 5 ms 31312 KB Output is correct
10 Correct 5 ms 31312 KB Output is correct
11 Correct 5 ms 31484 KB Output is correct
12 Correct 5 ms 33360 KB Output is correct
13 Correct 6 ms 37456 KB Output is correct
14 Correct 8 ms 51828 KB Output is correct
15 Correct 9 ms 52048 KB Output is correct
16 Correct 8 ms 52048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35408 KB Output is correct
2 Correct 8 ms 51936 KB Output is correct
3 Correct 8 ms 52048 KB Output is correct
4 Correct 7 ms 52060 KB Output is correct
5 Correct 7 ms 52048 KB Output is correct
6 Correct 9 ms 52048 KB Output is correct
7 Correct 7 ms 52048 KB Output is correct
8 Correct 5 ms 31312 KB Output is correct
9 Correct 5 ms 31312 KB Output is correct
10 Correct 5 ms 31312 KB Output is correct
11 Correct 5 ms 31484 KB Output is correct
12 Correct 5 ms 33360 KB Output is correct
13 Correct 6 ms 37456 KB Output is correct
14 Correct 8 ms 51828 KB Output is correct
15 Correct 9 ms 52048 KB Output is correct
16 Correct 8 ms 52048 KB Output is correct
17 Correct 68 ms 108724 KB Output is correct
18 Correct 74 ms 108872 KB Output is correct
19 Correct 73 ms 109824 KB Output is correct
20 Correct 85 ms 109896 KB Output is correct
21 Correct 76 ms 108772 KB Output is correct
22 Correct 76 ms 108712 KB Output is correct
23 Correct 77 ms 108720 KB Output is correct
24 Correct 77 ms 108732 KB Output is correct
25 Correct 62 ms 108732 KB Output is correct
26 Correct 64 ms 108528 KB Output is correct
27 Correct 62 ms 98912 KB Output is correct
28 Correct 66 ms 96444 KB Output is correct
29 Correct 78 ms 93768 KB Output is correct
30 Correct 75 ms 91308 KB Output is correct
31 Correct 80 ms 109232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35408 KB Output is correct
2 Correct 7 ms 37456 KB Output is correct
3 Correct 6 ms 31324 KB Output is correct
4 Correct 6 ms 31356 KB Output is correct
5 Correct 5 ms 31312 KB Output is correct
6 Correct 5 ms 31312 KB Output is correct
7 Correct 105 ms 94896 KB Output is correct
8 Correct 114 ms 90028 KB Output is correct
9 Correct 110 ms 110776 KB Output is correct
10 Correct 104 ms 84396 KB Output is correct
11 Correct 113 ms 83628 KB Output is correct
12 Correct 103 ms 98732 KB Output is correct
13 Correct 34 ms 33208 KB Output is correct
14 Correct 25 ms 31824 KB Output is correct
15 Correct 104 ms 101316 KB Output is correct
16 Correct 112 ms 112312 KB Output is correct
17 Correct 107 ms 103864 KB Output is correct
18 Correct 35 ms 33096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35408 KB Output is correct
2 Correct 8 ms 51936 KB Output is correct
3 Correct 8 ms 52048 KB Output is correct
4 Correct 7 ms 52060 KB Output is correct
5 Correct 7 ms 52048 KB Output is correct
6 Correct 9 ms 52048 KB Output is correct
7 Correct 7 ms 52048 KB Output is correct
8 Correct 5 ms 31312 KB Output is correct
9 Correct 5 ms 31312 KB Output is correct
10 Correct 5 ms 31312 KB Output is correct
11 Correct 5 ms 31484 KB Output is correct
12 Correct 5 ms 33360 KB Output is correct
13 Correct 6 ms 37456 KB Output is correct
14 Correct 8 ms 51828 KB Output is correct
15 Correct 9 ms 52048 KB Output is correct
16 Correct 8 ms 52048 KB Output is correct
17 Correct 68 ms 108724 KB Output is correct
18 Correct 74 ms 108872 KB Output is correct
19 Correct 73 ms 109824 KB Output is correct
20 Correct 85 ms 109896 KB Output is correct
21 Correct 76 ms 108772 KB Output is correct
22 Correct 76 ms 108712 KB Output is correct
23 Correct 77 ms 108720 KB Output is correct
24 Correct 77 ms 108732 KB Output is correct
25 Correct 62 ms 108732 KB Output is correct
26 Correct 64 ms 108528 KB Output is correct
27 Correct 62 ms 98912 KB Output is correct
28 Correct 66 ms 96444 KB Output is correct
29 Correct 78 ms 93768 KB Output is correct
30 Correct 75 ms 91308 KB Output is correct
31 Correct 80 ms 109232 KB Output is correct
32 Correct 6 ms 35408 KB Output is correct
33 Correct 7 ms 37456 KB Output is correct
34 Correct 6 ms 31324 KB Output is correct
35 Correct 6 ms 31356 KB Output is correct
36 Correct 5 ms 31312 KB Output is correct
37 Correct 5 ms 31312 KB Output is correct
38 Correct 105 ms 94896 KB Output is correct
39 Correct 114 ms 90028 KB Output is correct
40 Correct 110 ms 110776 KB Output is correct
41 Correct 104 ms 84396 KB Output is correct
42 Correct 113 ms 83628 KB Output is correct
43 Correct 103 ms 98732 KB Output is correct
44 Correct 34 ms 33208 KB Output is correct
45 Correct 25 ms 31824 KB Output is correct
46 Correct 104 ms 101316 KB Output is correct
47 Correct 112 ms 112312 KB Output is correct
48 Correct 107 ms 103864 KB Output is correct
49 Correct 35 ms 33096 KB Output is correct
50 Correct 130 ms 101668 KB Output is correct
51 Correct 126 ms 112200 KB Output is correct
52 Correct 129 ms 110824 KB Output is correct
53 Correct 122 ms 110880 KB Output is correct
54 Correct 129 ms 103936 KB Output is correct
55 Correct 145 ms 110900 KB Output is correct
56 Correct 129 ms 95816 KB Output is correct
57 Correct 107 ms 85320 KB Output is correct
58 Correct 108 ms 88424 KB Output is correct