Submission #757712

# Submission time Handle Problem Language Result Execution time Memory
757712 2023-06-13T15:33:21 Z ttamx Bitaro's travel (JOI23_travel) C++14
100 / 100
1191 ms 273484 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

int n,q;
int a[N];
int d[N],v1[N],v2[N],l1[N],l2[N];

struct node{
    int val;
    node *l,*r;
    node():val(0),l(nullptr),r(nullptr){}
    node(int val):val(val),l(nullptr),r(nullptr){}
    node(int val,node *l,node *r):val(val),l(l),r(r){}
};
typedef node* nodeptr;

struct maxtree{
    nodeptr rt[N];
    void build(int l,int r,nodeptr &t){
        t=new node(0);
        if(l==r)return;
        int m=(l+r)/2;
        build(l,m,t->l);
        build(m+1,r,t->r);
    }
    void build(int ver){
        build(1,n,rt[ver]);
    }
    void update(int l,int r,nodeptr &t,nodeptr k,int x,int val){
        t=new node(*k);
        t->val=max(t->val,val);
        if(l==r)return;
        int m=(l+r)/2;
        if(x<=m)update(l,m,t->l,k->l,x,val);
        else update(m+1,r,t->r,k->r,x,val);
    }
    void update(int from,int to,int idx,int val){
        update(1,n,rt[to],rt[from],idx,val);
    }
    int query(int l,int r,nodeptr t,int x,int y){
        if(y<l||r<x)return 0;
        if(x<=l&&r<=y)return t->val;
        int m=(l+r)/2;
        return max(query(l,m,t->l,x,y),query(m+1,r,t->r,x,y));
    }
    int query(int ver,int l,int r){
        return query(1,n,rt[ver],l,r);
    }
}mxs;

struct mnstree{
    nodeptr rt[N];
    void build(int l,int r,nodeptr &t){
        t=new node(n);
        if(l==r)return;
        int m=(l+r)/2;
        build(l,m,t->l);
        build(m+1,r,t->r);
    }
    void build(int ver){
        build(1,n,rt[ver]);
    }
    void update(int l,int r,nodeptr &t,nodeptr k,int x,int val){
        t=new node(*k);
        t->val=min(t->val,val);
        if(l==r)return;
        int m=(l+r)/2;
        if(x<=m)update(l,m,t->l,k->l,x,val);
        else update(m+1,r,t->r,k->r,x,val);
    }
    void update(int from,int to,int idx,int val){
        update(1,n,rt[to],rt[from],idx,val);
    }
    int query(int l,int r,nodeptr t,int x,int y){
        if(y<l||r<x)return n;
        if(x<=l&&r<=y)return t->val;
        int m=(l+r)/2;
        return min(query(l,m,t->l,x,y),query(m+1,r,t->r,x,y));
    }
    int query(int ver,int l,int r){
        return query(1,n,rt[ver],l,r);
    }
}mns;

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> a[i];
    }
    a[0]=-2e9;
    a[n+1]=2e9;
    for(int i=1;i<n;i++){
        d[i]=a[i+1]-a[i];
    }
    int sum=0;
    for(int i=n-1;i>0;i--){
        v1[i]=l1[i]=sum-d[i];
        sum+=d[i];
    }
    sort(l1+1,l1+n);
    mxs.build(0);
    for(int i=1;i<n;i++){
        int idx=lower_bound(l1+1,l1+n,v1[i])-l1;
        mxs.update(i-1,i,idx,i);
    }
    sum=0;
    for(int i=1;i<n;i++){
        v2[i]=l2[i]=sum-d[i];
        sum+=d[i];
    }
    sort(l2+1,l2+n);
    mns.build(n);
    for(int i=n-1;i>0;i--){
        int idx=lower_bound(l2+1,l2+n,v2[i])-l2;
        mns.update(i+1,i,idx,i);
    }
    cin >> q;
    while(q--){
        int x;
        cin >> x;
        if(x<=a[1]){
            cout << a[n]-x << '\n';
            continue;
        }
        if(x>=a[n]){
            cout << x-a[1] << '\n';
            continue;
        }
        ll ans=0;
        int l=lower_bound(a+1,a+n+1,x)-a-1;
        int r=upper_bound(a+1,a+n+1,x)-a;
        if(r-l==1){
            if(x-a[l]<=a[r]-x){
                ans+=x-a[l];
                x=l;
                l--;
            }else{
                ans+=a[r]-x;
                x=r;
                r++;
            }
        }else{
            x=(l+r)/2;
        }
        while(1<=l&&r<=n){
            if(a[x]-a[l]<=a[r]-a[x]){
                int idx=lower_bound(l1+1,l1+n,a[n]-a[x])-l1-1;
                int res=mxs.query(l-1,1,idx)+1;
                ans+=a[x]-a[res];
                x=res;
                l=res-1;
            }else{
                int idx=upper_bound(l2+1,l2+n,a[x]-a[1])-l2-1;
                int res=mns.query(r,1,idx);
                ans+=a[res]-a[x];
                x=res;
                r=res+1;
            }
        }
        if(r<=n){
            ans+=a[n]-a[x];
        }
        if(1<=l){
            ans+=a[x]-a[1];
        }
        cout << ans << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 2132 KB Output is correct
3 Correct 3 ms 2132 KB Output is correct
4 Correct 3 ms 2128 KB Output is correct
5 Correct 5 ms 2136 KB Output is correct
6 Correct 3 ms 2132 KB Output is correct
7 Correct 5 ms 2088 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 3 ms 2136 KB Output is correct
15 Correct 3 ms 2128 KB Output is correct
16 Correct 3 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 2132 KB Output is correct
3 Correct 3 ms 2132 KB Output is correct
4 Correct 3 ms 2128 KB Output is correct
5 Correct 5 ms 2136 KB Output is correct
6 Correct 3 ms 2132 KB Output is correct
7 Correct 5 ms 2088 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 3 ms 2136 KB Output is correct
15 Correct 3 ms 2128 KB Output is correct
16 Correct 3 ms 2132 KB Output is correct
17 Correct 347 ms 269104 KB Output is correct
18 Correct 347 ms 269140 KB Output is correct
19 Correct 333 ms 269160 KB Output is correct
20 Correct 340 ms 269212 KB Output is correct
21 Correct 338 ms 269124 KB Output is correct
22 Correct 346 ms 269156 KB Output is correct
23 Correct 354 ms 269164 KB Output is correct
24 Correct 385 ms 269072 KB Output is correct
25 Correct 345 ms 269184 KB Output is correct
26 Correct 363 ms 269192 KB Output is correct
27 Correct 349 ms 269248 KB Output is correct
28 Correct 352 ms 269088 KB Output is correct
29 Correct 349 ms 269132 KB Output is correct
30 Correct 359 ms 269104 KB Output is correct
31 Correct 368 ms 269160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1088 ms 271168 KB Output is correct
8 Correct 874 ms 271140 KB Output is correct
9 Correct 1191 ms 271224 KB Output is correct
10 Correct 674 ms 271132 KB Output is correct
11 Correct 618 ms 271104 KB Output is correct
12 Correct 563 ms 271172 KB Output is correct
13 Correct 48 ms 4168 KB Output is correct
14 Correct 42 ms 1424 KB Output is correct
15 Correct 577 ms 272256 KB Output is correct
16 Correct 549 ms 272340 KB Output is correct
17 Correct 564 ms 272328 KB Output is correct
18 Correct 55 ms 4056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 2132 KB Output is correct
3 Correct 3 ms 2132 KB Output is correct
4 Correct 3 ms 2128 KB Output is correct
5 Correct 5 ms 2136 KB Output is correct
6 Correct 3 ms 2132 KB Output is correct
7 Correct 5 ms 2088 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 3 ms 2136 KB Output is correct
15 Correct 3 ms 2128 KB Output is correct
16 Correct 3 ms 2132 KB Output is correct
17 Correct 347 ms 269104 KB Output is correct
18 Correct 347 ms 269140 KB Output is correct
19 Correct 333 ms 269160 KB Output is correct
20 Correct 340 ms 269212 KB Output is correct
21 Correct 338 ms 269124 KB Output is correct
22 Correct 346 ms 269156 KB Output is correct
23 Correct 354 ms 269164 KB Output is correct
24 Correct 385 ms 269072 KB Output is correct
25 Correct 345 ms 269184 KB Output is correct
26 Correct 363 ms 269192 KB Output is correct
27 Correct 349 ms 269248 KB Output is correct
28 Correct 352 ms 269088 KB Output is correct
29 Correct 349 ms 269132 KB Output is correct
30 Correct 359 ms 269104 KB Output is correct
31 Correct 368 ms 269160 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 324 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1088 ms 271168 KB Output is correct
39 Correct 874 ms 271140 KB Output is correct
40 Correct 1191 ms 271224 KB Output is correct
41 Correct 674 ms 271132 KB Output is correct
42 Correct 618 ms 271104 KB Output is correct
43 Correct 563 ms 271172 KB Output is correct
44 Correct 48 ms 4168 KB Output is correct
45 Correct 42 ms 1424 KB Output is correct
46 Correct 577 ms 272256 KB Output is correct
47 Correct 549 ms 272340 KB Output is correct
48 Correct 564 ms 272328 KB Output is correct
49 Correct 55 ms 4056 KB Output is correct
50 Correct 1114 ms 273464 KB Output is correct
51 Correct 948 ms 273404 KB Output is correct
52 Correct 821 ms 273484 KB Output is correct
53 Correct 782 ms 273368 KB Output is correct
54 Correct 765 ms 273436 KB Output is correct
55 Correct 741 ms 273324 KB Output is correct
56 Correct 659 ms 273216 KB Output is correct
57 Correct 700 ms 273204 KB Output is correct
58 Correct 692 ms 273168 KB Output is correct