Submission #256155

# Submission time Handle Problem Language Result Execution time Memory
256155 2020-08-02T10:35:03 Z IgorI Bubble Sort 2 (JOI18_bubblesort2) C++17
60 / 100
9000 ms 14968 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 INF = 1e9 + 7;
const int K = 400;

struct Element{
    int x;
    int pos_order;
    int sp;
};

struct SegTree{
    vector<int> tree;
    vector<int> push;
    SegTree()
    {
        tree.resize(4 * K);
        push.resize(4 * K);
    }
    void Push(int V)
    {
        tree[2 * V + 1] += push[V];
        push[2 * V + 1] += push[V];
        tree[2 * V + 2] += push[V];
        push[2 * V + 2] += push[V];
        push[V] = 0;
    }
    void __Add(int l, int r, int x, int L, int R, int V)
    {
        if (l <= L && R <= r)
        {
            tree[V] += x;
            push[V] += x;
            return;
        }
        if (r <= L || R <= l)
        {
            return;
        }
        int M = (L + R) / 2;
        Push(V);
        __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 Add(int l, int r, int x)
    {
        __Add(l, r, x, 0, K, 0);
    }
    int __Get(int l, int r, int L, int R, int V)
    {
        if (l <= L && R <= r)
        {
            return tree[V];
        }
        if (r <= L || R <= l)
        {
            return 0;
        }
        int M = (L + R) / 2;
        Push(V);
        return max(__Get(l, r, L, M, 2 * V + 1), __Get(l, r, M, R, 2 * V + 2));
    }
    int Get(int l, int r)
    {
        return __Get(l, r, 0, K, 0);
    }
    void build(int L, int R, int V, vector<Element> &val)
    {
        if (L + 1 == R)
        {
            tree[V] = val[L].sp;
            push[V] = 0;
            return;
        }
        int M = (L + R) / 2;
        build(L, M, 2 * V + 1, val);
        build(M, R, 2 * V + 2, val);
        push[V] = 0;
        tree[V] = max(tree[2 * V + 1], tree[2 * V + 2]);
    }
};

bool order(Element a, Element b)
{
    return a.pos_order < b.pos_order;
}

bool sorted(Element a, Element b)
{
    return a.x < b.x;
}

struct Block{
    SegTree d;
    vector<Element> a;
    Block()
    {
        a.resize(K);
        for (int i = 0; i < K; i++)
        {
            a[i].x = INF;
            a[i].pos_order = i;
        }
    }
    int cntmore(int x)
    {
        int L = -1, R = a.size();
        while (L + 1 < R)
        {
            int M = (L + R) / 2;
            if (a[M].x > x)
                R = M;
            else
                L = M;
        }
        return K - R;
    }
    void change(int pos_in_block, int initial, int A)
    {
        for (int i = 0; i < K; i++)
        {
            a[i].sp = d.Get(i, i + 1);
        }
        sort(all(a), order);
        int Z = a[pos_in_block].x;
        a[pos_in_block].x = A;
        for (int i = 0; i < pos_in_block; i++)
        {
            if (a[i].x > a[pos_in_block].x) initial++;
        }
        a[pos_in_block].sp = initial;
        for (int i = pos_in_block + 1; i < K; i++)
        {
            a[i].sp -= Z > a[i].x;
            a[i].sp += A > a[i].x;
        }
        sort(all(a), sorted);
        d.build(0, K, 0, a);
    }
    void query(int x, int val)
    {
        int L = -1, R = K;
        while (L + 1 < R)
        {
            int M = (L + R) / 2;
            if (a[M].x < x)
                L = M;
            else
                R = M;
        }
        d.Add(0, L + 1, val);
    }
};

vi countScans(vi a, vi x, vi v)
{
    int n = a.size(), q = x.size();
    vi ans(q);
    vector<Block> data(n / K + 1);
    for (int i = 0; i < n; i++)
    {
        int bl = i / K;
        int ps = i % K;
        int initial = 0;
        for (int j = 0; j < bl; j++) initial += data[j].cntmore(a[i]);
        data[bl].change(ps, initial, a[i]);
    }
    for (int i = 0; i < q; i++)
    {
        int bl = x[i] / K;
        int ps = x[i] % K;
        int initial = 0;
        for (int j = 0; j < bl; j++) initial += data[j].cntmore(v[i]);
        data[bl].change(ps, initial, v[i]);
        int fr = a[x[i]];
        int to = v[i];
        a[x[i]] = v[i];
        for (int j = bl + 1; j < n / K + 1; j++) data[j].query(fr, -1);
        for (int j = bl + 1; j < n / K + 1; j++) data[j].query(to, 1);
        int answer = 0;
        for (int j = 0; j < n / K + 1; j++) answer = max(answer, data[j].d.Get(0, K));
        ans[i] = answer;
        #ifdef LOCAL
        cout << "after i = " << i << endl;
        for (int j = 0; j < n / K + 1; j++)
        {
            cout << j << ": " << endl;
            for (int i = 0; i < K; i++) cout << data[j].a[i].x << " ";
            cout << endl;
            for (int i = 0; i < K; i++) cout << data[j].d.Get(i, i + 1) << " ";
            cout << endl;
        }
        #endif // LOCAL
    }
    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

/*
10 10
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
*/

Compilation message

bubblesort2.cpp: In member function 'int SegTree::Add(int, int, int)':
bubblesort2.cpp:77:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
# Verdict Execution time Memory Grader output
1 Correct 74 ms 384 KB Output is correct
2 Correct 118 ms 420 KB Output is correct
3 Correct 294 ms 632 KB Output is correct
4 Correct 278 ms 632 KB Output is correct
5 Correct 285 ms 512 KB Output is correct
6 Correct 248 ms 524 KB Output is correct
7 Correct 258 ms 524 KB Output is correct
8 Correct 273 ms 512 KB Output is correct
9 Correct 279 ms 632 KB Output is correct
10 Correct 247 ms 632 KB Output is correct
11 Correct 277 ms 632 KB Output is correct
12 Correct 249 ms 632 KB Output is correct
13 Correct 246 ms 520 KB Output is correct
14 Correct 239 ms 512 KB Output is correct
15 Correct 270 ms 520 KB Output is correct
16 Correct 238 ms 520 KB Output is correct
17 Correct 231 ms 512 KB Output is correct
18 Correct 231 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 384 KB Output is correct
2 Correct 118 ms 420 KB Output is correct
3 Correct 294 ms 632 KB Output is correct
4 Correct 278 ms 632 KB Output is correct
5 Correct 285 ms 512 KB Output is correct
6 Correct 248 ms 524 KB Output is correct
7 Correct 258 ms 524 KB Output is correct
8 Correct 273 ms 512 KB Output is correct
9 Correct 279 ms 632 KB Output is correct
10 Correct 247 ms 632 KB Output is correct
11 Correct 277 ms 632 KB Output is correct
12 Correct 249 ms 632 KB Output is correct
13 Correct 246 ms 520 KB Output is correct
14 Correct 239 ms 512 KB Output is correct
15 Correct 270 ms 520 KB Output is correct
16 Correct 238 ms 520 KB Output is correct
17 Correct 231 ms 512 KB Output is correct
18 Correct 231 ms 512 KB Output is correct
19 Correct 1012 ms 868 KB Output is correct
20 Correct 1139 ms 1016 KB Output is correct
21 Correct 1062 ms 1016 KB Output is correct
22 Correct 1134 ms 1016 KB Output is correct
23 Correct 1004 ms 1016 KB Output is correct
24 Correct 994 ms 1016 KB Output is correct
25 Correct 986 ms 1016 KB Output is correct
26 Correct 931 ms 1016 KB Output is correct
27 Correct 904 ms 1016 KB Output is correct
28 Correct 905 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1932 ms 1912 KB Output is correct
2 Correct 5030 ms 2896 KB Output is correct
3 Correct 8384 ms 3960 KB Output is correct
4 Correct 8392 ms 4472 KB Output is correct
5 Correct 8106 ms 4472 KB Output is correct
6 Correct 8039 ms 4472 KB Output is correct
7 Correct 7943 ms 4504 KB Output is correct
8 Correct 7776 ms 4472 KB Output is correct
9 Correct 7656 ms 4472 KB Output is correct
10 Correct 5294 ms 4584 KB Output is correct
11 Correct 5310 ms 4588 KB Output is correct
12 Correct 5273 ms 4580 KB Output is correct
13 Correct 5289 ms 4480 KB Output is correct
14 Correct 5376 ms 4576 KB Output is correct
15 Correct 5530 ms 4572 KB Output is correct
16 Correct 5504 ms 4568 KB Output is correct
17 Correct 5509 ms 4568 KB Output is correct
18 Correct 5686 ms 4564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 384 KB Output is correct
2 Correct 118 ms 420 KB Output is correct
3 Correct 294 ms 632 KB Output is correct
4 Correct 278 ms 632 KB Output is correct
5 Correct 285 ms 512 KB Output is correct
6 Correct 248 ms 524 KB Output is correct
7 Correct 258 ms 524 KB Output is correct
8 Correct 273 ms 512 KB Output is correct
9 Correct 279 ms 632 KB Output is correct
10 Correct 247 ms 632 KB Output is correct
11 Correct 277 ms 632 KB Output is correct
12 Correct 249 ms 632 KB Output is correct
13 Correct 246 ms 520 KB Output is correct
14 Correct 239 ms 512 KB Output is correct
15 Correct 270 ms 520 KB Output is correct
16 Correct 238 ms 520 KB Output is correct
17 Correct 231 ms 512 KB Output is correct
18 Correct 231 ms 512 KB Output is correct
19 Correct 1012 ms 868 KB Output is correct
20 Correct 1139 ms 1016 KB Output is correct
21 Correct 1062 ms 1016 KB Output is correct
22 Correct 1134 ms 1016 KB Output is correct
23 Correct 1004 ms 1016 KB Output is correct
24 Correct 994 ms 1016 KB Output is correct
25 Correct 986 ms 1016 KB Output is correct
26 Correct 931 ms 1016 KB Output is correct
27 Correct 904 ms 1016 KB Output is correct
28 Correct 905 ms 1016 KB Output is correct
29 Correct 1932 ms 1912 KB Output is correct
30 Correct 5030 ms 2896 KB Output is correct
31 Correct 8384 ms 3960 KB Output is correct
32 Correct 8392 ms 4472 KB Output is correct
33 Correct 8106 ms 4472 KB Output is correct
34 Correct 8039 ms 4472 KB Output is correct
35 Correct 7943 ms 4504 KB Output is correct
36 Correct 7776 ms 4472 KB Output is correct
37 Correct 7656 ms 4472 KB Output is correct
38 Correct 5294 ms 4584 KB Output is correct
39 Correct 5310 ms 4588 KB Output is correct
40 Correct 5273 ms 4580 KB Output is correct
41 Correct 5289 ms 4480 KB Output is correct
42 Correct 5376 ms 4576 KB Output is correct
43 Correct 5530 ms 4572 KB Output is correct
44 Correct 5504 ms 4568 KB Output is correct
45 Correct 5509 ms 4568 KB Output is correct
46 Correct 5686 ms 4564 KB Output is correct
47 Execution timed out 9082 ms 14968 KB Time limit exceeded
48 Halted 0 ms 0 KB -