Submission #156189

# Submission time Handle Problem Language Result Execution time Memory
156189 2019-10-04T11:04:29 Z theboatman Money (IZhO17_money) C++17
0 / 100
16 ms 15992 KB
#include <bits/stdc++.h>

#define y1 theboatman
#define make_struct(args...) {args}

using namespace std;

const int inf = 1e9 + 10;
const int N = 1e6 + 10;

struct tree {
    vector <int> t;

    int tl, tr, v;
    void init(int n) {
        tl = 0;
        tr = n - 1;
        v = 1;

        t.resize(n * 4);
    }

    void inc(int v, int tl, int tr, int pos) {
        if (tl == tr) {
            t[v] |= 1;
        }
        else {
            int tm = tl + tr >> 1;
            if (pos <= tm) {
                inc(v << 1, tl, tm, pos);
            }
            else {
                inc(v << 1 | 1, tm + 1, tr, pos);
            }
        }

        t[v] = t[v << 1] + t[v << 1 | 1];
    }

    void inc(int pos) {
        inc(v, tl, tr, pos);
    }

    int get(int v, int tl, int tr, int l, int r) {
        if (l > r) {
            return 0;
        }

        if (tl == l && tr == r) {
            return t[v];
        }

        int tm = tl + tr >> 1;
        int ql = get(v << 1, tl, tm, l, min(r, tm));
        int qr = get(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r);

        return ql + qr;
    }

    int get(int l, int r) {
        return get(v, tl, tr, l, r);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    int n;
    cin >> n;

    vector <int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }

    int now = 0, x = 0;
    while(now < n && x <= a[now]) {
        x = a[now];
        now++;
    }

    tree bank;
    bank.init(N);

    set <int> s;
    int l = inf, r = -inf;

    for(int i = 0; i < now; i++) {
        bank.inc(a[i]);

        s.insert(a[i]);

        l = min(l, a[i]);
        r = max(r, a[i]);
    }

    int ans = 0;
    for(int i = now; i < n; i++) {
        ans++;

        //cout << a[i] << " " << l << " " << r << endl;
        if (a[i] >= r) {
            int x = 0;
            while(i < n && x <= a[i]) {
                x = a[i];
                r = a[i];

                bank.inc(x);
                s.insert(x);

                i++;
            }
            i--;
        }
        else {
            vector <int> lazy;
            if (a[i] < l) {
                int x = 0;
                while(i < n && x <= a[i] && a[i] <= l) {
                    x = a[i];
                    lazy.push_back(x);
                    i++;
                }
                i--;
            }
            else {
                int x = 0;
                int les = *--s.upper_bound(a[i]);

                while(i < n && x <= a[i]) {
                    auto more = s.lower_bound(a[i]);
                    if (more == s.end()) {
                        break;
                    }

                    int pos = bank.get(0, les);
                    int pos1 = bank.get(0, *more);

                    //cout << les << " " << *more << " " << pos << " " << pos1 << " " << i << endl;

                    if (pos1 - pos <= 1) {
                        x = a[i];
                        lazy.push_back(x);
                        i++;
                    }
                    else {
                        break;
                    }
                }

                i--;
            }

            for(auto it : lazy) {
                bank.inc(it);
                s.insert(it);

                l = min(l, it);
                r = max(r, it);
            }
        }
    }

    cout << ans + 1 << "\n";
    return 0;
}

/*
find_by_order(X) k -> элемент, нумерация с нуля
order_of_key(X) кол-во < чем X
*/

Compilation message

money.cpp: In member function 'void tree::inc(int, int, int, int)':
money.cpp:28:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
             int tm = tl + tr >> 1;
                      ~~~^~~~
money.cpp: In member function 'int tree::get(int, int, int, int, int)':
money.cpp:53:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int tm = tl + tr >> 1;
                  ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15992 KB Output is correct
2 Incorrect 16 ms 15992 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15992 KB Output is correct
2 Incorrect 16 ms 15992 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15992 KB Output is correct
2 Incorrect 16 ms 15992 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15992 KB Output is correct
2 Incorrect 16 ms 15992 KB Output isn't correct
3 Halted 0 ms 0 KB -