Submission #854800

# Submission time Handle Problem Language Result Execution time Memory
854800 2023-09-29T02:37:19 Z anha3k25cvp Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
2464 ms 93684 KB
/* Solution
Let n be size of A and q be amount of queries.
O(n log n * q):
1. Suppose there is array B which is {A[i], i} for all i. Also it's sorted.
2. Then the answer is max for all i in [0; n - 1] (i - j), where B[j] = {A[i], i}.
If we build B and calculate answer after each query, we get O(n log n * q) solution.
O((n + q) log n):
3. To build B faster it's easy to notice that only one element in B moves from one position to another. Then what is needed is to pull element out from one position and put it into another one. It can be easily done by treap.
4. To calculate answer, let every element with index i (according to A) in treap have value c = (i - j) where B[j] = {A[i], i}.
5. Since answer is maximum among them, then each treap should also store maximum value in its subtree.
6. After update, when element moves from one place to another, there is a segment which also moves. In other words, j for each element in this segment changes by the same amount. So, it's also needed to increase or decrease c on a treap by the same amount. It can be done by lazy propagation.
*/
#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct treap {
	treap *l = nullptr, *r = nullptr;
	int x, y, p, siz = 1;
	int c;
	int lazy = 0;
	int val;
	
	void pushdown() {
		if (l != nullptr) l -> lazy += lazy;
		if (r != nullptr) r -> lazy += lazy;
		val += lazy;
		c += lazy;
		lazy = 0;
	}
	
	void upd() {
		pushdown();
		if (l != nullptr) l -> pushdown();
		if (r != nullptr) r -> pushdown();
		siz = ((l == nullptr) ? 0 : l -> siz) + ((r == nullptr) ? 0 : r -> siz) + 1;
		val = max(c, max((l == nullptr) ? c : l -> val, (r == nullptr) ? c : r -> val));
	}
	
	treap(int a, int pp, int cc) : x(a), y(rng()), p(pp), c(cc) {val = c;}
};

int siz(treap *t) { return (t == nullptr) ? 0 : t -> siz; }

treap* merge(treap *t1, treap *t2) {
	if (t1 == nullptr)
		return t2;
	if (t2 == nullptr)
		return t1;
	t1 -> pushdown();
	t2 -> pushdown();
	if (t1 -> y > t2 -> y) {
		t1 -> r = merge(t1 -> r, t2);
		t1 -> upd();
		return t1;
	} else {
		t2 -> l = merge(t1, t2 -> l);
		t2 -> upd();
		return t2;
	}
}

pair<treap*, treap*> split(treap *t, int x, int pos) {
	if (t == nullptr)
		return {nullptr, nullptr};
	t -> pushdown();
	if (t -> x > x || (t -> x == x && t -> p >= pos)) {
		auto tmp = split(t -> l, x, pos);
		t -> l = tmp.second;
		t -> upd();
		return {tmp.first, t};
	} else {
		auto tmp = split(t -> r, x, pos);
		t -> r = tmp.first;
		t -> upd();
		return {t, tmp.second};
	}
}

inline void addAll(treap *t, int a) { if (t != nullptr) t -> lazy += a; }

treap *root;

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
	int n = A.size();
	pair<int, int> B[n];
	for (int i = 0; i < n; i++)
		B[i] = {A[i], i};
	sort(B, B + n);
	
	for (int i = 0; i < n; i++) 
		root = merge(root, new treap(B[i].fs, B[i].sc, B[i].sc - i));
	
	vector<int> answer(X.size());
	for (unsigned int query = 0; query < X.size(); query++) {
		int pos = X[query], val = V[query];
		if (A[pos] > val) {
			//a.fs -end position of element- b.fs -this element- th.sc
			auto a = split(root, val, pos);
			auto b = split(a.sc, A[pos], pos);
			auto th = split(b.sc, A[pos], pos + 1);
			addAll(b.fs, -1);
			treap *d = new treap(val, pos, pos - siz(a.fs));
			root = merge(merge(a.fs, d), merge(b.fs, th.sc));
		} else {
			//a.fs -this element- b.fs -end position of element- b.sc
			auto a = split(root, A[pos], pos);
			auto th = split(a.sc, A[pos], pos + 1);
			auto b = split(th.sc, val, pos);
			addAll(b.fs, 1);
			treap *d = new treap(val, pos, pos - siz(a.fs) - siz(b.fs));
			root = merge(merge(a.fs, b.fs), merge(d, b.sc));
		}
		
		A[pos] = val;
		root -> pushdown();
		answer[query] = root -> val;
	}
		
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 4 ms 604 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 4 ms 760 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 604 KB Output is correct
10 Correct 4 ms 604 KB Output is correct
11 Correct 4 ms 604 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 604 KB Output is correct
15 Correct 4 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 856 KB Output is correct
18 Correct 4 ms 704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 4 ms 604 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 4 ms 760 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 604 KB Output is correct
10 Correct 4 ms 604 KB Output is correct
11 Correct 4 ms 604 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 604 KB Output is correct
15 Correct 4 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 856 KB Output is correct
18 Correct 4 ms 704 KB Output is correct
19 Correct 15 ms 1708 KB Output is correct
20 Correct 18 ms 2000 KB Output is correct
21 Correct 17 ms 1884 KB Output is correct
22 Correct 17 ms 1884 KB Output is correct
23 Correct 16 ms 1884 KB Output is correct
24 Correct 17 ms 1876 KB Output is correct
25 Correct 17 ms 1876 KB Output is correct
26 Correct 16 ms 1884 KB Output is correct
27 Correct 16 ms 1888 KB Output is correct
28 Correct 17 ms 1704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2652 KB Output is correct
2 Correct 67 ms 5812 KB Output is correct
3 Correct 124 ms 8920 KB Output is correct
4 Correct 127 ms 8772 KB Output is correct
5 Correct 123 ms 8796 KB Output is correct
6 Correct 130 ms 8820 KB Output is correct
7 Correct 125 ms 8784 KB Output is correct
8 Correct 125 ms 8784 KB Output is correct
9 Correct 124 ms 8788 KB Output is correct
10 Correct 92 ms 9300 KB Output is correct
11 Correct 90 ms 9040 KB Output is correct
12 Correct 94 ms 9040 KB Output is correct
13 Correct 97 ms 9048 KB Output is correct
14 Correct 90 ms 8984 KB Output is correct
15 Correct 94 ms 9040 KB Output is correct
16 Correct 99 ms 9008 KB Output is correct
17 Correct 100 ms 9064 KB Output is correct
18 Correct 103 ms 9300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 4 ms 604 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 4 ms 604 KB Output is correct
7 Correct 4 ms 760 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 4 ms 604 KB Output is correct
10 Correct 4 ms 604 KB Output is correct
11 Correct 4 ms 604 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 604 KB Output is correct
15 Correct 4 ms 604 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 856 KB Output is correct
18 Correct 4 ms 704 KB Output is correct
19 Correct 15 ms 1708 KB Output is correct
20 Correct 18 ms 2000 KB Output is correct
21 Correct 17 ms 1884 KB Output is correct
22 Correct 17 ms 1884 KB Output is correct
23 Correct 16 ms 1884 KB Output is correct
24 Correct 17 ms 1876 KB Output is correct
25 Correct 17 ms 1876 KB Output is correct
26 Correct 16 ms 1884 KB Output is correct
27 Correct 16 ms 1888 KB Output is correct
28 Correct 17 ms 1704 KB Output is correct
29 Correct 16 ms 2652 KB Output is correct
30 Correct 67 ms 5812 KB Output is correct
31 Correct 124 ms 8920 KB Output is correct
32 Correct 127 ms 8772 KB Output is correct
33 Correct 123 ms 8796 KB Output is correct
34 Correct 130 ms 8820 KB Output is correct
35 Correct 125 ms 8784 KB Output is correct
36 Correct 125 ms 8784 KB Output is correct
37 Correct 124 ms 8788 KB Output is correct
38 Correct 92 ms 9300 KB Output is correct
39 Correct 90 ms 9040 KB Output is correct
40 Correct 94 ms 9040 KB Output is correct
41 Correct 97 ms 9048 KB Output is correct
42 Correct 90 ms 8984 KB Output is correct
43 Correct 94 ms 9040 KB Output is correct
44 Correct 99 ms 9008 KB Output is correct
45 Correct 100 ms 9064 KB Output is correct
46 Correct 103 ms 9300 KB Output is correct
47 Correct 455 ms 27732 KB Output is correct
48 Correct 2198 ms 85392 KB Output is correct
49 Correct 2379 ms 93544 KB Output is correct
50 Correct 2403 ms 93540 KB Output is correct
51 Correct 2464 ms 93512 KB Output is correct
52 Correct 2401 ms 93500 KB Output is correct
53 Correct 2444 ms 93452 KB Output is correct
54 Correct 2433 ms 93680 KB Output is correct
55 Correct 2432 ms 93684 KB Output is correct
56 Correct 2310 ms 93508 KB Output is correct
57 Correct 2416 ms 93512 KB Output is correct
58 Correct 2391 ms 93572 KB Output is correct
59 Correct 2144 ms 92328 KB Output is correct
60 Correct 2081 ms 92204 KB Output is correct
61 Correct 2208 ms 92244 KB Output is correct
62 Correct 1955 ms 92028 KB Output is correct
63 Correct 2013 ms 92348 KB Output is correct
64 Correct 2050 ms 92192 KB Output is correct
65 Correct 1902 ms 92104 KB Output is correct
66 Correct 1887 ms 92348 KB Output is correct
67 Correct 1888 ms 92336 KB Output is correct