Submission #256509

# Submission time Handle Problem Language Result Execution time Memory
256509 2020-08-02T19:05:49 Z IgorI Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
4972 ms 140848 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 = 1100002;

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 1152 KB Output is correct
4 Correct 15 ms 1152 KB Output is correct
5 Correct 11 ms 1152 KB Output is correct
6 Correct 8 ms 1152 KB Output is correct
7 Correct 10 ms 1152 KB Output is correct
8 Correct 12 ms 1152 KB Output is correct
9 Correct 9 ms 1152 KB Output is correct
10 Correct 8 ms 1024 KB Output is correct
11 Correct 8 ms 1024 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 1024 KB Output is correct
14 Correct 8 ms 1024 KB Output is correct
15 Correct 9 ms 1024 KB Output is correct
16 Correct 8 ms 1004 KB Output is correct
17 Correct 8 ms 1024 KB Output is correct
18 Correct 9 ms 1024 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 1152 KB Output is correct
4 Correct 15 ms 1152 KB Output is correct
5 Correct 11 ms 1152 KB Output is correct
6 Correct 8 ms 1152 KB Output is correct
7 Correct 10 ms 1152 KB Output is correct
8 Correct 12 ms 1152 KB Output is correct
9 Correct 9 ms 1152 KB Output is correct
10 Correct 8 ms 1024 KB Output is correct
11 Correct 8 ms 1024 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 1024 KB Output is correct
14 Correct 8 ms 1024 KB Output is correct
15 Correct 9 ms 1024 KB Output is correct
16 Correct 8 ms 1004 KB Output is correct
17 Correct 8 ms 1024 KB Output is correct
18 Correct 9 ms 1024 KB Output is correct
19 Correct 38 ms 2536 KB Output is correct
20 Correct 35 ms 2820 KB Output is correct
21 Correct 35 ms 2816 KB Output is correct
22 Correct 35 ms 2816 KB Output is correct
23 Correct 38 ms 2552 KB Output is correct
24 Correct 31 ms 2560 KB Output is correct
25 Correct 32 ms 2432 KB Output is correct
26 Correct 32 ms 2432 KB Output is correct
27 Correct 33 ms 2304 KB Output is correct
28 Correct 32 ms 2304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 4596 KB Output is correct
2 Correct 148 ms 9460 KB Output is correct
3 Correct 275 ms 14444 KB Output is correct
4 Correct 278 ms 14576 KB Output is correct
5 Correct 272 ms 14316 KB Output is correct
6 Correct 297 ms 14188 KB Output is correct
7 Correct 265 ms 14188 KB Output is correct
8 Correct 270 ms 14188 KB Output is correct
9 Correct 275 ms 14188 KB Output is correct
10 Correct 202 ms 9068 KB Output is correct
11 Correct 203 ms 9196 KB Output is correct
12 Correct 210 ms 9068 KB Output is correct
13 Correct 199 ms 9196 KB Output is correct
14 Correct 188 ms 9196 KB Output is correct
15 Correct 193 ms 9200 KB Output is correct
16 Correct 202 ms 9160 KB Output is correct
17 Correct 195 ms 9196 KB Output is correct
18 Correct 184 ms 9196 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 1152 KB Output is correct
4 Correct 15 ms 1152 KB Output is correct
5 Correct 11 ms 1152 KB Output is correct
6 Correct 8 ms 1152 KB Output is correct
7 Correct 10 ms 1152 KB Output is correct
8 Correct 12 ms 1152 KB Output is correct
9 Correct 9 ms 1152 KB Output is correct
10 Correct 8 ms 1024 KB Output is correct
11 Correct 8 ms 1024 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 1024 KB Output is correct
14 Correct 8 ms 1024 KB Output is correct
15 Correct 9 ms 1024 KB Output is correct
16 Correct 8 ms 1004 KB Output is correct
17 Correct 8 ms 1024 KB Output is correct
18 Correct 9 ms 1024 KB Output is correct
19 Correct 38 ms 2536 KB Output is correct
20 Correct 35 ms 2820 KB Output is correct
21 Correct 35 ms 2816 KB Output is correct
22 Correct 35 ms 2816 KB Output is correct
23 Correct 38 ms 2552 KB Output is correct
24 Correct 31 ms 2560 KB Output is correct
25 Correct 32 ms 2432 KB Output is correct
26 Correct 32 ms 2432 KB Output is correct
27 Correct 33 ms 2304 KB Output is correct
28 Correct 32 ms 2304 KB Output is correct
29 Correct 59 ms 4596 KB Output is correct
30 Correct 148 ms 9460 KB Output is correct
31 Correct 275 ms 14444 KB Output is correct
32 Correct 278 ms 14576 KB Output is correct
33 Correct 272 ms 14316 KB Output is correct
34 Correct 297 ms 14188 KB Output is correct
35 Correct 265 ms 14188 KB Output is correct
36 Correct 270 ms 14188 KB Output is correct
37 Correct 275 ms 14188 KB Output is correct
38 Correct 202 ms 9068 KB Output is correct
39 Correct 203 ms 9196 KB Output is correct
40 Correct 210 ms 9068 KB Output is correct
41 Correct 199 ms 9196 KB Output is correct
42 Correct 188 ms 9196 KB Output is correct
43 Correct 193 ms 9200 KB Output is correct
44 Correct 202 ms 9160 KB Output is correct
45 Correct 195 ms 9196 KB Output is correct
46 Correct 184 ms 9196 KB Output is correct
47 Correct 1126 ms 42248 KB Output is correct
48 Correct 4275 ms 128680 KB Output is correct
49 Correct 4667 ms 140712 KB Output is correct
50 Correct 4607 ms 140640 KB Output is correct
51 Correct 4972 ms 140628 KB Output is correct
52 Correct 4538 ms 140756 KB Output is correct
53 Correct 4662 ms 140848 KB Output is correct
54 Correct 4059 ms 140664 KB Output is correct
55 Correct 4272 ms 140628 KB Output is correct
56 Correct 4207 ms 140628 KB Output is correct
57 Correct 4704 ms 140708 KB Output is correct
58 Correct 4198 ms 140692 KB Output is correct
59 Correct 3927 ms 125884 KB Output is correct
60 Correct 4131 ms 125908 KB Output is correct
61 Correct 4251 ms 125944 KB Output is correct
62 Correct 4045 ms 118484 KB Output is correct
63 Correct 4046 ms 118744 KB Output is correct
64 Correct 3883 ms 118484 KB Output is correct
65 Correct 3912 ms 111080 KB Output is correct
66 Correct 3838 ms 111048 KB Output is correct
67 Correct 3721 ms 111060 KB Output is correct