Submission #102674

# Submission time Handle Problem Language Result Execution time Memory
102674 2019-03-26T16:21:21 Z tincamatei Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
2613 ms 130032 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"

using namespace std;

const int MAX_N = 500000;
const int MAX_Q = 500000;
const int MAX_VALS = MAX_N + MAX_Q;
const int INF = 1000000000;

struct Aint {
	int aint[1+4*MAX_VALS];
	int lazy[1+4*MAX_VALS];
	set<int> poz[MAX_VALS];
	int maxr;
	int higher;

	int getbest(int val) {
		if(poz[val].empty())
			return -INF;
		else
			return *(poz[val].rbegin());
	}

	void build(int nod, int l, int r) {
		int mid = (l + r) / 2;
		lazy[nod] = 0;

		if(l == r) {
			higher -= poz[l].size();
			aint[nod] = getbest(l) + higher;
		} else if(l < r) {
			build(2 * nod, l, mid);
			build(2 * nod + 1, mid + 1, r);
			aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
		}
	}

	void propagate(int nod, int l, int r) {
		aint[nod] += lazy[nod];
		if(l < r) {
			lazy[2 * nod] += lazy[nod];
			lazy[2 * nod + 1] += lazy[nod];
		}
		lazy[nod] = 0;
	}
	
	void addRange(int i, int j, int val, int l, int r, int nod) {
		propagate(nod, l, r);
		if(j < l || r < i || j < i) return ;

		if(i <= l && r <= j) {
			lazy[nod] += val;
			propagate(nod, l, r);
		} else {
			int mid = (l + r) / 2;
			addRange(i, j, val, l, mid, 2 * nod);
			addRange(i, j, val, mid + 1, r, 2 * nod + 1);

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

	void erasePoz(int i, int val) {
		int oldpoz, newpoz;
		
		oldpoz = getbest(val);
		poz[val].erase(i);
		newpoz = getbest(val);

		addRange(val, val, -oldpoz + newpoz, 0, maxr, 1);
		addRange(0, val - 1, -1, 0, maxr, 1);
	}

	void insertPoz(int i, int val) {
		int oldpoz, newpoz;
		
		oldpoz = getbest(val);
		poz[val].insert(i);
		newpoz = getbest(val);

		addRange(val, val, -oldpoz + newpoz, 0, maxr, 1);
		addRange(0, val - 1, 1, 0, maxr, 1);
	}
	
	int query() {
		propagate(1, 0, maxr);
		return aint[1];
	}
};

struct Solver {
	int N, Q;
	int v[MAX_N], poz[MAX_Q], val[MAX_Q];
	int *p[MAX_N+MAX_Q];
	Aint *aint;

	Solver(const vector<int> &A, const vector<int> &X, const vector<int> &V) {
		N = A.size();
		Q = X.size();

		for(int i = 0; i < N; ++i)
			v[i] = A[i];
		for(int i = 0; i < Q; ++i) {
			poz[i] = X[i];
			val[i] = V[i];
		}
	}
	
	static bool cmp(int *a, int *b) {
		return *a < *b;
	}

	int normalize() {
		int top = 0;

		for(int i = 0; i < N; ++i)
			p[top++] = &v[i];
		for(int i = 0; i < Q; ++i)
			p[top++] = &val[i];

		sort(p, p + top, cmp);

		int last = *p[0], j = 0;
		for(int i = 0; i < top; ++i)
			if(*p[i] == last)
				*p[i] = j;
			else {
				last = *p[i];
				*p[i] = ++j;
			}

		return j;
	}

	vector<int> getans() {
		vector<int> ans;

		aint = new Aint();
		aint->maxr = normalize();
		aint->higher = N;

		for(int i = 0; i < N; ++i)
			aint->poz[v[i]].insert(i);
		
		aint->build(1, 0, aint->maxr);

		for(int i = 0; i < Q; ++i) {
			aint->erasePoz(poz[i], v[poz[i]]);
			aint->insertPoz(poz[i], val[i]);
			
			v[poz[i]] = val[i];

			ans.push_back(aint->query() - N + 1);
		}

		return ans;
	}
};

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
	Solver *solver = new Solver(A, X, V);

	return solver->getans();
}
# Verdict Execution time Memory Grader output
1 Correct 70 ms 78712 KB Output is correct
2 Correct 77 ms 78712 KB Output is correct
3 Correct 81 ms 78840 KB Output is correct
4 Correct 86 ms 78968 KB Output is correct
5 Correct 92 ms 78840 KB Output is correct
6 Correct 80 ms 78964 KB Output is correct
7 Correct 83 ms 78896 KB Output is correct
8 Correct 83 ms 78840 KB Output is correct
9 Correct 80 ms 78876 KB Output is correct
10 Correct 79 ms 78812 KB Output is correct
11 Correct 78 ms 78840 KB Output is correct
12 Correct 78 ms 78840 KB Output is correct
13 Correct 70 ms 78840 KB Output is correct
14 Correct 71 ms 78840 KB Output is correct
15 Correct 71 ms 78840 KB Output is correct
16 Correct 75 ms 78804 KB Output is correct
17 Correct 80 ms 78812 KB Output is correct
18 Correct 76 ms 78968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 78712 KB Output is correct
2 Correct 77 ms 78712 KB Output is correct
3 Correct 81 ms 78840 KB Output is correct
4 Correct 86 ms 78968 KB Output is correct
5 Correct 92 ms 78840 KB Output is correct
6 Correct 80 ms 78964 KB Output is correct
7 Correct 83 ms 78896 KB Output is correct
8 Correct 83 ms 78840 KB Output is correct
9 Correct 80 ms 78876 KB Output is correct
10 Correct 79 ms 78812 KB Output is correct
11 Correct 78 ms 78840 KB Output is correct
12 Correct 78 ms 78840 KB Output is correct
13 Correct 70 ms 78840 KB Output is correct
14 Correct 71 ms 78840 KB Output is correct
15 Correct 71 ms 78840 KB Output is correct
16 Correct 75 ms 78804 KB Output is correct
17 Correct 80 ms 78812 KB Output is correct
18 Correct 76 ms 78968 KB Output is correct
19 Correct 92 ms 79412 KB Output is correct
20 Correct 93 ms 79492 KB Output is correct
21 Correct 96 ms 79556 KB Output is correct
22 Correct 84 ms 79608 KB Output is correct
23 Correct 83 ms 79480 KB Output is correct
24 Correct 79 ms 79480 KB Output is correct
25 Correct 104 ms 79596 KB Output is correct
26 Correct 93 ms 79608 KB Output is correct
27 Correct 92 ms 79480 KB Output is correct
28 Correct 88 ms 79480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 80616 KB Output is correct
2 Correct 120 ms 82280 KB Output is correct
3 Correct 168 ms 83960 KB Output is correct
4 Correct 179 ms 83960 KB Output is correct
5 Correct 197 ms 83964 KB Output is correct
6 Correct 197 ms 84004 KB Output is correct
7 Correct 188 ms 83960 KB Output is correct
8 Correct 204 ms 83964 KB Output is correct
9 Correct 193 ms 84100 KB Output is correct
10 Correct 165 ms 83960 KB Output is correct
11 Correct 157 ms 83960 KB Output is correct
12 Correct 154 ms 83960 KB Output is correct
13 Correct 154 ms 83884 KB Output is correct
14 Correct 161 ms 83960 KB Output is correct
15 Correct 148 ms 83960 KB Output is correct
16 Correct 151 ms 83960 KB Output is correct
17 Correct 172 ms 83960 KB Output is correct
18 Correct 144 ms 84088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 78712 KB Output is correct
2 Correct 77 ms 78712 KB Output is correct
3 Correct 81 ms 78840 KB Output is correct
4 Correct 86 ms 78968 KB Output is correct
5 Correct 92 ms 78840 KB Output is correct
6 Correct 80 ms 78964 KB Output is correct
7 Correct 83 ms 78896 KB Output is correct
8 Correct 83 ms 78840 KB Output is correct
9 Correct 80 ms 78876 KB Output is correct
10 Correct 79 ms 78812 KB Output is correct
11 Correct 78 ms 78840 KB Output is correct
12 Correct 78 ms 78840 KB Output is correct
13 Correct 70 ms 78840 KB Output is correct
14 Correct 71 ms 78840 KB Output is correct
15 Correct 71 ms 78840 KB Output is correct
16 Correct 75 ms 78804 KB Output is correct
17 Correct 80 ms 78812 KB Output is correct
18 Correct 76 ms 78968 KB Output is correct
19 Correct 92 ms 79412 KB Output is correct
20 Correct 93 ms 79492 KB Output is correct
21 Correct 96 ms 79556 KB Output is correct
22 Correct 84 ms 79608 KB Output is correct
23 Correct 83 ms 79480 KB Output is correct
24 Correct 79 ms 79480 KB Output is correct
25 Correct 104 ms 79596 KB Output is correct
26 Correct 93 ms 79608 KB Output is correct
27 Correct 92 ms 79480 KB Output is correct
28 Correct 88 ms 79480 KB Output is correct
29 Correct 89 ms 80616 KB Output is correct
30 Correct 120 ms 82280 KB Output is correct
31 Correct 168 ms 83960 KB Output is correct
32 Correct 179 ms 83960 KB Output is correct
33 Correct 197 ms 83964 KB Output is correct
34 Correct 197 ms 84004 KB Output is correct
35 Correct 188 ms 83960 KB Output is correct
36 Correct 204 ms 83964 KB Output is correct
37 Correct 193 ms 84100 KB Output is correct
38 Correct 165 ms 83960 KB Output is correct
39 Correct 157 ms 83960 KB Output is correct
40 Correct 154 ms 83960 KB Output is correct
41 Correct 154 ms 83884 KB Output is correct
42 Correct 161 ms 83960 KB Output is correct
43 Correct 148 ms 83960 KB Output is correct
44 Correct 151 ms 83960 KB Output is correct
45 Correct 172 ms 83960 KB Output is correct
46 Correct 144 ms 84088 KB Output is correct
47 Correct 667 ms 94748 KB Output is correct
48 Correct 2388 ms 124608 KB Output is correct
49 Correct 2613 ms 129924 KB Output is correct
50 Correct 2397 ms 130032 KB Output is correct
51 Correct 2264 ms 129888 KB Output is correct
52 Correct 2402 ms 129900 KB Output is correct
53 Correct 2538 ms 129896 KB Output is correct
54 Correct 2510 ms 129696 KB Output is correct
55 Correct 2256 ms 129792 KB Output is correct
56 Correct 2203 ms 129900 KB Output is correct
57 Correct 2337 ms 129972 KB Output is correct
58 Correct 2552 ms 129904 KB Output is correct
59 Correct 2102 ms 129888 KB Output is correct
60 Correct 2096 ms 129776 KB Output is correct
61 Correct 2103 ms 129904 KB Output is correct
62 Correct 1911 ms 129932 KB Output is correct
63 Correct 1966 ms 129776 KB Output is correct
64 Correct 1771 ms 129884 KB Output is correct
65 Correct 1874 ms 129832 KB Output is correct
66 Correct 1822 ms 129668 KB Output is correct
67 Correct 1781 ms 129904 KB Output is correct