Submission #705249

# Submission time Handle Problem Language Result Execution time Memory
705249 2023-03-03T17:05:15 Z bebra Bubble Sort 2 (JOI18_bubblesort2) C++17
38 / 100
9000 ms 1620 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;


struct FenwickTree {
    vector<int> tree;
    int size;

    FenwickTree(int n) {
        size = n;
        tree.resize(size);
    }

    void update(int pos, int value) {
        for (int i = pos; i < size; i |= i + 1) {
            tree[i] += value;
        }
    }

    int query(int l, int r) {
        int res = 0;
        for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
            res += tree[i];
        }
        for (int i = l - 1; i >= 0; i = (i & (i + 1)) - 1) {
            res -= tree[i];
        }
        return res;
    }
};




vector<int> countScans(vector<int> a, vector<int> pos, vector<int> value){
	int q = pos.size();

    vector<int> all_values = a;
    all_values.insert(all_values.end(), value.begin(), value.end());


    sort(all_values.begin(), all_values.end());
    all_values.resize(unique(all_values.begin(), all_values.end()) - all_values.begin());

    int k = all_values.size();
    map<int, int> mp;
    for (int i = 0; i < k; ++i) {
        mp[all_values[i]] = i;
    }

    for (auto& x : a) {
        x = mp[x];
    }
    for (auto& x : value) {
        x = mp[x];
    }

    auto count = [&]() -> int {
        // the answer is maximum number of inversions for a single number
        FenwickTree tree(k);
        // maybe will be better to reverse the array for simplicity
        int res = 0;
        for (auto x : a) {
            res = max(res, tree.query(x + 1, k - 1));
            tree.update(x, 1);
        }
        return res;
    };

    vector<int> ans(q);
    for (int i = 0; i < q; ++i) {
        a[pos[i]] = value[i];
        ans[i] = count();
    }

	return ans;
}



// int main() {
//     ios_base::sync_with_stdio(false);
//     cin.tie(nullptr);

//     int n, q;
//     cin >> n >> q;
//     vector<int> a(n);
//     for (auto& x : a) {
//         cin >> x;
//     }
//     vector<int> pos(q), value(q);
//     for (int i = 0; i < q; ++i) {
//         cin >> pos[i] >> value[i];
//     }
//     for (auto x : countScans(a, pos, value)) {
//         cout << x << '\n';
//     }
//     return 0;
// }


/*
4 2
1 2 3 4
0 3
2 1
*/
# Verdict Execution time Memory Grader output
1 Correct 8 ms 384 KB Output is correct
2 Correct 20 ms 340 KB Output is correct
3 Correct 108 ms 608 KB Output is correct
4 Correct 108 ms 596 KB Output is correct
5 Correct 113 ms 596 KB Output is correct
6 Correct 118 ms 596 KB Output is correct
7 Correct 114 ms 596 KB Output is correct
8 Correct 133 ms 596 KB Output is correct
9 Correct 108 ms 600 KB Output is correct
10 Correct 111 ms 568 KB Output is correct
11 Correct 109 ms 560 KB Output is correct
12 Correct 123 ms 576 KB Output is correct
13 Correct 143 ms 552 KB Output is correct
14 Correct 97 ms 552 KB Output is correct
15 Correct 103 ms 572 KB Output is correct
16 Correct 102 ms 592 KB Output is correct
17 Correct 97 ms 468 KB Output is correct
18 Correct 106 ms 540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 384 KB Output is correct
2 Correct 20 ms 340 KB Output is correct
3 Correct 108 ms 608 KB Output is correct
4 Correct 108 ms 596 KB Output is correct
5 Correct 113 ms 596 KB Output is correct
6 Correct 118 ms 596 KB Output is correct
7 Correct 114 ms 596 KB Output is correct
8 Correct 133 ms 596 KB Output is correct
9 Correct 108 ms 600 KB Output is correct
10 Correct 111 ms 568 KB Output is correct
11 Correct 109 ms 560 KB Output is correct
12 Correct 123 ms 576 KB Output is correct
13 Correct 143 ms 552 KB Output is correct
14 Correct 97 ms 552 KB Output is correct
15 Correct 103 ms 572 KB Output is correct
16 Correct 102 ms 592 KB Output is correct
17 Correct 97 ms 468 KB Output is correct
18 Correct 106 ms 540 KB Output is correct
19 Correct 2103 ms 1404 KB Output is correct
20 Correct 2017 ms 1560 KB Output is correct
21 Correct 2174 ms 1564 KB Output is correct
22 Correct 2000 ms 1564 KB Output is correct
23 Correct 1898 ms 1436 KB Output is correct
24 Correct 1964 ms 1440 KB Output is correct
25 Correct 1733 ms 1376 KB Output is correct
26 Correct 1927 ms 1380 KB Output is correct
27 Correct 1761 ms 1356 KB Output is correct
28 Correct 1798 ms 1328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1126 ms 852 KB Output is correct
2 Execution timed out 9064 ms 1620 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 384 KB Output is correct
2 Correct 20 ms 340 KB Output is correct
3 Correct 108 ms 608 KB Output is correct
4 Correct 108 ms 596 KB Output is correct
5 Correct 113 ms 596 KB Output is correct
6 Correct 118 ms 596 KB Output is correct
7 Correct 114 ms 596 KB Output is correct
8 Correct 133 ms 596 KB Output is correct
9 Correct 108 ms 600 KB Output is correct
10 Correct 111 ms 568 KB Output is correct
11 Correct 109 ms 560 KB Output is correct
12 Correct 123 ms 576 KB Output is correct
13 Correct 143 ms 552 KB Output is correct
14 Correct 97 ms 552 KB Output is correct
15 Correct 103 ms 572 KB Output is correct
16 Correct 102 ms 592 KB Output is correct
17 Correct 97 ms 468 KB Output is correct
18 Correct 106 ms 540 KB Output is correct
19 Correct 2103 ms 1404 KB Output is correct
20 Correct 2017 ms 1560 KB Output is correct
21 Correct 2174 ms 1564 KB Output is correct
22 Correct 2000 ms 1564 KB Output is correct
23 Correct 1898 ms 1436 KB Output is correct
24 Correct 1964 ms 1440 KB Output is correct
25 Correct 1733 ms 1376 KB Output is correct
26 Correct 1927 ms 1380 KB Output is correct
27 Correct 1761 ms 1356 KB Output is correct
28 Correct 1798 ms 1328 KB Output is correct
29 Correct 1126 ms 852 KB Output is correct
30 Execution timed out 9064 ms 1620 KB Time limit exceeded
31 Halted 0 ms 0 KB -