Submission #364540

# Submission time Handle Problem Language Result Execution time Memory
364540 2021-02-09T12:37:47 Z buyolitsez Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3505 ms 78196 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>

using namespace std;

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

#define eb emplace_back

typedef long long ll;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(seed);


const int MAXN = 1e6 + 15;
const ll ADD = 1e6;

int t[4 * MAXN], tadd[4 * MAXN];
int f[MAXN];

void Update(int pos, int val) {
    for(;pos < MAXN; pos = (pos | (pos + 1))) {
        f[pos] += val;
    }
}

int Sum(int pos) {
    int ans = 0;
    for(; pos >= 0; pos = (pos & (pos + 1)) - 1) {
        ans += f[pos];
    }
    return ans;
}

void Push(int v, int vl, int vr) {
    if (tadd[v]) {
        t[v] += tadd[v];
        if (vl != vr) {
            tadd[2 * v + 1] += tadd[v];
            tadd[2 * v + 2] += tadd[v];
        }
        tadd[v] = 0;
    }
}

void Update(int l, int r, int val, int v = 0, int vl = 0, int vr = MAXN - 1) {
    Push(v, vl, vr);
    if (vr < l || r < vl) {return;}
    if (vl >= l && vr <= r) {
        tadd[v] += val;
        Push(v, vl, vr);
        return;
    }
    int vm = vl + (vr - vl) / 2;
    Update(l, r, val, 2 * v + 1, vl, vm);
    Update(l, r, val, 2 * v + 2, vm + 1, vr);
    t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}

int Query(int pos, int v = 0, int vl = 0, int vr = MAXN - 1) {
    Push(v, vl, vr);
    if (vl == vr) {
        return t[v];
    }
    int vm = vl + (vr - vl) / 2;
    if (pos <= vm) {
        return Query(pos, 2 * v + 1, vl, vm);
    }
    return Query(pos, 2 * v + 2, vm + 1, vr);
}

int GetMx() {
    Push(0, 0, MAXN - 1);
    return t[0];
}

std::vector<int> countScans(std::vector<int> aa,std::vector<int> xx,std::vector<int> vv){
    fill(t, t + 4 * MAXN, -(int)1e9);
    vector <ll> a, x, v, all;
    int n = aa.size(), q = xx.size();
    for (int i = 0; i < n; ++i) {
        a.eb((ll)aa[i] * ADD + i);
        all.eb(a[i]);
    }
    for (int i = 0; i < xx.size(); ++i) {
        x.eb(xx[i]);
        v.eb((ll)vv[i] * ADD + xx[i]);
        all.eb(v[i]);
    }
    sort(all.begin(), all.end());
    all.resize(unique(all.begin(), all.end()) - all.begin());
    for (int i = 0; i < n; ++i) {
        a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin();
        Update(a[i], 1);
    }
    for (int i = 0; i < n; ++i) {
        int val = Query(a[i]);
        Update(a[i], a[i], -val);
        Update(a[i], a[i], i);
        int cntLess = Sum(a[i] - 1);
        Update(a[i], a[i], -cntLess);
    }
    for (auto &u : v) {
        u = lower_bound(all.begin(), all.end(), u) - all.begin();
    }
    vector <int> s;
    for(int f = 0; f < q; ++f) {
        int pos = x[f];
        int x = a[pos];
        int y = v[f];
        a[pos] = y;
        Update(x + 1, MAXN - 1, 1);
        Update(y + 1, MAXN - 1, -1);
        Update(x, -1);
        Update(y, 1);
        int val = Query(x);
        Update(x, x, -val - (int)1e9);
        int cntLess = Sum(y - 1);
        int nowVal = Query(y);
        Update(y, y, -nowVal + (pos - cntLess));
        s.eb(GetMx());
    }
    return s;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int i = 0; i < xx.size(); ++i) {
      |                     ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16364 KB Output is correct
2 Correct 14 ms 16236 KB Output is correct
3 Correct 19 ms 16492 KB Output is correct
4 Correct 19 ms 16364 KB Output is correct
5 Correct 19 ms 16364 KB Output is correct
6 Correct 19 ms 16364 KB Output is correct
7 Correct 19 ms 16364 KB Output is correct
8 Correct 21 ms 16364 KB Output is correct
9 Correct 20 ms 16364 KB Output is correct
10 Correct 20 ms 16364 KB Output is correct
11 Correct 19 ms 16492 KB Output is correct
12 Correct 19 ms 16364 KB Output is correct
13 Correct 19 ms 16364 KB Output is correct
14 Correct 19 ms 16364 KB Output is correct
15 Correct 19 ms 16364 KB Output is correct
16 Correct 19 ms 16364 KB Output is correct
17 Correct 19 ms 16364 KB Output is correct
18 Correct 19 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16364 KB Output is correct
2 Correct 14 ms 16236 KB Output is correct
3 Correct 19 ms 16492 KB Output is correct
4 Correct 19 ms 16364 KB Output is correct
5 Correct 19 ms 16364 KB Output is correct
6 Correct 19 ms 16364 KB Output is correct
7 Correct 19 ms 16364 KB Output is correct
8 Correct 21 ms 16364 KB Output is correct
9 Correct 20 ms 16364 KB Output is correct
10 Correct 20 ms 16364 KB Output is correct
11 Correct 19 ms 16492 KB Output is correct
12 Correct 19 ms 16364 KB Output is correct
13 Correct 19 ms 16364 KB Output is correct
14 Correct 19 ms 16364 KB Output is correct
15 Correct 19 ms 16364 KB Output is correct
16 Correct 19 ms 16364 KB Output is correct
17 Correct 19 ms 16364 KB Output is correct
18 Correct 19 ms 16364 KB Output is correct
19 Correct 42 ms 17004 KB Output is correct
20 Correct 47 ms 17132 KB Output is correct
21 Correct 46 ms 17132 KB Output is correct
22 Correct 52 ms 17152 KB Output is correct
23 Correct 46 ms 17004 KB Output is correct
24 Correct 46 ms 17004 KB Output is correct
25 Correct 45 ms 17004 KB Output is correct
26 Correct 45 ms 17004 KB Output is correct
27 Correct 45 ms 17004 KB Output is correct
28 Correct 44 ms 17004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 17384 KB Output is correct
2 Correct 151 ms 19088 KB Output is correct
3 Correct 256 ms 21212 KB Output is correct
4 Correct 256 ms 21084 KB Output is correct
5 Correct 309 ms 21084 KB Output is correct
6 Correct 257 ms 21084 KB Output is correct
7 Correct 254 ms 21084 KB Output is correct
8 Correct 255 ms 21084 KB Output is correct
9 Correct 246 ms 21084 KB Output is correct
10 Correct 217 ms 20572 KB Output is correct
11 Correct 225 ms 20700 KB Output is correct
12 Correct 226 ms 20588 KB Output is correct
13 Correct 219 ms 20572 KB Output is correct
14 Correct 214 ms 20572 KB Output is correct
15 Correct 220 ms 20756 KB Output is correct
16 Correct 214 ms 20572 KB Output is correct
17 Correct 212 ms 20444 KB Output is correct
18 Correct 211 ms 20444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16364 KB Output is correct
2 Correct 14 ms 16236 KB Output is correct
3 Correct 19 ms 16492 KB Output is correct
4 Correct 19 ms 16364 KB Output is correct
5 Correct 19 ms 16364 KB Output is correct
6 Correct 19 ms 16364 KB Output is correct
7 Correct 19 ms 16364 KB Output is correct
8 Correct 21 ms 16364 KB Output is correct
9 Correct 20 ms 16364 KB Output is correct
10 Correct 20 ms 16364 KB Output is correct
11 Correct 19 ms 16492 KB Output is correct
12 Correct 19 ms 16364 KB Output is correct
13 Correct 19 ms 16364 KB Output is correct
14 Correct 19 ms 16364 KB Output is correct
15 Correct 19 ms 16364 KB Output is correct
16 Correct 19 ms 16364 KB Output is correct
17 Correct 19 ms 16364 KB Output is correct
18 Correct 19 ms 16364 KB Output is correct
19 Correct 42 ms 17004 KB Output is correct
20 Correct 47 ms 17132 KB Output is correct
21 Correct 46 ms 17132 KB Output is correct
22 Correct 52 ms 17152 KB Output is correct
23 Correct 46 ms 17004 KB Output is correct
24 Correct 46 ms 17004 KB Output is correct
25 Correct 45 ms 17004 KB Output is correct
26 Correct 45 ms 17004 KB Output is correct
27 Correct 45 ms 17004 KB Output is correct
28 Correct 44 ms 17004 KB Output is correct
29 Correct 75 ms 17384 KB Output is correct
30 Correct 151 ms 19088 KB Output is correct
31 Correct 256 ms 21212 KB Output is correct
32 Correct 256 ms 21084 KB Output is correct
33 Correct 309 ms 21084 KB Output is correct
34 Correct 257 ms 21084 KB Output is correct
35 Correct 254 ms 21084 KB Output is correct
36 Correct 255 ms 21084 KB Output is correct
37 Correct 246 ms 21084 KB Output is correct
38 Correct 217 ms 20572 KB Output is correct
39 Correct 225 ms 20700 KB Output is correct
40 Correct 226 ms 20588 KB Output is correct
41 Correct 219 ms 20572 KB Output is correct
42 Correct 214 ms 20572 KB Output is correct
43 Correct 220 ms 20756 KB Output is correct
44 Correct 214 ms 20572 KB Output is correct
45 Correct 212 ms 20444 KB Output is correct
46 Correct 211 ms 20444 KB Output is correct
47 Correct 874 ms 35404 KB Output is correct
48 Correct 3228 ms 73520 KB Output is correct
49 Correct 3505 ms 77752 KB Output is correct
50 Correct 3466 ms 77880 KB Output is correct
51 Correct 3423 ms 77916 KB Output is correct
52 Correct 3458 ms 77796 KB Output is correct
53 Correct 3460 ms 78068 KB Output is correct
54 Correct 3216 ms 77940 KB Output is correct
55 Correct 3289 ms 78136 KB Output is correct
56 Correct 3197 ms 78108 KB Output is correct
57 Correct 3312 ms 78196 KB Output is correct
58 Correct 3174 ms 78180 KB Output is correct
59 Correct 2954 ms 75448 KB Output is correct
60 Correct 3004 ms 75448 KB Output is correct
61 Correct 3085 ms 75444 KB Output is correct
62 Correct 2936 ms 74520 KB Output is correct
63 Correct 2922 ms 74484 KB Output is correct
64 Correct 2967 ms 74468 KB Output is correct
65 Correct 2751 ms 73600 KB Output is correct
66 Correct 2773 ms 73584 KB Output is correct
67 Correct 2787 ms 73712 KB Output is correct