Submission #256504

# Submission time Handle Problem Language Result Execution time Memory
256504 2020-08-02T19:02:28 Z IgorI Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
4963 ms 128628 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

const int N = 2000002;

int tree[4 * N];
int valu[4 * N];
int push[4 * N];
int act[4 * N];

void Push(int V, int L, int M, int R)
{
    tree[2 * V + 1] += push[V];
    valu[2 * V + 1] += push[V];
    push[2 * V + 1] += push[V];
    tree[2 * V + 2] += push[V];
    valu[2 * V + 2] += push[V];
    push[2 * V + 2] += push[V];
    push[V] = 0;
    if (L + 1 == M && !act[2 * V + 1]) tree[2 * V + 1] = 0;
    if (M + 1 == R && !act[2 * V + 2]) tree[2 * V + 2] = 0;
}

void Active(int pos, int L = 0, int R = N, int V = 0)
{
    if (L + 1 == R)
    {
        act[V] ^= 1;
        if (act[V]) tree[V] = valu[V];
        else tree[V] = 0;
        return;
    }
    int M = (L + R) / 2;
    Push(V, L, M, R);
    if (pos < M) Active(pos, L, M, 2 * V + 1);
    else Active(pos, M, R, 2 * V + 2);
    tree[V] = max(tree[2 * V + 1], tree[2 * V + 2]);
}

void Add(int l, int r, int x, int L = 0, int R = N, int V = 0)
{
    if (l <= L && R <= r)
    {
        tree[V] += x;
        valu[V] += x;
        push[V] += x;
        if (L + 1 == R && !act[V]) tree[V] = 0;
        return;
    }
    if (R <= l || r <= L)
    {
        return;
    }
    int M = (L + R) / 2;
    Push(V, L, M, R);
    Add(l, r, x, L, M, 2 * V + 1);
    Add(l, r, x, M, R, 2 * V + 2);
    tree[V] = max(tree[2 * V + 1], tree[2 * V + 2]);
}

int Get()
{
    return tree[0];
}

vi countScans(vi a, vi x, vi v)
{
    vector<pair<int, int> > elems;
    int n = a.size();
    int q = x.size();
    for (int i = 0; i < n; i++)
    {
        elems.push_back({a[i], i});
    }
    for (int i = 0; i < q; i++)
    {
        elems.push_back({v[i], x[i]});
    }
    uniq(elems);
    map<pii, int> mm;
    for (int i = 0; i < elems.size(); i++)
    {
        mm[elems[i]] = i;
    }
    for (int i = 0; i < elems.size(); i++)
    {
        Add(i, i + 1, elems[i].second);
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = mm[{a[i], i}];
        Active(a[i]);
        Add(a[i] + 1, elems.size(), -1);
    }
    vi ans(q);
    for (int i = 0; i < q; i++)
    {
        int pos = x[i];
        Add(a[pos] + 1, elems.size(), 1);
        Active(a[pos]);
        a[pos] = v[i];
        a[pos] = mm[{a[pos], pos}];
        Active(a[pos]);
        Add(a[pos] + 1, elems.size(), -1);
        ans[i] = Get();
    }
    return ans;
}

#ifdef LOCAL
signed main()
{
    int n, q;
    cin >> n >> q;
    vi a(n), x(q), v(q);
    forn(i, n) cin >> a[i];
    forn(i, q) cin >> x[i] >> v[i];
    vi ans = countScans(a, x, v);
    forn(i, q) cout << ans[i] << "\n";
}
#endif // LOCAL

/*
4 2
1 2 3 4
0 3
2 1

10 20
1 2 3 4 5 1 2 3 4 5
0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
*/

Compilation message

bubblesort2.cpp: In function 'vi countScans(vi, vi, vi)':
bubblesort2.cpp:108:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < elems.size(); i++)
                     ~~^~~~~~~~~~~~~~
bubblesort2.cpp:112:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < elems.size(); i++)
                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 768 KB Output is correct
3 Correct 9 ms 1024 KB Output is correct
4 Correct 9 ms 1024 KB Output is correct
5 Correct 9 ms 1024 KB Output is correct
6 Correct 8 ms 1024 KB Output is correct
7 Correct 9 ms 1024 KB Output is correct
8 Correct 8 ms 1024 KB Output is correct
9 Correct 8 ms 1024 KB Output is correct
10 Correct 8 ms 896 KB Output is correct
11 Correct 8 ms 896 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 896 KB Output is correct
14 Correct 8 ms 896 KB Output is correct
15 Correct 9 ms 896 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 9 ms 896 KB Output is correct
18 Correct 9 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 768 KB Output is correct
3 Correct 9 ms 1024 KB Output is correct
4 Correct 9 ms 1024 KB Output is correct
5 Correct 9 ms 1024 KB Output is correct
6 Correct 8 ms 1024 KB Output is correct
7 Correct 9 ms 1024 KB Output is correct
8 Correct 8 ms 1024 KB Output is correct
9 Correct 8 ms 1024 KB Output is correct
10 Correct 8 ms 896 KB Output is correct
11 Correct 8 ms 896 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 896 KB Output is correct
14 Correct 8 ms 896 KB Output is correct
15 Correct 9 ms 896 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 9 ms 896 KB Output is correct
18 Correct 9 ms 896 KB Output is correct
19 Correct 34 ms 2176 KB Output is correct
20 Correct 38 ms 2424 KB Output is correct
21 Correct 47 ms 2424 KB Output is correct
22 Correct 37 ms 2424 KB Output is correct
23 Correct 42 ms 2208 KB Output is correct
24 Correct 34 ms 2176 KB Output is correct
25 Correct 36 ms 2048 KB Output is correct
26 Correct 36 ms 2048 KB Output is correct
27 Correct 33 ms 2048 KB Output is correct
28 Correct 32 ms 2040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 3824 KB Output is correct
2 Correct 157 ms 7792 KB Output is correct
3 Correct 305 ms 12100 KB Output is correct
4 Correct 297 ms 12072 KB Output is correct
5 Correct 259 ms 12016 KB Output is correct
6 Correct 300 ms 11756 KB Output is correct
7 Correct 298 ms 11756 KB Output is correct
8 Correct 296 ms 11756 KB Output is correct
9 Correct 271 ms 11756 KB Output is correct
10 Correct 205 ms 7792 KB Output is correct
11 Correct 199 ms 7792 KB Output is correct
12 Correct 224 ms 7704 KB Output is correct
13 Correct 184 ms 7788 KB Output is correct
14 Correct 210 ms 7840 KB Output is correct
15 Correct 209 ms 7764 KB Output is correct
16 Correct 199 ms 7788 KB Output is correct
17 Correct 203 ms 7792 KB Output is correct
18 Correct 216 ms 7788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 768 KB Output is correct
3 Correct 9 ms 1024 KB Output is correct
4 Correct 9 ms 1024 KB Output is correct
5 Correct 9 ms 1024 KB Output is correct
6 Correct 8 ms 1024 KB Output is correct
7 Correct 9 ms 1024 KB Output is correct
8 Correct 8 ms 1024 KB Output is correct
9 Correct 8 ms 1024 KB Output is correct
10 Correct 8 ms 896 KB Output is correct
11 Correct 8 ms 896 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 896 KB Output is correct
14 Correct 8 ms 896 KB Output is correct
15 Correct 9 ms 896 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 9 ms 896 KB Output is correct
18 Correct 9 ms 896 KB Output is correct
19 Correct 34 ms 2176 KB Output is correct
20 Correct 38 ms 2424 KB Output is correct
21 Correct 47 ms 2424 KB Output is correct
22 Correct 37 ms 2424 KB Output is correct
23 Correct 42 ms 2208 KB Output is correct
24 Correct 34 ms 2176 KB Output is correct
25 Correct 36 ms 2048 KB Output is correct
26 Correct 36 ms 2048 KB Output is correct
27 Correct 33 ms 2048 KB Output is correct
28 Correct 32 ms 2040 KB Output is correct
29 Correct 53 ms 3824 KB Output is correct
30 Correct 157 ms 7792 KB Output is correct
31 Correct 305 ms 12100 KB Output is correct
32 Correct 297 ms 12072 KB Output is correct
33 Correct 259 ms 12016 KB Output is correct
34 Correct 300 ms 11756 KB Output is correct
35 Correct 298 ms 11756 KB Output is correct
36 Correct 296 ms 11756 KB Output is correct
37 Correct 271 ms 11756 KB Output is correct
38 Correct 205 ms 7792 KB Output is correct
39 Correct 199 ms 7792 KB Output is correct
40 Correct 224 ms 7704 KB Output is correct
41 Correct 184 ms 7788 KB Output is correct
42 Correct 210 ms 7840 KB Output is correct
43 Correct 209 ms 7764 KB Output is correct
44 Correct 199 ms 7788 KB Output is correct
45 Correct 203 ms 7792 KB Output is correct
46 Correct 216 ms 7788 KB Output is correct
47 Correct 1134 ms 34672 KB Output is correct
48 Correct 4144 ms 105640 KB Output is correct
49 Correct 4628 ms 115668 KB Output is correct
50 Correct 4732 ms 115664 KB Output is correct
51 Correct 4657 ms 115408 KB Output is correct
52 Correct 4551 ms 115540 KB Output is correct
53 Correct 4611 ms 115508 KB Output is correct
54 Correct 4051 ms 115492 KB Output is correct
55 Correct 4963 ms 128540 KB Output is correct
56 Correct 4148 ms 128488 KB Output is correct
57 Correct 4512 ms 128628 KB Output is correct
58 Correct 4142 ms 128596 KB Output is correct
59 Correct 3712 ms 115504 KB Output is correct
60 Correct 3780 ms 115540 KB Output is correct
61 Correct 3833 ms 115520 KB Output is correct
62 Correct 3741 ms 109568 KB Output is correct
63 Correct 3868 ms 109632 KB Output is correct
64 Correct 3825 ms 109508 KB Output is correct
65 Correct 3337 ms 103672 KB Output is correct
66 Correct 3348 ms 103696 KB Output is correct
67 Correct 3217 ms 103636 KB Output is correct