Submission #887931

# Submission time Handle Problem Language Result Execution time Memory
887931 2023-12-15T14:00:42 Z nnhzzz Po (COCI21_po) C++14
70 / 70
74 ms 13480 KB
// cre: Nguyen Ngoc Hung - Train VOI 2024 :>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>

using namespace std;

#define    SORT_UNIQUE(v)  sort(ALL(v)),(v).resize(unique(ALL(v))-(v).begin())
#define        __nnhzzz__  signed main()
#define          BIT(i,j)  ((i>>j)&1LL)
#define           MASK(i)  (1LL<<i)
#define           RALL(x)  (x).rbegin(),(x).rend()
#define            ALL(x)  (x).begin(),(x).end()
#define             SZ(x)  (int)(x).size()
#define                fi  first
#define                se  second
#define               INF  0x3f3f3f3f
#define                ll  long long
#define               ull  unsigned long long
#define                ld  long double
#define                vi  vector<int>
#define               vvi  vector<vi>
#define              vvvi  vector<vvi>
#define             vvvvi  vector<vvvi>
#define               pii  pair<int,int>
#define              vpii  vector<pii>
#define      RESET(x,val)  memset((x),(val),sizeof(x))
#define REPDIS(i,be,en,j)  for(int i = (be); i<=(en); i+=j)
#define     REPD(i,be,en)  for(int i = (be); i>=(en); i--)
#define      REP(i,be,en)  for(int i = (be); i<=(en); i++)
//-------------------------------------------------------------//
const int oo = 1e9,LOG = 20,MAXN = 1e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }

/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Nguyen Ngoc Hung - nnhzzz
    Training for VOI24 gold medal
----------------------------------------------------------------
*/

int a[MAXN];

struct SEGMENT_TREE{
    vpii st;
    vi lazy;
    int n;

    void pushdown(int id, int l, int r){
        if(l==r || lazy[id]==0){
            return ;
        }
        st[id<<1].fi -= lazy[id];
        st[id<<1|1].fi -= lazy[id];
        lazy[id<<1] += lazy[id];
        lazy[id<<1|1] += lazy[id];
        lazy[id] = 0;
    }

    void build(int id, int l, int r){
        if(l==r){
            st[id] = {a[l],l};
            return ;
        }
        int mid = (l+r)>>1;
        build(id<<1,l,mid);
        build(id<<1|1,mid+1,r);
        if(st[id<<1].fi<=st[id<<1|1].fi) st[id] = st[id<<1];
        else st[id] = st[id<<1|1];
    }

    void update(int id, int l, int r, int lo, int hi, int val){
        if(l>hi || r<lo){
            return ;
        }
        if(l>=lo && r<=hi){
            st[id].fi -= val;
            lazy[id] += val;
            return ;
        }
        pushdown(id,l,r);
        int mid = (l+r)>>1;
        update(id<<1,l,mid,lo,hi,val);
        update(id<<1|1,mid+1,r,lo,hi,val);
        if(st[id<<1].fi<=st[id<<1|1].fi) st[id] = st[id<<1];
        else st[id] = st[id<<1|1];
    }

    pii get(int id, int l, int r, int lo, int hi){
        if(l>hi || r<lo){
            return {oo,n+1};
        }
        if(l>=lo && r<=hi){
            return st[id];
        }
        pushdown(id,l,r);
        int mid = (l+r)>>1;
        pii res = get(id<<1,l,mid,lo,hi);
        pii tmp = get(id<<1|1,mid+1,r,lo,hi);
        if(res.fi<=tmp.fi){
            return res;
        }else{
            return tmp;
        }
    }

    pii get(int l, int r){
        return get(1,1,n,l,r);
    }

    void update(int l, int r, int val){
        update(1,1,n,l,r,val);
    }

    SEGMENT_TREE(int _ = 0):n(_){
        st.assign(n<<2|1,{0,0});
        lazy.assign(n<<2|1,0);
    }
};

int res = 0,n;
SEGMENT_TREE seg;

void dnc(int l, int r, int x){
    if(l>r) return ;
    seg.update(l,r,x);
    auto [mi,pos] = seg.get(l,r);
    res += (mi>0);
    dnc(l,pos-1,mi);
    dnc(pos+1,r,mi);
}

void solve(){
    int mi = oo; cin >> n;
    REP(i,1,n){
        cin >> a[i];
        mini(mi,a[i]);
    }
    seg = SEGMENT_TREE(n);
    seg.build(1,1,n);
    dnc(1,n,mi);
    cout << res+(mi>0);
}

__nnhzzz__{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define name "test"
    if(fopen(name".inp","r")){
        freopen(name".inp","r",stdin);
        freopen(name".out","w",stdout);
    }
    #define name1 "nnhzzz"
    if(fopen(name1".inp","r")){
        freopen(name1".inp","r",stdin);
        freopen(name1".out","w",stdout);
    }

    int test = 1;

    while(test--){
      solve();
    }
    cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
    return 0;
}

Compilation message

Main.cpp: In function 'void dnc(int, int, int)':
Main.cpp:171:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  171 |     auto [mi,pos] = seg.get(l,r);
      |          ^
Main.cpp: In function 'int main()':
Main.cpp:194:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  194 |         freopen(name".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:195:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |         freopen(name".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:199:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  199 |         freopen(name1".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  200 |         freopen(name1".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 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 20 ms 3144 KB Output is correct
5 Correct 29 ms 4444 KB Output is correct
6 Correct 66 ms 11040 KB Output is correct
7 Correct 74 ms 13480 KB Output is correct