Submission #84471

# Submission time Handle Problem Language Result Execution time Memory
84471 2018-11-15T11:14:25 Z bogdan10bos Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
1828 ms 300184 KB
#include <bits/stdc++.h>

//#define DEBUG

#ifndef DEBUG
#include "bubblesort2.h"
#endif

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, int> p3i;

int N, Q;

class SegmentTree
{
public:
    int N, sti, dri, val, pos;
    int aint[4000005], add[4000005];

    void lazy(int nod, int st, int dr)
    {
        if(!add[nod])   return;

        aint[nod * 2] += add[nod];
        add[nod * 2] += add[nod];

        aint[nod * 2 + 1] += add[nod];
        add[nod * 2 + 1] += add[nod];

        add[nod] = 0;
    }

    void B(int nod, int st, int dr)
    {
        aint[nod] = -(1 << 30);
        add[nod] = 0;
        if(st == dr)    return;
        int mij = st + (dr - st) / 2;
        B(nod * 2, st, mij);
        B(nod * 2 + 1, mij + 1, dr);
    }

    void U(int nod, int st, int dr)
    {
        if(sti <= st && dr <= dri)
        {
            aint[nod] += val;
            add[nod] += val;
            return;
        }

        int mij = st + (dr - st) / 2;
        lazy(nod, st, dr);

        if(sti <= mij)  U(nod * 2, st, mij);
        if(mij < dri)   U(nod * 2 + 1, mij + 1, dr);

        aint[nod] = max(aint[nod * 2], aint[nod * 2 + 1]);
    }

    void Up(int nod, int st, int dr)
    {
        if(st == dr)
        {
            aint[nod] = val;
            add[nod] = 0;
            return;
        }

        int mij = st + (dr - st) / 2;
        lazy(nod, st, dr);

        if(pos <= mij)  Up(nod * 2, st, mij);
        else   Up(nod * 2 + 1, mij + 1, dr);

        aint[nod] = max(aint[nod * 2], aint[nod * 2 + 1]);
    }

    void build() { B(1, 1, N); }

    int query() { return aint[1]; }

    void update(int st, int dr, int _val)
    {
        if(st > dr)   return;
        sti = st, dri = dr, val = _val;
        U(1, 1, N);
    }

    void updatepos(int _pos, int _val)
    {
        pos = _pos; val = _val;
        Up(1, 1, N);
    }

}aint;

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V)
{
	N = A.size(), Q = V.size();
	vector<int> ret;
	ret.resize(Q);

	vector<p3i> nrm;
	for(int i = 0; i < N; i++)  nrm.push_back({ {A[i], i}, 0 });
	for(int i = 0; i < Q; i++)  nrm.push_back({ {V[i], X[i]}, i + 1 });
	sort(nrm.begin(), nrm.end());
	for(int i = 0; i < nrm.size(); i++)
    {
        int val = i + 1;
        if(nrm[i].second == 0)  A[ nrm[i].first.second ] = val;
        else    V[ nrm[i].second - 1 ] = val;
    }

	aint.N = N + Q;
	aint.build();

	for(int i = 0; i < N; i++)
        aint.update(A[i], A[i], i + (1 << 30));
    for(int i = 0; i < N; i++)
        aint.update(A[i] + 1, N + Q, -1);

    for(int q = 0; q < Q; q++)
    {
        int pos = X[q];
        int val = V[q];
        int lstval = A[pos];
        A[pos] = val;

        aint.update(lstval, lstval, -(1 << 30) - pos);
        aint.update(lstval + 1, N + Q, 1);

        aint.update(val, val, (1 << 30) + pos);
        aint.update(val + 1, N + Q, -1);

        ret[q] = aint.query();
    }

    return ret;
}

#ifdef DEBUG
int readInt(){
	int i;
	if(scanf("%d",&i)!=1){
		fprintf(stderr,"Error while reading input\n");
		exit(1);
	}
	return i;
}

int main(){

    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);

	int N,Q;
	N=readInt();
	Q=readInt();

	std::vector<int> A(N);
	for(int i=0;i<N;i++)
		A[i]=readInt();

	std::vector<int> X(Q),V(Q);
	for(int j=0;j<Q;j++){
		X[j]=readInt();
		V[j]=readInt();
	}

	std::vector<int> res=countScans(A,X,V);

	for(int j=0;j<int(res.size());j++)
		printf("%d\n",res[j]);
}
#endif

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:110:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < nrm.size(); i++)
                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 644 KB Output is correct
3 Correct 6 ms 712 KB Output is correct
4 Correct 6 ms 824 KB Output is correct
5 Correct 6 ms 1036 KB Output is correct
6 Correct 6 ms 1100 KB Output is correct
7 Correct 6 ms 1140 KB Output is correct
8 Correct 6 ms 1252 KB Output is correct
9 Correct 6 ms 1252 KB Output is correct
10 Correct 5 ms 1252 KB Output is correct
11 Correct 5 ms 1284 KB Output is correct
12 Correct 5 ms 1324 KB Output is correct
13 Correct 5 ms 1380 KB Output is correct
14 Correct 5 ms 1436 KB Output is correct
15 Correct 6 ms 1484 KB Output is correct
16 Correct 6 ms 1540 KB Output is correct
17 Correct 6 ms 1584 KB Output is correct
18 Correct 5 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 644 KB Output is correct
3 Correct 6 ms 712 KB Output is correct
4 Correct 6 ms 824 KB Output is correct
5 Correct 6 ms 1036 KB Output is correct
6 Correct 6 ms 1100 KB Output is correct
7 Correct 6 ms 1140 KB Output is correct
8 Correct 6 ms 1252 KB Output is correct
9 Correct 6 ms 1252 KB Output is correct
10 Correct 5 ms 1252 KB Output is correct
11 Correct 5 ms 1284 KB Output is correct
12 Correct 5 ms 1324 KB Output is correct
13 Correct 5 ms 1380 KB Output is correct
14 Correct 5 ms 1436 KB Output is correct
15 Correct 6 ms 1484 KB Output is correct
16 Correct 6 ms 1540 KB Output is correct
17 Correct 6 ms 1584 KB Output is correct
18 Correct 5 ms 1620 KB Output is correct
19 Correct 16 ms 2240 KB Output is correct
20 Correct 18 ms 2532 KB Output is correct
21 Correct 19 ms 2724 KB Output is correct
22 Correct 19 ms 2920 KB Output is correct
23 Correct 17 ms 3116 KB Output is correct
24 Correct 18 ms 3280 KB Output is correct
25 Correct 18 ms 3320 KB Output is correct
26 Correct 17 ms 3476 KB Output is correct
27 Correct 26 ms 3636 KB Output is correct
28 Correct 18 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 4472 KB Output is correct
2 Correct 72 ms 6164 KB Output is correct
3 Correct 118 ms 8780 KB Output is correct
4 Correct 119 ms 9348 KB Output is correct
5 Correct 118 ms 10044 KB Output is correct
6 Correct 119 ms 10492 KB Output is correct
7 Correct 118 ms 11144 KB Output is correct
8 Correct 110 ms 11716 KB Output is correct
9 Correct 119 ms 12324 KB Output is correct
10 Correct 103 ms 13036 KB Output is correct
11 Correct 101 ms 13708 KB Output is correct
12 Correct 106 ms 14348 KB Output is correct
13 Correct 103 ms 15132 KB Output is correct
14 Correct 99 ms 15648 KB Output is correct
15 Correct 99 ms 16300 KB Output is correct
16 Correct 101 ms 16936 KB Output is correct
17 Correct 99 ms 17568 KB Output is correct
18 Correct 101 ms 18224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 644 KB Output is correct
3 Correct 6 ms 712 KB Output is correct
4 Correct 6 ms 824 KB Output is correct
5 Correct 6 ms 1036 KB Output is correct
6 Correct 6 ms 1100 KB Output is correct
7 Correct 6 ms 1140 KB Output is correct
8 Correct 6 ms 1252 KB Output is correct
9 Correct 6 ms 1252 KB Output is correct
10 Correct 5 ms 1252 KB Output is correct
11 Correct 5 ms 1284 KB Output is correct
12 Correct 5 ms 1324 KB Output is correct
13 Correct 5 ms 1380 KB Output is correct
14 Correct 5 ms 1436 KB Output is correct
15 Correct 6 ms 1484 KB Output is correct
16 Correct 6 ms 1540 KB Output is correct
17 Correct 6 ms 1584 KB Output is correct
18 Correct 5 ms 1620 KB Output is correct
19 Correct 16 ms 2240 KB Output is correct
20 Correct 18 ms 2532 KB Output is correct
21 Correct 19 ms 2724 KB Output is correct
22 Correct 19 ms 2920 KB Output is correct
23 Correct 17 ms 3116 KB Output is correct
24 Correct 18 ms 3280 KB Output is correct
25 Correct 18 ms 3320 KB Output is correct
26 Correct 17 ms 3476 KB Output is correct
27 Correct 26 ms 3636 KB Output is correct
28 Correct 18 ms 3796 KB Output is correct
29 Correct 24 ms 4472 KB Output is correct
30 Correct 72 ms 6164 KB Output is correct
31 Correct 118 ms 8780 KB Output is correct
32 Correct 119 ms 9348 KB Output is correct
33 Correct 118 ms 10044 KB Output is correct
34 Correct 119 ms 10492 KB Output is correct
35 Correct 118 ms 11144 KB Output is correct
36 Correct 110 ms 11716 KB Output is correct
37 Correct 119 ms 12324 KB Output is correct
38 Correct 103 ms 13036 KB Output is correct
39 Correct 101 ms 13708 KB Output is correct
40 Correct 106 ms 14348 KB Output is correct
41 Correct 103 ms 15132 KB Output is correct
42 Correct 99 ms 15648 KB Output is correct
43 Correct 99 ms 16300 KB Output is correct
44 Correct 101 ms 16936 KB Output is correct
45 Correct 99 ms 17568 KB Output is correct
46 Correct 101 ms 18224 KB Output is correct
47 Correct 453 ms 32924 KB Output is correct
48 Correct 1606 ms 69068 KB Output is correct
49 Correct 1780 ms 83976 KB Output is correct
50 Correct 1828 ms 96868 KB Output is correct
51 Correct 1776 ms 109760 KB Output is correct
52 Correct 1768 ms 122788 KB Output is correct
53 Correct 1772 ms 135608 KB Output is correct
54 Correct 1672 ms 148536 KB Output is correct
55 Correct 1802 ms 161576 KB Output is correct
56 Correct 1619 ms 174512 KB Output is correct
57 Correct 1694 ms 187548 KB Output is correct
58 Correct 1584 ms 200740 KB Output is correct
59 Correct 1533 ms 211868 KB Output is correct
60 Correct 1648 ms 223636 KB Output is correct
61 Correct 1716 ms 235180 KB Output is correct
62 Correct 1529 ms 246788 KB Output is correct
63 Correct 1548 ms 257764 KB Output is correct
64 Correct 1514 ms 268732 KB Output is correct
65 Correct 1490 ms 279212 KB Output is correct
66 Correct 1439 ms 289696 KB Output is correct
67 Correct 1471 ms 300184 KB Output is correct