Submission #126569

# Submission time Handle Problem Language Result Execution time Memory
126569 2019-07-08T06:04:19 Z E869120 Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
5755 ms 57400 KB
#include "bubblesort2.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class RangeAddMaxSegmentTree {
public:
	vector<int> dat, lazy; int size_ = 1;

	void output() {
		cout << "dat : " << endl;
		for (int i = 1; i <= size_; i *= 2) {
			for (int j = i; j < i * 2; j++) {
				for (int k = 0; k < 5 * ((size_ / i) - 1); k++) cout << " ";
				cout << "[";
				if (dat[j] >= 0 && dat[j] <= 9) cout << "  " << dat[j];
				else if (dat[j] >= -9 && dat[j] <= 99) cout << " " << dat[j];
				else cout << dat[j];
				cout << "]";
				for (int k = 0; k < 5 * ((size_ / i) - 1); k++) cout << " ";
				cout << "     ";
			}
			cout << endl;
		}
		cout << endl;
		cout << "lazy : " << endl;
		for (int i = 1; i <= size_; i *= 2) {
			for (int j = i; j < i * 2; j++) {
				for (int k = 0; k < 5 * ((size_ / i) - 1); k++) cout << " ";
				cout << "[";
				if (lazy[j] >= 0 && lazy[j] <= 9) cout << "  " << lazy[j];
				else if (lazy[j] >= -9 && lazy[j] <= 99) cout << " " << lazy[j];
				else cout << lazy[j];
				cout << "]";
				for (int k = 0; k < 5 * ((size_ / i) - 1); k++) cout << " ";
				cout << "     ";
			}
			cout << endl;
		}
	}
	void init(int sz) {
		while (size_ <= sz) size_ *= 2;
		dat.resize(size_ * 2, 0);
		lazy.resize(size_ * 2, 0);
	}
	void push(int pos) {
		if (pos < size_) {
			lazy[pos * 2 + 0] += lazy[pos];
			lazy[pos * 2 + 1] += lazy[pos];
			dat[pos] = max(dat[pos * 2] + lazy[pos * 2], dat[pos * 2 + 1] + lazy[pos * 2 + 1]);
			lazy[pos] = 0;
		}
		else {
			dat[pos] += lazy[pos];
			lazy[pos] = 0;
		}
	}
	void add_(int l, int r, int x, int a, int b, int u) {
		push(u);
		if (l <= a && b <= r) {
			lazy[u] += x;
			push(u);
			return;
		}
		if (r <= a || b <= l) return;

		add_(l, r, x, a, (a + b) >> 1, u * 2);
		add_(l, r, x, (a + b) >> 1, b, u * 2 + 1);
		push(u);
	}
	void add(int l, int r, int x) {
		add_(l, r, x, 0, size_, 1);
	}
	int query_(int l, int r, int a, int b, int u) {
		push(u);
		if (l <= a && b <= r) return dat[u];
		if (r <= a || b <= l) return -(1 << 30);

		int v1 = query_(l, r, a, (a + b) >> 1, u * 2);
		int v2 = query_(l, r, (a + b) >> 1, b, u * 2 + 1);
		return max(v1, v2);
	}
	int query(int l, int r) {
		return query_(l, r, 0, size_, 1);
	}
};

class RangeAddMaxSegmentTree2 {
public:
	vector<int> dat;

	void output() {
		for (int i = 0; i < dat.size(); i++) printf("% 4d", dat[i]); cout << endl;
	}
	void init(int sz) {
		dat.resize(sz + 2, 0);
	}
	void add(int l, int r, int x) {
		for (int i = l; i < r; i++) dat[i] += x;
	}
	int query(int l, int r) {
		int maxn = -(1 << 30);
		for (int i = l; i < r; i++) maxn = max(maxn, dat[i]);
		return maxn;
	}
};

class BIT {
public:
	vector<int> bit; int size_ = 1;

	void init(int sz) {
		size_ = sz + 2;
		bit.resize(size_ + 2, 0);
	}
	void add(int pos, int x) {
		pos++;
		while (pos <= size_) {
			bit[pos] += x;
			pos += (pos&-pos);
		}
	}
	int sum(int pos) {
		pos++; int s = 0;
		while (pos >= 1) {
			s += bit[pos];
			pos -= (pos&-pos);
		}
		return s;
	}
};

const int INF = 9;
int N, Q; vector<pair<int, int>> G;
RangeAddMaxSegmentTree Z; BIT Y;

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
	N = A.size(); Q = X.size();
	for (int i = 0; i < N; i++) G.push_back(make_pair(A[i], i));
	for (int i = 0; i < Q; i++) G.push_back(make_pair(V[i], X[i]));
	sort(G.begin(), G.end());

	Z.init(G.size() + 2); Y.init(G.size() + 2);
	for (int i = 0; i < G.size(); i++) {
		Z.add(i, i + 1, -INF - Z.query(i, i + 1));
	}
	for (int i = 0; i < N; i++) {
		int pos1 = lower_bound(G.begin(), G.end(), make_pair(A[i], i)) - G.begin();
		Y.add(pos1, 1);
	}
	for (int i = 0; i < N; i++) {
		int pos1 = lower_bound(G.begin(), G.end(), make_pair(A[i], i)) - G.begin();
		int val = i - Y.sum(pos1 - 1);
		Z.add(pos1, pos1 + 1, val - Z.query(pos1, pos1 + 1));
	}
	//Z.output();

	vector<int>ans;

	for (int i = 0; i < Q; i++) {
		int pos1 = lower_bound(G.begin(), G.end(), make_pair(A[X[i]], X[i])) - G.begin();
		int pos2 = lower_bound(G.begin(), G.end(), make_pair(V[i], X[i])) - G.begin();
		Z.add(pos1, pos1 + 1, -INF - Z.query(pos1, pos1 + 1)); Y.add(pos1, -1);
		int val = X[i] - Y.sum(pos2 - 1);
		Z.add(pos2, pos2 + 1, val - Z.query(pos2, pos2 + 1)); Y.add(pos2, 1);

		//Z.output();

		if (pos1 < pos2) Z.add(pos1 + 1, pos2, 1);
		else Z.add(pos2 + 1, pos1, -1);
		//Z.output();
		ans.push_back(Z.query(0, G.size() + 1));
		A[X[i]] = V[i];
	}
	return ans;
}

Compilation message

bubblesort2.cpp: In member function 'void RangeAddMaxSegmentTree2::output()':
bubblesort2.cpp:94:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < dat.size(); i++) printf("% 4d", dat[i]); cout << endl;
                   ~~^~~~~~~~~~~~
bubblesort2.cpp:94:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (int i = 0; i < dat.size(); i++) printf("% 4d", dat[i]); cout << endl;
   ^~~
bubblesort2.cpp:94:64: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (int i = 0; i < dat.size(); i++) printf("% 4d", dat[i]); cout << endl;
                                                                ^~~~
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:145:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G.size(); i++) {
                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 13 ms 504 KB Output is correct
4 Correct 13 ms 504 KB Output is correct
5 Correct 13 ms 516 KB Output is correct
6 Correct 13 ms 504 KB Output is correct
7 Correct 13 ms 504 KB Output is correct
8 Correct 13 ms 552 KB Output is correct
9 Correct 13 ms 504 KB Output is correct
10 Correct 13 ms 504 KB Output is correct
11 Correct 13 ms 564 KB Output is correct
12 Correct 13 ms 504 KB Output is correct
13 Correct 13 ms 504 KB Output is correct
14 Correct 13 ms 504 KB Output is correct
15 Correct 13 ms 504 KB Output is correct
16 Correct 13 ms 504 KB Output is correct
17 Correct 13 ms 632 KB Output is correct
18 Correct 13 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 13 ms 504 KB Output is correct
4 Correct 13 ms 504 KB Output is correct
5 Correct 13 ms 516 KB Output is correct
6 Correct 13 ms 504 KB Output is correct
7 Correct 13 ms 504 KB Output is correct
8 Correct 13 ms 552 KB Output is correct
9 Correct 13 ms 504 KB Output is correct
10 Correct 13 ms 504 KB Output is correct
11 Correct 13 ms 564 KB Output is correct
12 Correct 13 ms 504 KB Output is correct
13 Correct 13 ms 504 KB Output is correct
14 Correct 13 ms 504 KB Output is correct
15 Correct 13 ms 504 KB Output is correct
16 Correct 13 ms 504 KB Output is correct
17 Correct 13 ms 632 KB Output is correct
18 Correct 13 ms 504 KB Output is correct
19 Correct 47 ms 1024 KB Output is correct
20 Correct 56 ms 1016 KB Output is correct
21 Correct 53 ms 1016 KB Output is correct
22 Correct 54 ms 1016 KB Output is correct
23 Correct 52 ms 1016 KB Output is correct
24 Correct 53 ms 1056 KB Output is correct
25 Correct 52 ms 1016 KB Output is correct
26 Correct 52 ms 1116 KB Output is correct
27 Correct 53 ms 1016 KB Output is correct
28 Correct 52 ms 1052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 1500 KB Output is correct
2 Correct 224 ms 3068 KB Output is correct
3 Correct 412 ms 5144 KB Output is correct
4 Correct 413 ms 5228 KB Output is correct
5 Correct 403 ms 5164 KB Output is correct
6 Correct 402 ms 5176 KB Output is correct
7 Correct 409 ms 5228 KB Output is correct
8 Correct 400 ms 5740 KB Output is correct
9 Correct 400 ms 5752 KB Output is correct
10 Correct 362 ms 5888 KB Output is correct
11 Correct 361 ms 5972 KB Output is correct
12 Correct 358 ms 5868 KB Output is correct
13 Correct 372 ms 5904 KB Output is correct
14 Correct 364 ms 5812 KB Output is correct
15 Correct 365 ms 5996 KB Output is correct
16 Correct 369 ms 5868 KB Output is correct
17 Correct 370 ms 5784 KB Output is correct
18 Correct 370 ms 5932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 13 ms 504 KB Output is correct
4 Correct 13 ms 504 KB Output is correct
5 Correct 13 ms 516 KB Output is correct
6 Correct 13 ms 504 KB Output is correct
7 Correct 13 ms 504 KB Output is correct
8 Correct 13 ms 552 KB Output is correct
9 Correct 13 ms 504 KB Output is correct
10 Correct 13 ms 504 KB Output is correct
11 Correct 13 ms 564 KB Output is correct
12 Correct 13 ms 504 KB Output is correct
13 Correct 13 ms 504 KB Output is correct
14 Correct 13 ms 504 KB Output is correct
15 Correct 13 ms 504 KB Output is correct
16 Correct 13 ms 504 KB Output is correct
17 Correct 13 ms 632 KB Output is correct
18 Correct 13 ms 504 KB Output is correct
19 Correct 47 ms 1024 KB Output is correct
20 Correct 56 ms 1016 KB Output is correct
21 Correct 53 ms 1016 KB Output is correct
22 Correct 54 ms 1016 KB Output is correct
23 Correct 52 ms 1016 KB Output is correct
24 Correct 53 ms 1056 KB Output is correct
25 Correct 52 ms 1016 KB Output is correct
26 Correct 52 ms 1116 KB Output is correct
27 Correct 53 ms 1016 KB Output is correct
28 Correct 52 ms 1052 KB Output is correct
29 Correct 73 ms 1500 KB Output is correct
30 Correct 224 ms 3068 KB Output is correct
31 Correct 412 ms 5144 KB Output is correct
32 Correct 413 ms 5228 KB Output is correct
33 Correct 403 ms 5164 KB Output is correct
34 Correct 402 ms 5176 KB Output is correct
35 Correct 409 ms 5228 KB Output is correct
36 Correct 400 ms 5740 KB Output is correct
37 Correct 400 ms 5752 KB Output is correct
38 Correct 362 ms 5888 KB Output is correct
39 Correct 361 ms 5972 KB Output is correct
40 Correct 358 ms 5868 KB Output is correct
41 Correct 372 ms 5904 KB Output is correct
42 Correct 364 ms 5812 KB Output is correct
43 Correct 365 ms 5996 KB Output is correct
44 Correct 369 ms 5868 KB Output is correct
45 Correct 370 ms 5784 KB Output is correct
46 Correct 370 ms 5932 KB Output is correct
47 Correct 1441 ms 20752 KB Output is correct
48 Correct 5252 ms 54348 KB Output is correct
49 Correct 5709 ms 57300 KB Output is correct
50 Correct 5688 ms 57184 KB Output is correct
51 Correct 5755 ms 57120 KB Output is correct
52 Correct 5752 ms 57296 KB Output is correct
53 Correct 5671 ms 57256 KB Output is correct
54 Correct 5438 ms 57200 KB Output is correct
55 Correct 5572 ms 57008 KB Output is correct
56 Correct 5409 ms 57300 KB Output is correct
57 Correct 5515 ms 57400 KB Output is correct
58 Correct 5320 ms 57300 KB Output is correct
59 Correct 5164 ms 56032 KB Output is correct
60 Correct 5196 ms 56000 KB Output is correct
61 Correct 5231 ms 56044 KB Output is correct
62 Correct 5133 ms 55944 KB Output is correct
63 Correct 5081 ms 56000 KB Output is correct
64 Correct 5067 ms 55796 KB Output is correct
65 Correct 4994 ms 55792 KB Output is correct
66 Correct 4988 ms 55852 KB Output is correct
67 Correct 5011 ms 55880 KB Output is correct