답안 #102672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102672 2019-03-26T16:01:26 Z tincamatei Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
3247 ms 130560 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;

	void build(int nod, int l, int r, int val) {
		int mid = (l + r) / 2;

		if(l < r) {
			build(2 * nod, l, mid, val);
			build(2 * nod + 1, mid + 1, r, val);
		}

		aint[nod] = val;
		lazy[nod] = 0;
	}

	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 = *(poz[val].rbegin());
		poz[val].erase(i);
		if(poz[val].empty())
			newpoz = -INF;
		else
			newpoz = *(poz[val].rbegin());

		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;
		
		if(poz[val].empty())
			oldpoz = -INF;
		else
			oldpoz = *(poz[val].rbegin());
		poz[val].insert(i);
		newpoz = *(poz[val].rbegin());
		
		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->build(1, 0, aint->maxr, -INF - N);

		for(int i = 0; i < N; ++i)
			aint->insertPoz(i, v[i]);

		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() + 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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 78712 KB Output is correct
2 Correct 86 ms 78840 KB Output is correct
3 Correct 82 ms 79020 KB Output is correct
4 Correct 80 ms 78884 KB Output is correct
5 Correct 84 ms 78864 KB Output is correct
6 Correct 84 ms 78840 KB Output is correct
7 Correct 88 ms 78900 KB Output is correct
8 Correct 81 ms 78908 KB Output is correct
9 Correct 79 ms 78844 KB Output is correct
10 Correct 79 ms 78840 KB Output is correct
11 Correct 93 ms 78844 KB Output is correct
12 Correct 79 ms 79044 KB Output is correct
13 Correct 74 ms 78868 KB Output is correct
14 Correct 80 ms 78812 KB Output is correct
15 Correct 78 ms 79096 KB Output is correct
16 Correct 76 ms 78840 KB Output is correct
17 Correct 82 ms 78936 KB Output is correct
18 Correct 85 ms 78968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 78712 KB Output is correct
2 Correct 86 ms 78840 KB Output is correct
3 Correct 82 ms 79020 KB Output is correct
4 Correct 80 ms 78884 KB Output is correct
5 Correct 84 ms 78864 KB Output is correct
6 Correct 84 ms 78840 KB Output is correct
7 Correct 88 ms 78900 KB Output is correct
8 Correct 81 ms 78908 KB Output is correct
9 Correct 79 ms 78844 KB Output is correct
10 Correct 79 ms 78840 KB Output is correct
11 Correct 93 ms 78844 KB Output is correct
12 Correct 79 ms 79044 KB Output is correct
13 Correct 74 ms 78868 KB Output is correct
14 Correct 80 ms 78812 KB Output is correct
15 Correct 78 ms 79096 KB Output is correct
16 Correct 76 ms 78840 KB Output is correct
17 Correct 82 ms 78936 KB Output is correct
18 Correct 85 ms 78968 KB Output is correct
19 Correct 101 ms 79448 KB Output is correct
20 Correct 100 ms 79580 KB Output is correct
21 Correct 99 ms 79608 KB Output is correct
22 Correct 105 ms 79664 KB Output is correct
23 Correct 98 ms 79608 KB Output is correct
24 Correct 108 ms 79608 KB Output is correct
25 Correct 97 ms 79728 KB Output is correct
26 Correct 92 ms 79660 KB Output is correct
27 Correct 97 ms 79580 KB Output is correct
28 Correct 110 ms 79580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 80540 KB Output is correct
2 Correct 148 ms 82636 KB Output is correct
3 Correct 228 ms 84188 KB Output is correct
4 Correct 198 ms 84216 KB Output is correct
5 Correct 259 ms 84248 KB Output is correct
6 Correct 224 ms 84128 KB Output is correct
7 Correct 212 ms 84316 KB Output is correct
8 Correct 234 ms 84232 KB Output is correct
9 Correct 212 ms 84216 KB Output is correct
10 Correct 188 ms 84296 KB Output is correct
11 Correct 179 ms 84144 KB Output is correct
12 Correct 180 ms 84152 KB Output is correct
13 Correct 157 ms 84168 KB Output is correct
14 Correct 156 ms 84128 KB Output is correct
15 Correct 158 ms 84244 KB Output is correct
16 Correct 145 ms 84100 KB Output is correct
17 Correct 149 ms 84188 KB Output is correct
18 Correct 146 ms 84216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 78712 KB Output is correct
2 Correct 86 ms 78840 KB Output is correct
3 Correct 82 ms 79020 KB Output is correct
4 Correct 80 ms 78884 KB Output is correct
5 Correct 84 ms 78864 KB Output is correct
6 Correct 84 ms 78840 KB Output is correct
7 Correct 88 ms 78900 KB Output is correct
8 Correct 81 ms 78908 KB Output is correct
9 Correct 79 ms 78844 KB Output is correct
10 Correct 79 ms 78840 KB Output is correct
11 Correct 93 ms 78844 KB Output is correct
12 Correct 79 ms 79044 KB Output is correct
13 Correct 74 ms 78868 KB Output is correct
14 Correct 80 ms 78812 KB Output is correct
15 Correct 78 ms 79096 KB Output is correct
16 Correct 76 ms 78840 KB Output is correct
17 Correct 82 ms 78936 KB Output is correct
18 Correct 85 ms 78968 KB Output is correct
19 Correct 101 ms 79448 KB Output is correct
20 Correct 100 ms 79580 KB Output is correct
21 Correct 99 ms 79608 KB Output is correct
22 Correct 105 ms 79664 KB Output is correct
23 Correct 98 ms 79608 KB Output is correct
24 Correct 108 ms 79608 KB Output is correct
25 Correct 97 ms 79728 KB Output is correct
26 Correct 92 ms 79660 KB Output is correct
27 Correct 97 ms 79580 KB Output is correct
28 Correct 110 ms 79580 KB Output is correct
29 Correct 106 ms 80540 KB Output is correct
30 Correct 148 ms 82636 KB Output is correct
31 Correct 228 ms 84188 KB Output is correct
32 Correct 198 ms 84216 KB Output is correct
33 Correct 259 ms 84248 KB Output is correct
34 Correct 224 ms 84128 KB Output is correct
35 Correct 212 ms 84316 KB Output is correct
36 Correct 234 ms 84232 KB Output is correct
37 Correct 212 ms 84216 KB Output is correct
38 Correct 188 ms 84296 KB Output is correct
39 Correct 179 ms 84144 KB Output is correct
40 Correct 180 ms 84152 KB Output is correct
41 Correct 157 ms 84168 KB Output is correct
42 Correct 156 ms 84128 KB Output is correct
43 Correct 158 ms 84244 KB Output is correct
44 Correct 145 ms 84100 KB Output is correct
45 Correct 149 ms 84188 KB Output is correct
46 Correct 146 ms 84216 KB Output is correct
47 Correct 805 ms 95324 KB Output is correct
48 Correct 2987 ms 125108 KB Output is correct
49 Correct 3159 ms 130464 KB Output is correct
50 Correct 2977 ms 130540 KB Output is correct
51 Correct 3247 ms 130544 KB Output is correct
52 Correct 3063 ms 130384 KB Output is correct
53 Correct 3028 ms 130440 KB Output is correct
54 Correct 2911 ms 130448 KB Output is correct
55 Correct 2833 ms 130560 KB Output is correct
56 Correct 2666 ms 130548 KB Output is correct
57 Correct 2889 ms 130452 KB Output is correct
58 Correct 2792 ms 130476 KB Output is correct
59 Correct 2217 ms 130312 KB Output is correct
60 Correct 2283 ms 130524 KB Output is correct
61 Correct 2658 ms 130532 KB Output is correct
62 Correct 2564 ms 130448 KB Output is correct
63 Correct 2402 ms 130548 KB Output is correct
64 Correct 2379 ms 130356 KB Output is correct
65 Correct 2234 ms 130544 KB Output is correct
66 Correct 2337 ms 130492 KB Output is correct
67 Correct 2207 ms 130420 KB Output is correct