Submission #874635

# Submission time Handle Problem Language Result Execution time Memory
874635 2023-11-17T12:58:04 Z sleepntsheep Mountain Trek Route (IZhO12_route) C++17
0 / 100
1 ms 6492 KB
#include <iostream>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 1000000

int z, n, k; i64 a[N+3], t[N+3<<1],d[N+3<<1];
#define SSS int m = (l+r)/2,vl=v+1,vr=v+(m-l+1)*2

void build(int v, int l, int r)
{
    if (l == r) return void(t[v] = a[l]);
    SSS;
    build(vl, l,m), build(vr,m+1,r);t[v]=min(t[vl],t[vr]);
}

void push(int v, int l,int r)
{
    if(!d[v])return;
    t[v]=d[v];
    if (l-r)
    {
        SSS;
        d[vl]=d[vr]=d[v];
    }
    d[v]=0;
}

void upd(int v, int l, int r, int x, int y ,int k)
{
    if (r<x||y<l)return;
    if(x<=l&&r<=y){d[v]=k;push(v,l,r);return;}
    SSS;
    upd(vl,l,m,x,y,k),upd(vr,m+1,r,x,y,k);t[v]=min(t[vl],t[vr]);
}

i64 qry(int v, int l, int r, int x, int y)
{
    if (r<x||y<l)return 1e18+1;
    if(x<=l&&r<=y){return t[v];}
    SSS;
    return min(qry(vl,l,m,x, y),qry(vr,m+1,r,x,y));
}

int main()
{
    ShinLena;
    cin >> n >> k;
    a[0] = a[n+2] = 1e18+1;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    a[n+1] = a[0];
    n += 3;

    build(0, 0, n-1);

    deque<pair<i64, i64>> v; deque<int> s = {0};
    for (int i = 1; i < n; ++i)
    {
        for (; s.size() && a[s.back()] <= a[i]; )
        {
            if (a[s.back()] == 1e18+1 && a[i] == 1e18+1) break;
            i64 S = qry(0, 0, n-1, s.back() + 1, i);
            if (a[s.back()]-S <= 0) 
            {
                s.pop_back();
                continue;
            }
            //cout<<s.back()<< ' ' << i << ' '<<a[s.back()]-S<<endl;
            v.emplace_back(i - s.back() - 1,a[s.back()]-S);
            upd(0,0,n-1,s.back(),i-1,a[s.back()]);
            s.pop_back();
        }
        s.push_back(i);
    }

    sort(ALL(v));
    for (; v.size() && v[0].first == 0; ) v.pop_front();
    for (; v.size() && k >= v[0].first; )
    {
        i64 take = min(k / v[0].first, v[0].second);
        k -= take * v[0].first;
        z += 2 * take;
        v.pop_front();
    }

    cout << z;

    return 0;
}


Compilation message

route.cpp:21:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   21 | int z, n, k; i64 a[N+3], t[N+3<<1],d[N+3<<1];
      |                             ^
route.cpp:21:39: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   21 | int z, n, k; i64 a[N+3], t[N+3<<1],d[N+3<<1];
      |                                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -