Submission #891937

# Submission time Handle Problem Language Result Execution time Memory
891937 2023-12-24T13:29:51 Z hafo Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
1701 ms 64996 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e6 + 7;
const ll oo = 1e9 + 69;

int n, q;
vector<pa> val;

struct ST {
    struct node {
        int mx;
        friend node operator + (node a, const node &b) {
            maxi(a.mx, b.mx);
            return a;
        }

    };

    node st[4 * maxn];
    int lz[4 * maxn];

    void init(int n) {
        for(int i = 0; i <= 4 * n; i++) st[i].mx = -oo;
    }

    void fix(int id, int l, int r) {
        if(!lz[id]) return;
        st[id].mx += lz[id];
        if(l != r) {
            lz[id << 1] += lz[id];
            lz[id << 1 | 1] += lz[id];
        }
        lz[id] = 0;
    }   

    void update2(int id, int l, int r, int pos, int val) {
        fix(id, l, r);
        if(pos < l || pos > r) return;
        if(l == r) {
            st[id].mx = val;
            return;
        }
        int mid = l + r >> 1;
        update2(id << 1, l, mid, pos, val);
        update2(id << 1 | 1, mid + 1, r, pos, val);
        st[id] = st[id << 1] + st[id << 1 | 1];
    }

    void update(int id, int l, int r, int u, int v, int val) {
        fix(id, l, r);
        if(r < u || l > v) return;
        if(u <= l && r <= v) {
            lz[id] += val;
            fix(id, l, r);
            return;
        }
        int mid = l + r >> 1;
        update(id << 1, l, mid, u, v, val);
        update(id << 1 | 1, mid + 1, r, u, v, val);
        st[id] = st[id << 1] + st[id << 1 | 1];
    }

    int get() {
        fix(1, 1, Size(val));
        return st[1].mx;
    }

} st;

struct BIT {
    int bit[maxn];

    void update(int x, int d) {
        for(; x <= Size(val); x += x&-x) bit[x] += d;
    }
    
    int get(int x) {
        int ans = 0;
        for(; x; x -= x&-x) ans += bit[x];
        return ans;
    }

} bit;

int id(int v, int x) {
    return lower_bound(all(val), make_pair(v, x)) - val.begin() + 1;
}

vector<int> countScans(vector<int> a, vector<int> x, vector<int> v) {
    n = Size(a);
    q = Size(x);
    for(int i = 0; i < n; i++) val.pb({a[i], i});
    for(int i = 0; i < q; i++) val.pb({v[i], x[i]});
    sort(all(val));
    val.erase(unique(all(val)), val.end());

    st.init(Size(val));
    for(int i = 0; i < n; i++) {
        bit.update(id(a[i], i), 1);
    }
    for(int i = 0; i < n; i++) {
        int idd = id(a[i], i);
        st.update2(1, 1, Size(val), idd, i - bit.get(idd - 1));
    }

    vector<int> res;
    for(int i = 0; i < q; i++) {
        int idd = id(a[x[i]], x[i]);
        st.update(1, 1, Size(val), idd + 1, Size(val), 1);
        st.update2(1, 1, Size(val), idd, -oo);
        bit.update(idd, -1);

        a[x[i]] = v[i];

        idd = id(a[x[i]], x[i]);
        st.update(1, 1, Size(val), idd + 1, Size(val), -1);
        st.update2(1, 1, Size(val), idd, x[i] - bit.get(idd - 1));
        bit.update(idd, 1);
        res.pb(st.get());
    }
    return res;
}

Compilation message

bubblesort2.cpp: In member function 'void ST::update2(int, int, int, int, int)':
bubblesort2.cpp:59:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int mid = l + r >> 1;
      |                   ~~^~~
bubblesort2.cpp: In member function 'void ST::update(int, int, int, int, int, int)':
bubblesort2.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 3 ms 2508 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 4 ms 2652 KB Output is correct
7 Correct 3 ms 2652 KB Output is correct
8 Correct 3 ms 2652 KB Output is correct
9 Correct 3 ms 2652 KB Output is correct
10 Correct 3 ms 2696 KB Output is correct
11 Correct 3 ms 2648 KB Output is correct
12 Correct 3 ms 2652 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 3 ms 2664 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 3 ms 2648 KB Output is correct
17 Correct 4 ms 2648 KB Output is correct
18 Correct 3 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 3 ms 2508 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 4 ms 2652 KB Output is correct
7 Correct 3 ms 2652 KB Output is correct
8 Correct 3 ms 2652 KB Output is correct
9 Correct 3 ms 2652 KB Output is correct
10 Correct 3 ms 2696 KB Output is correct
11 Correct 3 ms 2648 KB Output is correct
12 Correct 3 ms 2652 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 3 ms 2664 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 3 ms 2648 KB Output is correct
17 Correct 4 ms 2648 KB Output is correct
18 Correct 3 ms 2648 KB Output is correct
19 Correct 12 ms 3164 KB Output is correct
20 Correct 14 ms 3164 KB Output is correct
21 Correct 13 ms 3344 KB Output is correct
22 Correct 13 ms 3164 KB Output is correct
23 Correct 13 ms 3164 KB Output is correct
24 Correct 13 ms 3160 KB Output is correct
25 Correct 13 ms 3164 KB Output is correct
26 Correct 12 ms 3296 KB Output is correct
27 Correct 12 ms 3164 KB Output is correct
28 Correct 12 ms 3108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 5588 KB Output is correct
2 Correct 57 ms 8904 KB Output is correct
3 Correct 103 ms 10948 KB Output is correct
4 Correct 109 ms 10772 KB Output is correct
5 Correct 96 ms 10532 KB Output is correct
6 Correct 96 ms 10692 KB Output is correct
7 Correct 98 ms 10780 KB Output is correct
8 Correct 96 ms 10716 KB Output is correct
9 Correct 95 ms 10680 KB Output is correct
10 Correct 75 ms 10184 KB Output is correct
11 Correct 74 ms 10136 KB Output is correct
12 Correct 74 ms 10188 KB Output is correct
13 Correct 72 ms 10184 KB Output is correct
14 Correct 72 ms 10088 KB Output is correct
15 Correct 71 ms 10184 KB Output is correct
16 Correct 71 ms 9672 KB Output is correct
17 Correct 69 ms 9668 KB Output is correct
18 Correct 69 ms 9640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 3 ms 2508 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 4 ms 2652 KB Output is correct
7 Correct 3 ms 2652 KB Output is correct
8 Correct 3 ms 2652 KB Output is correct
9 Correct 3 ms 2652 KB Output is correct
10 Correct 3 ms 2696 KB Output is correct
11 Correct 3 ms 2648 KB Output is correct
12 Correct 3 ms 2652 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 3 ms 2664 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 3 ms 2648 KB Output is correct
17 Correct 4 ms 2648 KB Output is correct
18 Correct 3 ms 2648 KB Output is correct
19 Correct 12 ms 3164 KB Output is correct
20 Correct 14 ms 3164 KB Output is correct
21 Correct 13 ms 3344 KB Output is correct
22 Correct 13 ms 3164 KB Output is correct
23 Correct 13 ms 3164 KB Output is correct
24 Correct 13 ms 3160 KB Output is correct
25 Correct 13 ms 3164 KB Output is correct
26 Correct 12 ms 3296 KB Output is correct
27 Correct 12 ms 3164 KB Output is correct
28 Correct 12 ms 3108 KB Output is correct
29 Correct 17 ms 5588 KB Output is correct
30 Correct 57 ms 8904 KB Output is correct
31 Correct 103 ms 10948 KB Output is correct
32 Correct 109 ms 10772 KB Output is correct
33 Correct 96 ms 10532 KB Output is correct
34 Correct 96 ms 10692 KB Output is correct
35 Correct 98 ms 10780 KB Output is correct
36 Correct 96 ms 10716 KB Output is correct
37 Correct 95 ms 10680 KB Output is correct
38 Correct 75 ms 10184 KB Output is correct
39 Correct 74 ms 10136 KB Output is correct
40 Correct 74 ms 10188 KB Output is correct
41 Correct 72 ms 10184 KB Output is correct
42 Correct 72 ms 10088 KB Output is correct
43 Correct 71 ms 10184 KB Output is correct
44 Correct 71 ms 9672 KB Output is correct
45 Correct 69 ms 9668 KB Output is correct
46 Correct 69 ms 9640 KB Output is correct
47 Correct 348 ms 26552 KB Output is correct
48 Correct 1557 ms 61204 KB Output is correct
49 Correct 1701 ms 64552 KB Output is correct
50 Correct 1691 ms 64688 KB Output is correct
51 Correct 1648 ms 64736 KB Output is correct
52 Correct 1622 ms 64592 KB Output is correct
53 Correct 1685 ms 64560 KB Output is correct
54 Correct 1512 ms 64844 KB Output is correct
55 Correct 1565 ms 64948 KB Output is correct
56 Correct 1512 ms 64644 KB Output is correct
57 Correct 1599 ms 64808 KB Output is correct
58 Correct 1562 ms 64996 KB Output is correct
59 Correct 1345 ms 62576 KB Output is correct
60 Correct 1321 ms 62492 KB Output is correct
61 Correct 1417 ms 62724 KB Output is correct
62 Correct 1293 ms 62596 KB Output is correct
63 Correct 1323 ms 62680 KB Output is correct
64 Correct 1280 ms 62256 KB Output is correct
65 Correct 1193 ms 59896 KB Output is correct
66 Correct 1143 ms 60200 KB Output is correct
67 Correct 1140 ms 60112 KB Output is correct