Submission #1091331

# Submission time Handle Problem Language Result Execution time Memory
1091331 2024-09-20T14:48:12 Z steveonalex Bubble Sort 2 (JOI18_bubblesort2) C++17
60 / 100
9000 ms 30136 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int BLOCK = 2000;

int di(int x, int y){
    return x / y + (x % y > 0);
}

struct SegmentTree{
    int n;
    vector<int> a, lazy;

    SegmentTree(int _n){
        n = _n;
        a.resize(n * 4 + 4); lazy.resize(n * 4 + 4);
    }

    void update_node(int i, int v){
        a[i] += v;
        lazy[i] += v;
    }

    void down(int id){
        update_node(id * 2, lazy[id]); update_node(id * 2 + 1, lazy[id]);
        lazy[id] = 0;
    }

    void update(int u, int v, int val){if (u <= v) update(u, v, val, 0, n-1, 1);}
    void update(int u, int v, int val, int l, int r, int id){
        if (u <= l && r <= v){
            update_node(id, val);
            return;
        }
        if (lazy[id]) down(id);
        int mid = (l + r) >> 1;
        if (u <= mid) update(u, v, val, l, mid, id * 2);
        if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
        a[id] = max(a[id * 2], a[id * 2 + 1]);
    }

    int get(){return a[1];}

    void build_tree(vector<int> b){build_tree(b, 0, n-1, 1);}
    void build_tree(vector<int> &b, int l, int r, int id){
        if (l == r){a[id] = b[l]; return;}
        lazy[id] = 0;
        int mid = (l + r) >> 1;
        build_tree(b, l, mid, id * 2); build_tree(b, mid + 1, r, id * 2 + 1);
        a[id] = max(a[id * 2], a[id * 2 + 1]);
    }

    vector<int> get_tree(){
        vector<int> ans(n);
        get_tree(ans, 0, n-1, 1);
        return ans;
    }
    void get_tree(vector<int> &ans, int l, int r, int id){
        if (l == r){ans[l] = a[id]; return;}
        if (lazy[id]) down(id);
        int mid = (l + r) >> 1;
        get_tree(ans, l, mid, id * 2); get_tree(ans, mid + 1, r, id * 2 + 1);
    }

};

vector<int> countScans(vector<int> a,vector<int> X,vector<int> V){
    int n = a.size(), q = X.size();

    int b_n = di(n, BLOCK);
    vector<vector<pair<int, int>>> stair(b_n);
    vector<SegmentTree> sigma;
    for(int i = 0; i < n; ++i){
        stair[i / BLOCK].push_back({a[i],i});
    }
    for(int i = 0; i < b_n; ++i) sort(ALL(stair[i]));

    for(int i = 0; i < b_n; ++i){
        sigma.push_back(SegmentTree(stair[i].size()));
        for(int k = 0; k < stair[i].size(); ++k){
            int cur = 0;
            for(int j = 0; j < i; ++j){
                int idx = upper_bound(ALL(stair[j]), stair[i][k]) - stair[j].begin();
                cur += stair[j].size() - idx;
            }
            for(int j = k + 1; j < stair[i].size(); ++j) if (stair[i][j].second < stair[i][k].second)
                cur++;
            sigma[i].update(k, k, cur);
        }
    }


    vector<int> answer(q);

    for(int cur_query = 0; cur_query < q; ++cur_query){

        // cout << "Query: " << cur_query << endl;
        int u = X[cur_query], v = V[cur_query];

        int cur_b = u / BLOCK;
        for(int i = cur_b + 1; i < b_n; ++i){
            int idx1 = lower_bound(ALL(stair[i]), make_pair(a[u], -1)) - stair[i].begin() - 1;
            int idx2 = lower_bound(ALL(stair[i]), make_pair(v, -1)) - stair[i].begin() - 1;
            if (idx1 != idx2){
                if (idx1 < idx2) sigma[i].update(idx1 + 1, idx2, 1);
                else sigma[i].update(idx2 + 1, idx1, -1);
            }
        }
        int idx = lower_bound(ALL(stair[cur_b]), make_pair(a[u], u)) - stair[cur_b].begin();
        vector<int> cur_sigma = sigma[cur_b].get_tree();


        a[u] = v;
        stair[cur_b][idx] = make_pair(a[u], u);


        while (idx + 1 < stair[cur_b].size() && stair[cur_b][idx] > stair[cur_b][idx + 1]){
            if (stair[cur_b][idx].second < stair[cur_b][idx + 1].second) cur_sigma[idx + 1]++;
            swap(stair[cur_b][idx], stair[cur_b][idx + 1]);
            swap(cur_sigma[idx], cur_sigma[idx + 1]);
            idx++;
        }
        while (idx > 0 && stair[cur_b][idx - 1] > stair[cur_b][idx]){
            if (stair[cur_b][idx].second < stair[cur_b][idx - 1].second) cur_sigma[idx - 1]--;
            swap(stair[cur_b][idx], stair[cur_b][idx - 1]);
            swap(cur_sigma[idx], cur_sigma[idx - 1]);
            idx--;
        }
        cur_sigma[idx] = 0;
        for(int j = 0; j < cur_b; ++j){
            int _idx = upper_bound(ALL(stair[j]), stair[cur_b][idx]) - stair[j].begin();
            cur_sigma[idx] += stair[j].size() - _idx;
        }
        for(int j = idx + 1; j < stair[cur_b].size(); ++j) if (stair[cur_b][j].second < stair[cur_b][idx].second)
            cur_sigma[idx]++;
        sigma[cur_b].build_tree(cur_sigma);

        int ans = 0;
        for(int i = 0; i < b_n; ++i) maximize(ans, sigma[i].get());
        answer[cur_query] = ans;

        // for(int i = 0; i<b_n; ++i){
        //     vector<int> sig = sigma[i].get_tree();
        //     printArr(sig, " ", "");
        // }
        // cout << "\n";
    }
    return answer;
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     int n, q; cin >> n >> q;
//     vector<int> a(n);
//     for(int i = 0; i<n; ++i) cin >> a[i];

//     vector<int> x(q), v(q);
//     for(int i = 0; i<q; ++i) cin >> x[i] >> v[i];

//     printArr(countScans(a, x, v));

//     return 0;
// }

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:132:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |         for(int k = 0; k < stair[i].size(); ++k){
      |                        ~~^~~~~~~~~~~~~~~~~
bubblesort2.cpp:138:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |             for(int j = k + 1; j < stair[i].size(); ++j) if (stair[i][j].second < stair[i][k].second)
      |                                ~~^~~~~~~~~~~~~~~~~
bubblesort2.cpp:169:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |         while (idx + 1 < stair[cur_b].size() && stair[cur_b][idx] > stair[cur_b][idx + 1]){
      |                ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
bubblesort2.cpp:186:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  186 |         for(int j = idx + 1; j < stair[cur_b].size(); ++j) if (stair[cur_b][j].second < stair[cur_b][idx].second)
      |                              ~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 348 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 39 ms 348 KB Output is correct
4 Correct 34 ms 344 KB Output is correct
5 Correct 31 ms 348 KB Output is correct
6 Correct 27 ms 588 KB Output is correct
7 Correct 28 ms 348 KB Output is correct
8 Correct 36 ms 348 KB Output is correct
9 Correct 31 ms 348 KB Output is correct
10 Correct 29 ms 344 KB Output is correct
11 Correct 29 ms 348 KB Output is correct
12 Correct 33 ms 348 KB Output is correct
13 Correct 29 ms 344 KB Output is correct
14 Correct 30 ms 348 KB Output is correct
15 Correct 29 ms 348 KB Output is correct
16 Correct 29 ms 344 KB Output is correct
17 Correct 38 ms 348 KB Output is correct
18 Correct 29 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 348 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 39 ms 348 KB Output is correct
4 Correct 34 ms 344 KB Output is correct
5 Correct 31 ms 348 KB Output is correct
6 Correct 27 ms 588 KB Output is correct
7 Correct 28 ms 348 KB Output is correct
8 Correct 36 ms 348 KB Output is correct
9 Correct 31 ms 348 KB Output is correct
10 Correct 29 ms 344 KB Output is correct
11 Correct 29 ms 348 KB Output is correct
12 Correct 33 ms 348 KB Output is correct
13 Correct 29 ms 344 KB Output is correct
14 Correct 30 ms 348 KB Output is correct
15 Correct 29 ms 348 KB Output is correct
16 Correct 29 ms 344 KB Output is correct
17 Correct 38 ms 348 KB Output is correct
18 Correct 29 ms 348 KB Output is correct
19 Correct 121 ms 856 KB Output is correct
20 Correct 146 ms 1112 KB Output is correct
21 Correct 121 ms 988 KB Output is correct
22 Correct 145 ms 856 KB Output is correct
23 Correct 127 ms 856 KB Output is correct
24 Correct 133 ms 968 KB Output is correct
25 Correct 121 ms 968 KB Output is correct
26 Correct 121 ms 968 KB Output is correct
27 Correct 116 ms 964 KB Output is correct
28 Correct 114 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1616 KB Output is correct
2 Correct 544 ms 2748 KB Output is correct
3 Correct 1153 ms 3788 KB Output is correct
4 Correct 1167 ms 3788 KB Output is correct
5 Correct 1109 ms 3792 KB Output is correct
6 Correct 1146 ms 3664 KB Output is correct
7 Correct 1075 ms 3668 KB Output is correct
8 Correct 1036 ms 3660 KB Output is correct
9 Correct 1041 ms 3796 KB Output is correct
10 Correct 701 ms 3600 KB Output is correct
11 Correct 709 ms 3792 KB Output is correct
12 Correct 752 ms 3708 KB Output is correct
13 Correct 697 ms 3792 KB Output is correct
14 Correct 720 ms 3784 KB Output is correct
15 Correct 734 ms 3672 KB Output is correct
16 Correct 786 ms 3796 KB Output is correct
17 Correct 784 ms 3672 KB Output is correct
18 Correct 710 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 348 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 39 ms 348 KB Output is correct
4 Correct 34 ms 344 KB Output is correct
5 Correct 31 ms 348 KB Output is correct
6 Correct 27 ms 588 KB Output is correct
7 Correct 28 ms 348 KB Output is correct
8 Correct 36 ms 348 KB Output is correct
9 Correct 31 ms 348 KB Output is correct
10 Correct 29 ms 344 KB Output is correct
11 Correct 29 ms 348 KB Output is correct
12 Correct 33 ms 348 KB Output is correct
13 Correct 29 ms 344 KB Output is correct
14 Correct 30 ms 348 KB Output is correct
15 Correct 29 ms 348 KB Output is correct
16 Correct 29 ms 344 KB Output is correct
17 Correct 38 ms 348 KB Output is correct
18 Correct 29 ms 348 KB Output is correct
19 Correct 121 ms 856 KB Output is correct
20 Correct 146 ms 1112 KB Output is correct
21 Correct 121 ms 988 KB Output is correct
22 Correct 145 ms 856 KB Output is correct
23 Correct 127 ms 856 KB Output is correct
24 Correct 133 ms 968 KB Output is correct
25 Correct 121 ms 968 KB Output is correct
26 Correct 121 ms 968 KB Output is correct
27 Correct 116 ms 964 KB Output is correct
28 Correct 114 ms 972 KB Output is correct
29 Correct 68 ms 1616 KB Output is correct
30 Correct 544 ms 2748 KB Output is correct
31 Correct 1153 ms 3788 KB Output is correct
32 Correct 1167 ms 3788 KB Output is correct
33 Correct 1109 ms 3792 KB Output is correct
34 Correct 1146 ms 3664 KB Output is correct
35 Correct 1075 ms 3668 KB Output is correct
36 Correct 1036 ms 3660 KB Output is correct
37 Correct 1041 ms 3796 KB Output is correct
38 Correct 701 ms 3600 KB Output is correct
39 Correct 709 ms 3792 KB Output is correct
40 Correct 752 ms 3708 KB Output is correct
41 Correct 697 ms 3792 KB Output is correct
42 Correct 720 ms 3784 KB Output is correct
43 Correct 734 ms 3672 KB Output is correct
44 Correct 786 ms 3796 KB Output is correct
45 Correct 784 ms 3672 KB Output is correct
46 Correct 710 ms 3668 KB Output is correct
47 Correct 6002 ms 10672 KB Output is correct
48 Execution timed out 9011 ms 30136 KB Time limit exceeded
49 Halted 0 ms 0 KB -