Submission #789397

# Submission time Handle Problem Language Result Execution time Memory
789397 2023-07-21T10:25:44 Z anton Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3349 ms 136000 KB
#include<bits/stdc++.h>
#include "bubblesort2.h"

using namespace std;
#define pii pair<int, int>


const int INF = (1<<30)-1;

struct Tree{
    vector<int> tree;
    vector<int> d;
    int m;

    Tree(vector<int> &v){
        int m = 1;
        while(m<v.size()){
            m*=2;
        }
        m*=2;

        tree.resize(m);
        d.resize(m);

        build(v, 0, v.size()-1, 1);
    }

    void build(vector<int> &v, int lt, int rt, int t){
        if(lt == rt){
            tree[t] = v[lt];
            d[t] = 0;
        }
        else{
            int mid = (lt+rt)/2;
            d[t] = 0;

            build(v, lt, mid, t*2);
            build(v, mid+1, rt, t*2+1);
            tree[t] = min(tree[t*2], tree[t*2+1]) +d[t];
        }
    }

    int update(int l, int r, int v, int lt, int rt, int t){
        if(l<=lt && rt<=r){
            d[t] += v;
            tree[t] += v;
            return tree[t];
        }
        else if(rt<l || r<lt){
            return INF;
        }
        else{
            int mid = (lt+rt)/2;
            update(l, r, v, lt, mid, t*2);
            update(l, r, v, mid+1, rt, t*2+1);
            tree[t] = min(tree[t*2], tree[t*2+1]) + d[t];
            return tree[t];
        }
    }

	void set_val(int id, int v, int lt, int rt, int t){
		if(lt == id && rt == id){
			tree[t] = v;
		}
		else{
			int mid =(lt+rt)/2;

			if(id<=mid){
				set_val(id, v-d[t], lt, mid, t*2);
			}
			else{
				set_val(id, v-d[t], mid+1, rt, t*2+1);
			}


			tree[t] = min(tree[t*2], tree[t*2+1])+d[t];
		}
	}
};

struct SumTree{
    vector<int> tree;
    int m;

    SumTree(vector<int> &v){
        int m = 1;
        while(m<v.size()){
            m*=2;
        }
        m*=2;

        tree.resize(m);

        build(v, 0, v.size()-1, 1);
    }

    void build(vector<int> &v, int lt, int rt, int t){
        if(lt == rt){
            tree[t] = v[lt];
        }
        else{
            int mid = (lt+rt)/2;

            build(v, lt, mid, t*2);
            build(v, mid+1, rt, t*2+1);
            tree[t] = tree[t*2] + tree[t*2+1];
        }
		//cout<<"buit "<<lt<<" "<<rt<<" "<<tree[t]<<endl;
    }

    int get_sum(int l, int r, int lt, int rt, int t){

		//cout<<"sum "<<lt<<" "<<rt<<" "<<tree[t]<<endl;
        if(l<=lt && rt<=r){
            return tree[t];
        }
        else if(rt<l || r<lt || l>r){
            return 0;
        }
        else{
            int mid = (lt+rt)/2;
            return get_sum(l, r, lt, mid, t*2) + get_sum(l, r, mid+1, rt, t*2+1);
        }
    }

	void add_val(int id, int v, int lt, int rt, int t){
		if(lt == id && rt == id){
			tree[t] += v;
		}
		else{
			int mid =(lt+rt)/2;

			if(id<=mid){
				add_val(id, v, lt, mid, t*2);
			}
			else{
				add_val(id, v, mid+1, rt, t*2+1);
			}
			tree[t] = tree[t*2] + tree[t*2+1];
		}
	}
};
int n, q;
vector<pii> big;
map<pii, int> small;

vector<int> countScans(vector<int> A,vector<int> X,vector<int> V){
	n = A.size();
    q=X.size();
	
    for(int i = 0; i<n; i++){
        big.push_back(pii(A[i], i));
    }
    for(int i = 0; i<q; i++){
        big.push_back(pii(V[i], X[i]));
    }

    sort(big.begin(), big.end());
    int bs = big.size();

    vector<int> v(bs, INF);
    
    for(int i = 0; i<bs; i++){
        small[big[i]] = i;
    }

    vector<pii> target(n);
    for(int i = 0; i<n; i++){
        target[i] = pii(A[i], i);
    }
    sort(target.begin(), target.end());
	vector<int> oc(bs, 0);
    for(int i = 0; i<n; i++){
        v[small[target[i]]] = i-target[i].second;
		oc[small[target[i]]] =1;
    }

    /*for(int i = 0; i<bs; i++){
        cout<<big[i].first<<" "<<big[i].second<<" "<<v[i]<<" "<<oc[i]<<endl;
    }*/
    Tree tr = Tree(v);
	SumTree s_tr = SumTree(oc);
	s_tr.get_sum(0, bs-1, 0, bs-1, 1);

	vector<int> r;
	for(int i = 0; i<q; i++){
		int cur_pos = small[pii(A[X[i]], X[i])];
		int pos = small[pii(V[i], X[i])];

		//cout<<"from "<<cur_pos<<" to "<<pos<<endl;
		A[X[i]] = V[i];

		if(cur_pos<pos){
			tr.update(cur_pos, pos, -1, 0, bs-1, 1);
		}
		else{
			tr.update(pos, cur_pos, 1, 0, bs-1, 1);
		}
		s_tr.add_val(cur_pos, -1, 0, bs-1, 1);
		s_tr.add_val(pos, 1, 0, bs-1, 1);

		int tar = s_tr.get_sum(0, pos-1, 0, bs-1, 1);
		//cout<<"new tar "<<tar<<endl;
		tr.set_val(cur_pos, INF, 0, bs-1, 1);
		tr.set_val(pos, tar-X[i], 0, bs-1, 1);

		r.push_back(-tr.tree[1]);
	}
	
	return r;
}

Compilation message

bubblesort2.cpp: In constructor 'Tree::Tree(std::vector<int>&)':
bubblesort2.cpp:17:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |         while(m<v.size()){
      |               ~^~~~~~~~~
bubblesort2.cpp: In constructor 'SumTree::SumTree(std::vector<int>&)':
bubblesort2.cpp:87:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         while(m<v.size()){
      |               ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 4 ms 724 KB Output is correct
5 Correct 4 ms 820 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 824 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 4 ms 692 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 4 ms 724 KB Output is correct
5 Correct 4 ms 820 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 824 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 4 ms 692 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
19 Correct 15 ms 2204 KB Output is correct
20 Correct 17 ms 2428 KB Output is correct
21 Correct 17 ms 2388 KB Output is correct
22 Correct 17 ms 2396 KB Output is correct
23 Correct 16 ms 2216 KB Output is correct
24 Correct 18 ms 2388 KB Output is correct
25 Correct 16 ms 2132 KB Output is correct
26 Correct 16 ms 2212 KB Output is correct
27 Correct 15 ms 2144 KB Output is correct
28 Correct 15 ms 2080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3920 KB Output is correct
2 Correct 69 ms 8300 KB Output is correct
3 Correct 136 ms 13708 KB Output is correct
4 Correct 149 ms 13924 KB Output is correct
5 Correct 135 ms 13704 KB Output is correct
6 Correct 149 ms 13632 KB Output is correct
7 Correct 134 ms 13580 KB Output is correct
8 Correct 141 ms 13632 KB Output is correct
9 Correct 133 ms 13632 KB Output is correct
10 Correct 106 ms 11012 KB Output is correct
11 Correct 108 ms 10924 KB Output is correct
12 Correct 106 ms 10976 KB Output is correct
13 Correct 107 ms 10944 KB Output is correct
14 Correct 106 ms 10956 KB Output is correct
15 Correct 105 ms 10968 KB Output is correct
16 Correct 101 ms 10944 KB Output is correct
17 Correct 110 ms 10956 KB Output is correct
18 Correct 102 ms 10956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 4 ms 724 KB Output is correct
5 Correct 4 ms 820 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 824 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 4 ms 692 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
19 Correct 15 ms 2204 KB Output is correct
20 Correct 17 ms 2428 KB Output is correct
21 Correct 17 ms 2388 KB Output is correct
22 Correct 17 ms 2396 KB Output is correct
23 Correct 16 ms 2216 KB Output is correct
24 Correct 18 ms 2388 KB Output is correct
25 Correct 16 ms 2132 KB Output is correct
26 Correct 16 ms 2212 KB Output is correct
27 Correct 15 ms 2144 KB Output is correct
28 Correct 15 ms 2080 KB Output is correct
29 Correct 19 ms 3920 KB Output is correct
30 Correct 69 ms 8300 KB Output is correct
31 Correct 136 ms 13708 KB Output is correct
32 Correct 149 ms 13924 KB Output is correct
33 Correct 135 ms 13704 KB Output is correct
34 Correct 149 ms 13632 KB Output is correct
35 Correct 134 ms 13580 KB Output is correct
36 Correct 141 ms 13632 KB Output is correct
37 Correct 133 ms 13632 KB Output is correct
38 Correct 106 ms 11012 KB Output is correct
39 Correct 108 ms 10924 KB Output is correct
40 Correct 106 ms 10976 KB Output is correct
41 Correct 107 ms 10944 KB Output is correct
42 Correct 106 ms 10956 KB Output is correct
43 Correct 105 ms 10968 KB Output is correct
44 Correct 101 ms 10944 KB Output is correct
45 Correct 110 ms 10956 KB Output is correct
46 Correct 102 ms 10956 KB Output is correct
47 Correct 608 ms 45800 KB Output is correct
48 Correct 2913 ms 126280 KB Output is correct
49 Correct 3137 ms 135864 KB Output is correct
50 Correct 3168 ms 135660 KB Output is correct
51 Correct 3249 ms 135736 KB Output is correct
52 Correct 3319 ms 135772 KB Output is correct
53 Correct 3252 ms 135716 KB Output is correct
54 Correct 3085 ms 136000 KB Output is correct
55 Correct 3100 ms 135876 KB Output is correct
56 Correct 3349 ms 135912 KB Output is correct
57 Correct 3215 ms 135824 KB Output is correct
58 Correct 3172 ms 135780 KB Output is correct
59 Correct 2748 ms 126712 KB Output is correct
60 Correct 2679 ms 126708 KB Output is correct
61 Correct 2624 ms 126640 KB Output is correct
62 Correct 2237 ms 122732 KB Output is correct
63 Correct 2410 ms 122596 KB Output is correct
64 Correct 2371 ms 122688 KB Output is correct
65 Correct 2236 ms 118544 KB Output is correct
66 Correct 2200 ms 118656 KB Output is correct
67 Correct 2136 ms 118540 KB Output is correct