Submission #776915

# Submission time Handle Problem Language Result Execution time Memory
776915 2023-07-08T11:48:54 Z PanosPask Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
4808 ms 268708 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct SegTree {
	const ll NO_OP = 0;
	const ll NEUTRAL = 0;

	int size;
	vector<ll> tree;
	vector<ll> operations;

	void init(int n) {
		size = 1;
		while (size < n)
			size *= 2;

		tree.resize(2 * size, NEUTRAL);
		operations.assign(2 * size, NO_OP);
	}

	void propagate(int x) {
		tree[2 * x + 1] += operations[x];
		tree[2 * x + 2] += operations[x];
		operations[2 * x + 1] += operations[x];
		operations[2 * x + 2] += operations[x];

		operations[x] = NO_OP;
	}

	void modify(int l, int r, int v, int x, int lx, int rx) {
		if (l >= rx || lx >= r) {
			return;
		}
		else if (l <= lx && rx <= r) {
			tree[x] += v;
			operations[x] += v;
			return;
		}

		propagate(x);

		int mid = (lx + rx) / 2;
		modify(l, r, v, 2 * x + 1, lx, mid);
		modify(l, r, v, 2 * x + 2, mid, rx);

		tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]);
	}
	void modify(int l, int r, int v) {
		modify(l, r, v, 0, 0, size);
	}

	ll calc(int l, int r, int x, int lx, int rx) {
		if (l >= rx || lx >= r) {
			return NEUTRAL;
		}
		else if (l <= lx && rx <= r) {
			return tree[x];
		}

		propagate(x);

		int mid = (lx + rx) / 2;
		ll c1 = calc(l, r, 2 * x + 1, lx, mid);
		ll c2 = calc(l, r, 2 * x + 2, mid, rx);

		return max(c1, c2);
	}
	ll calc(int l, int r) {
		return max(calc(l, r, 0, 0, size), 0ll);
	}
};

int N;
set<int> seen;
vector<set<int>> latest;
unordered_map<int, int> compress;
vector<int> startv;

SegTree st;

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V)
{
	// Coordinate compression
	for (int i = 0; i < A.size(); i++)
		seen.insert(A[i]);
	for (int i = 0; i < V.size(); i++)
		seen.insert(V[i]);
	for (auto val : seen)
		compress[val] = N++;

	latest.resize(N);
	st.init(N);
	for (int val = 0; val < N; val++)
		latest[val].insert(0);
	for (int i = 0; i < A.size(); i++) {
		A[i] = compress[A[i]];
		latest[A[i]].insert(i + 1);

		st.modify(A[i], N, -1);
	}

	for (int val = 0; val < N; val++) {
		st.modify(val, val + 1, *latest[val].rbegin());
	}

	vector<int> ans;
	for (int q = 0; q < X.size(); q++) {
		int i = X[q];
		int val = compress[V[q]];
		int prev = A[i];

		// Take care of the old value first
		st.modify(prev, N, +1);
		st.modify(prev, prev + 1, - *latest[prev].rbegin());
		latest[prev].erase(i + 1);
		st.modify(prev, prev + 1, *latest[prev].rbegin());

		// Proccess the new value
		st.modify(val, N, -1);
		st.modify(val, val + 1, - *latest[val].rbegin());
		latest[val].insert(i + 1);
		st.modify(val, val + 1, *latest[val].rbegin());

		A[i] = val;
		ans.push_back(st.calc(0, N));
	}

	return ans;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for (int i = 0; i < A.size(); i++)
      |                  ~~^~~~~~~~~~
bubblesort2.cpp:90:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |  for (int i = 0; i < V.size(); i++)
      |                  ~~^~~~~~~~~~
bubblesort2.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |  for (int i = 0; i < A.size(); i++) {
      |                  ~~^~~~~~~~~~
bubblesort2.cpp:111:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |  for (int q = 0; q < X.size(); q++) {
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 712 KB Output is correct
3 Correct 8 ms 1236 KB Output is correct
4 Correct 6 ms 1236 KB Output is correct
5 Correct 6 ms 1316 KB Output is correct
6 Correct 6 ms 1236 KB Output is correct
7 Correct 6 ms 1236 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 6 ms 1236 KB Output is correct
10 Correct 6 ms 1236 KB Output is correct
11 Correct 6 ms 1236 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 6 ms 1108 KB Output is correct
14 Correct 6 ms 1108 KB Output is correct
15 Correct 6 ms 1108 KB Output is correct
16 Correct 6 ms 1128 KB Output is correct
17 Correct 5 ms 1108 KB Output is correct
18 Correct 7 ms 1136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 712 KB Output is correct
3 Correct 8 ms 1236 KB Output is correct
4 Correct 6 ms 1236 KB Output is correct
5 Correct 6 ms 1316 KB Output is correct
6 Correct 6 ms 1236 KB Output is correct
7 Correct 6 ms 1236 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 6 ms 1236 KB Output is correct
10 Correct 6 ms 1236 KB Output is correct
11 Correct 6 ms 1236 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 6 ms 1108 KB Output is correct
14 Correct 6 ms 1108 KB Output is correct
15 Correct 6 ms 1108 KB Output is correct
16 Correct 6 ms 1128 KB Output is correct
17 Correct 5 ms 1108 KB Output is correct
18 Correct 7 ms 1136 KB Output is correct
19 Correct 31 ms 3972 KB Output is correct
20 Correct 26 ms 4368 KB Output is correct
21 Correct 26 ms 4300 KB Output is correct
22 Correct 26 ms 4372 KB Output is correct
23 Correct 26 ms 4024 KB Output is correct
24 Correct 34 ms 4036 KB Output is correct
25 Correct 25 ms 3796 KB Output is correct
26 Correct 24 ms 3812 KB Output is correct
27 Correct 24 ms 3684 KB Output is correct
28 Correct 24 ms 3660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1876 KB Output is correct
2 Correct 45 ms 3416 KB Output is correct
3 Correct 78 ms 4792 KB Output is correct
4 Correct 80 ms 4768 KB Output is correct
5 Correct 77 ms 4688 KB Output is correct
6 Correct 76 ms 4796 KB Output is correct
7 Correct 74 ms 4796 KB Output is correct
8 Correct 74 ms 4760 KB Output is correct
9 Correct 74 ms 4768 KB Output is correct
10 Correct 61 ms 4788 KB Output is correct
11 Correct 62 ms 4812 KB Output is correct
12 Correct 63 ms 4820 KB Output is correct
13 Correct 61 ms 4836 KB Output is correct
14 Correct 61 ms 4812 KB Output is correct
15 Correct 62 ms 4816 KB Output is correct
16 Correct 59 ms 4832 KB Output is correct
17 Correct 59 ms 4852 KB Output is correct
18 Correct 59 ms 4812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 712 KB Output is correct
3 Correct 8 ms 1236 KB Output is correct
4 Correct 6 ms 1236 KB Output is correct
5 Correct 6 ms 1316 KB Output is correct
6 Correct 6 ms 1236 KB Output is correct
7 Correct 6 ms 1236 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 6 ms 1236 KB Output is correct
10 Correct 6 ms 1236 KB Output is correct
11 Correct 6 ms 1236 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 6 ms 1108 KB Output is correct
14 Correct 6 ms 1108 KB Output is correct
15 Correct 6 ms 1108 KB Output is correct
16 Correct 6 ms 1128 KB Output is correct
17 Correct 5 ms 1108 KB Output is correct
18 Correct 7 ms 1136 KB Output is correct
19 Correct 31 ms 3972 KB Output is correct
20 Correct 26 ms 4368 KB Output is correct
21 Correct 26 ms 4300 KB Output is correct
22 Correct 26 ms 4372 KB Output is correct
23 Correct 26 ms 4024 KB Output is correct
24 Correct 34 ms 4036 KB Output is correct
25 Correct 25 ms 3796 KB Output is correct
26 Correct 24 ms 3812 KB Output is correct
27 Correct 24 ms 3684 KB Output is correct
28 Correct 24 ms 3660 KB Output is correct
29 Correct 14 ms 1876 KB Output is correct
30 Correct 45 ms 3416 KB Output is correct
31 Correct 78 ms 4792 KB Output is correct
32 Correct 80 ms 4768 KB Output is correct
33 Correct 77 ms 4688 KB Output is correct
34 Correct 76 ms 4796 KB Output is correct
35 Correct 74 ms 4796 KB Output is correct
36 Correct 74 ms 4760 KB Output is correct
37 Correct 74 ms 4768 KB Output is correct
38 Correct 61 ms 4788 KB Output is correct
39 Correct 62 ms 4812 KB Output is correct
40 Correct 63 ms 4820 KB Output is correct
41 Correct 61 ms 4836 KB Output is correct
42 Correct 61 ms 4812 KB Output is correct
43 Correct 62 ms 4816 KB Output is correct
44 Correct 59 ms 4832 KB Output is correct
45 Correct 59 ms 4852 KB Output is correct
46 Correct 59 ms 4812 KB Output is correct
47 Correct 997 ms 86772 KB Output is correct
48 Correct 4074 ms 248212 KB Output is correct
49 Correct 4497 ms 268572 KB Output is correct
50 Correct 4501 ms 268584 KB Output is correct
51 Correct 4543 ms 268520 KB Output is correct
52 Correct 4525 ms 268524 KB Output is correct
53 Correct 4660 ms 268552 KB Output is correct
54 Correct 4284 ms 268664 KB Output is correct
55 Correct 4808 ms 268524 KB Output is correct
56 Correct 4335 ms 268708 KB Output is correct
57 Correct 4403 ms 268656 KB Output is correct
58 Correct 4271 ms 268572 KB Output is correct
59 Correct 3661 ms 245988 KB Output is correct
60 Correct 3750 ms 245944 KB Output is correct
61 Correct 3576 ms 245852 KB Output is correct
62 Correct 3475 ms 235064 KB Output is correct
63 Correct 3406 ms 235032 KB Output is correct
64 Correct 3664 ms 235068 KB Output is correct
65 Correct 3492 ms 224216 KB Output is correct
66 Correct 3377 ms 224176 KB Output is correct
67 Correct 3329 ms 224212 KB Output is correct