Submission #1105071

#TimeUsernameProblemLanguageResultExecution timeMemory
1105071underwaterkillerwhaleExercise Deadlines (CCO20_day1problem2)C++17
25 / 25
367 ms8956 KiB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 2e5 + 7;
const int Mod = 1e6 + 3;///lon
const ll INF = 1e18 + 7;
const ll BASE = 137;
const int szBL = 250;

int n;
int a[N];

struct Segment_Tree {
    int m;
    pii st[N << 2];

    void build (int id, int l, int r) {
        if (l == r) {
            st[id] = {a[l], 1};
            return;
        }
        int mid = l + r >> 1;
        build (id << 1, l, mid);
        build (id << 1 | 1, mid + 1, r);
        st[id].fs = max(st[id << 1].fs, st[id << 1 | 1].fs);
        st[id].se = st[id << 1].se + st[id << 1 | 1].se;
    }
    void init (int n) {
        m = n;
        build (1, 1, m);
    }

    void update (int id, int l, int r, int pos) {
        if (l > pos || r < pos) return;
        if (l == r) {
            st[id] = {0, 0};
            return;
        }
        int mid = l + r >> 1;
        update (id << 1, l, mid, pos);
        update (id << 1 | 1, mid + 1, r, pos);
        st[id].fs = max(st[id << 1].fs, st[id << 1 | 1].fs);
        st[id].se = st[id << 1].se + st[id << 1 | 1].se;
    }

    int get (int id, int l, int r, int u, int v) {
        if (l > v || r < u) return 0;
        if (l >= u && r <= v) return st[id].fs;
        int mid = l + r >> 1;
        return max (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
    }

    int cnt (int id, int l, int r, int u, int v) {
        if (l > v || r < u) return 0;
        if (l >= u && r <= v) return st[id].se;
        int mid = l + r >> 1;
        return cnt (id << 1, l, mid, u, v) + cnt (id << 1 | 1, mid + 1, r, u, v);
    }

    void update (int pos) {
        update (1, 1, m, pos);
    }

    int get (int u, int v) {
        return get (1, 1, m, u, v);
    }

    int cnt (int u, int v) {
        return cnt (1, 1, m, u, v);
    }
}ST;

void solution () {
    cin >> n;
    rep (i, 1, n) cin >> a[i];
    ST.init(n);
    ll res = 0;
    reb (i, n, 1) {
        int pos; {
            int lf = 1, rt = n;
            while (lf < rt) {
                int mid = lf + rt + 1>> 1;
                if (ST.get(mid, n) >= i) lf = mid;
                else rt = mid - 1;
            }
            if (ST.get(lf, n) < i) pos = -1;
            else pos = lf;
        }
        if (pos == -1) {
            cout << -1 <<"\n";
            return;
        }
        res += ST.cnt(pos, n) - 1;
        ST.update(pos);
    }
    cout << res <<"\n";
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +12

*/

Compilation message (stderr)

Main.cpp: In member function 'void Segment_Tree::build(int, int, int)':
Main.cpp:39:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   39 |         int mid = l + r >> 1;
      |                   ~~^~~
Main.cpp: In member function 'void Segment_Tree::update(int, int, int, int)':
Main.cpp:56:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |         int mid = l + r >> 1;
      |                   ~~^~~
Main.cpp: In member function 'int Segment_Tree::get(int, int, int, int, int)':
Main.cpp:66:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |         int mid = l + r >> 1;
      |                   ~~^~~
Main.cpp: In member function 'int Segment_Tree::cnt(int, int, int, int, int)':
Main.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |         int mid = l + r >> 1;
      |                   ~~^~~
Main.cpp: In function 'void solution()':
Main.cpp:99:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |                 int mid = lf + rt + 1>> 1;
      |                           ~~~~~~~~^~~
Main.cpp: At global scope:
Main.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  118 | main () {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...