Submission #201808

# Submission time Handle Problem Language Result Execution time Memory
201808 2020-02-12T04:39:14 Z tri Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
5105 ms 81748 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace __gnu_pbds;

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


// set
typedef tree<
        pi,
        null_type,
        less<pi>,
        rb_tree_tag,
        tree_order_statistics_node_update>
        ordered_set;

const int MAXN = 10e5 + 100;
const int INF = 1e9;

struct LSegTr {
    int tr[4 * MAXN], lz[4 * MAXN];

    void push(int i, int l, int r) {
        tr[i] += lz[i];
        if (l != r) {
            lz[i * 2] += lz[i];
            lz[i * 2 + 1] += lz[i];
        }
        lz[i] = 0;
    }

    int q(int i, int l, int r, int s, int e) {
        if (e < l || r < s) {
            return -INF;
        }
        push(i, l, r);
        if (s <= l && r <= e) {
            return tr[i];
        }
        int mid = (l + r) / 2;
        return max(q(i * 2, l, mid, s, e), q(i * 2 + 1, mid + 1, r, s, e));
    }

    void u(int i, int l, int r, int s, int e, int d) {
        push(i, l, r); // pushed early to use in recalculation of parent
        if (e < l || r < s) {
            return;
        }
        if (s <= l && r <= e) {
            lz[i] += d;
            push(i, l, r);
            return;
        }
        int mid = (l + r) / 2;
        u(i * 2, l, mid, s, e, d);
        u(i * 2 + 1, mid + 1, r, s, e, d);
        tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
    }

    void set(int i, int l, int r, int x, int v) {
        push(i, l, r);
        if (l == r) {
            tr[i] = v;
            return;
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            set(i * 2, l, mid, x, v);
            push(i * 2 + 1, mid + 1, r);
        } else {
            set(i * 2 + 1, mid + 1, r, x, v);
            push(i * 2, l, mid);
        }
        tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
    }

    void b(int i, int l, int r, vi &init) {
        lz[i] = 0;
        if (l == r) {
            tr[i] = init[l];
            return;
        }
        int mid = (l + r) / 2;
        b(i * 2, l, mid, init);
        b(i * 2 + 1, mid + 1, r, init);
        tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
    }

} lSegTr;


int N, Q;

ordered_set vals;

vi countScans(vi A, vi X, vi V) {
    vector<pi> allVal;

    vi a(A.begin(), A.end());
    N = a.size();
    Q = X.size();

    for (int i = 0; i < N; i++) {
        allVal.pb({a[i], i});
    }

    for (int i = 0; i < Q; i++) {
        allVal.pb({V[i], X[i]});
    }

    sort(allVal.begin(), allVal.end());
    allVal.erase(unique(allVal.begin(), allVal.end()), allVal.end());

//    ps(allVal);

    for (int i = 0; i < N; i++) {
        vals.insert({a[i], i});
    }

    int M = allVal.size();

    vi init(M, -INF);

    for (int cP = 0; cP < N; cP++) {
        pi cVal = {a[cP], cP};
        int gP = vals.order_of_key(cVal);

        int index = lower_bound(allVal.begin(), allVal.end(), cVal) - allVal.begin();
        init[index] = cP - gP;
    }

//    ps(init);

    lSegTr.b(1, 0, M - 1, init);


//    for (int i = 0; i < M; i++) {
//        pr(lSegTr.q(1, 0, M - 1, i, i), " ");
//    }
//    ps();
//    ps("checkAll: ", lSegTr.q(1, 0, M - 1, 0, 6));


    vi ans(Q);
    for (int cQ = 0; cQ < Q; cQ++) {
        int cP = X[cQ];
        pi oV = {a[cP], cP};
        pi nV = {V[cQ], cP};

        a[cP] = V[cQ];
        vals.erase(oV);
        vals.insert(nV);

        int oIndex = lower_bound(allVal.begin(), allVal.end(), oV) - allVal.begin();
        int nIndex = lower_bound(allVal.begin(), allVal.end(), nV) - allVal.begin();

        if (oIndex + 1 < M) {
//            ps(oIndex + 1, M - 1, +1);
            lSegTr.u(1, 0, M - 1, oIndex + 1, M - 1, +1);
        }

//        for (int i = 0; i < M; i++) {
//            pr(lSegTr.q(1, 0, M - 1, i, i), " ");
//        }
//        ps();
//        ps("checkAll: ", lSegTr.q(1, 0, M - 1, 0, 6));


        if (nIndex + 1 < M) {
//            ps(nIndex + 1, M - 1, -1);
            lSegTr.u(1, 0, M - 1, nIndex + 1, M - 1, -1);
        }

//        for (int i = 0; i < M; i++) {
//            pr(lSegTr.q(1, 0, M - 1, i, i), " ");
//        }
//        ps();
//        ps("checkAll: ", lSegTr.q(1, 0, M - 1, 0, 6));


//        ps(cQ);
//        ps(oV, nV);
//        ps(oIndex, nIndex);

        lSegTr.set(1, 0, M - 1, oIndex, -INF);

        int gP = vals.order_of_key(nV);
        lSegTr.set(1, 0, M - 1, nIndex, cP - gP);

        ans[cQ] = lSegTr.q(1, 0, M - 1, 0, M - 1);

//        ps(a);

//        for (int i = 0; i < M; i++) {
//            pr(lSegTr.q(1, 0, M - 1, i, i), " ");
//        }
//        ps();
//        ps("checkAll: ", lSegTr.q(1, 0, M - 1, 0, M - 1));
//        ps("check: ", lSegTr.q(1, 0, M - 1, 4, 4));
//        ps("check: ", lSegTr.q(1, 0, M - 1, 4, 5));
//        ps("check: ", lSegTr.q(1, 0, M - 1, 4, 6));
//        ps("check: ", lSegTr.q(1, 0, M - 1, 0, 6));
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 11 ms 636 KB Output is correct
4 Correct 11 ms 636 KB Output is correct
5 Correct 10 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 12 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 10 ms 632 KB Output is correct
12 Correct 10 ms 632 KB Output is correct
13 Correct 10 ms 632 KB Output is correct
14 Correct 10 ms 632 KB Output is correct
15 Correct 10 ms 632 KB Output is correct
16 Correct 11 ms 760 KB Output is correct
17 Correct 10 ms 632 KB Output is correct
18 Correct 10 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 11 ms 636 KB Output is correct
4 Correct 11 ms 636 KB Output is correct
5 Correct 10 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 12 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 10 ms 632 KB Output is correct
12 Correct 10 ms 632 KB Output is correct
13 Correct 10 ms 632 KB Output is correct
14 Correct 10 ms 632 KB Output is correct
15 Correct 10 ms 632 KB Output is correct
16 Correct 11 ms 760 KB Output is correct
17 Correct 10 ms 632 KB Output is correct
18 Correct 10 ms 632 KB Output is correct
19 Correct 31 ms 1400 KB Output is correct
20 Correct 32 ms 1528 KB Output is correct
21 Correct 31 ms 1528 KB Output is correct
22 Correct 31 ms 1528 KB Output is correct
23 Correct 30 ms 1528 KB Output is correct
24 Correct 32 ms 1528 KB Output is correct
25 Correct 29 ms 1452 KB Output is correct
26 Correct 29 ms 1528 KB Output is correct
27 Correct 29 ms 1528 KB Output is correct
28 Correct 28 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 3316 KB Output is correct
2 Correct 130 ms 5616 KB Output is correct
3 Correct 242 ms 8300 KB Output is correct
4 Correct 249 ms 8300 KB Output is correct
5 Correct 249 ms 8428 KB Output is correct
6 Correct 240 ms 8300 KB Output is correct
7 Correct 240 ms 8300 KB Output is correct
8 Correct 244 ms 8300 KB Output is correct
9 Correct 238 ms 8300 KB Output is correct
10 Correct 197 ms 7148 KB Output is correct
11 Correct 188 ms 7020 KB Output is correct
12 Correct 191 ms 7020 KB Output is correct
13 Correct 190 ms 7148 KB Output is correct
14 Correct 187 ms 7020 KB Output is correct
15 Correct 197 ms 7020 KB Output is correct
16 Correct 180 ms 7020 KB Output is correct
17 Correct 180 ms 7020 KB Output is correct
18 Correct 187 ms 7020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 11 ms 636 KB Output is correct
4 Correct 11 ms 636 KB Output is correct
5 Correct 10 ms 632 KB Output is correct
6 Correct 10 ms 632 KB Output is correct
7 Correct 12 ms 632 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 10 ms 632 KB Output is correct
10 Correct 10 ms 632 KB Output is correct
11 Correct 10 ms 632 KB Output is correct
12 Correct 10 ms 632 KB Output is correct
13 Correct 10 ms 632 KB Output is correct
14 Correct 10 ms 632 KB Output is correct
15 Correct 10 ms 632 KB Output is correct
16 Correct 11 ms 760 KB Output is correct
17 Correct 10 ms 632 KB Output is correct
18 Correct 10 ms 632 KB Output is correct
19 Correct 31 ms 1400 KB Output is correct
20 Correct 32 ms 1528 KB Output is correct
21 Correct 31 ms 1528 KB Output is correct
22 Correct 31 ms 1528 KB Output is correct
23 Correct 30 ms 1528 KB Output is correct
24 Correct 32 ms 1528 KB Output is correct
25 Correct 29 ms 1452 KB Output is correct
26 Correct 29 ms 1528 KB Output is correct
27 Correct 29 ms 1528 KB Output is correct
28 Correct 28 ms 1528 KB Output is correct
29 Correct 36 ms 3316 KB Output is correct
30 Correct 130 ms 5616 KB Output is correct
31 Correct 242 ms 8300 KB Output is correct
32 Correct 249 ms 8300 KB Output is correct
33 Correct 249 ms 8428 KB Output is correct
34 Correct 240 ms 8300 KB Output is correct
35 Correct 240 ms 8300 KB Output is correct
36 Correct 244 ms 8300 KB Output is correct
37 Correct 238 ms 8300 KB Output is correct
38 Correct 197 ms 7148 KB Output is correct
39 Correct 188 ms 7020 KB Output is correct
40 Correct 191 ms 7020 KB Output is correct
41 Correct 190 ms 7148 KB Output is correct
42 Correct 187 ms 7020 KB Output is correct
43 Correct 197 ms 7020 KB Output is correct
44 Correct 180 ms 7020 KB Output is correct
45 Correct 180 ms 7020 KB Output is correct
46 Correct 187 ms 7020 KB Output is correct
47 Correct 1159 ms 26572 KB Output is correct
48 Correct 4715 ms 69048 KB Output is correct
49 Correct 5091 ms 81724 KB Output is correct
50 Correct 5105 ms 81724 KB Output is correct
51 Correct 5068 ms 81620 KB Output is correct
52 Correct 4984 ms 81480 KB Output is correct
53 Correct 4896 ms 81580 KB Output is correct
54 Correct 4389 ms 81492 KB Output is correct
55 Correct 4794 ms 81556 KB Output is correct
56 Correct 4379 ms 81748 KB Output is correct
57 Correct 4689 ms 81692 KB Output is correct
58 Correct 4323 ms 81620 KB Output is correct
59 Correct 3907 ms 81108 KB Output is correct
60 Correct 3891 ms 80984 KB Output is correct
61 Correct 3886 ms 81108 KB Output is correct
62 Correct 3647 ms 80920 KB Output is correct
63 Correct 3658 ms 80852 KB Output is correct
64 Correct 3583 ms 80724 KB Output is correct
65 Correct 3385 ms 80640 KB Output is correct
66 Correct 3300 ms 80728 KB Output is correct
67 Correct 3356 ms 80800 KB Output is correct