Submission #539292

# Submission time Handle Problem Language Result Execution time Memory
539292 2022-03-18T16:16:07 Z LucaDantas Bubble Sort 2 (JOI18_bubblesort2) C++17
60 / 100
9000 ms 101036 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"
using namespace std;

constexpr int maxn = 1<<20, B = 1500, inf = 0x3f3f3f3f;

struct SegmentTree {
	int tree[maxn], lazy[maxn], a[maxn];
	void flush(int node, int i, int j) {
		if(!lazy[node]) return;
		if(i != j) {
			lazy[node<<1] += lazy[node];
			lazy[node<<1|1] += lazy[node];
		}
		tree[node] += lazy[node];
		lazy[node] = 0;
	}
	void build(int node, int i, int j) {
		lazy[node] = 0;
		if(i == j) return (void)(tree[node] = a[i]);
		int m = (i+j) >> 1;
		build(node<<1, i, m);
		build(node<<1|1, m+1, j);
		tree[node] = max(tree[node<<1], tree[node<<1|1]);
	}
	void upd(int node, int i, int j, int l, int r, int v) {
		flush(node, i, j);
		if(i > r || j < l) return;
		if(i >= l && j <= r) {
			lazy[node] += v;
			flush(node, i, j);
			return;
		}
		int m = (i+j) >> 1;
		upd(node<<1, i, m, l, r, v);
		upd(node<<1|1, m+1, j, l, r, v);
		tree[node] = max(tree[node<<1], tree[node<<1|1]);
	}
	int query() { return tree[1] + lazy[1]; } // quero o maximo geral mesmo
} seg;

void compress(vector<int>& A, vector<int>& V) {
	map<int,int> mp;
	for(int x : A)
		mp[x] = 0;
	for(int x : V)
		mp[x] = 0;
 
	int coord = 0;
	for(auto& it : mp)
		it.second = ++coord;
 
	for(int& x : A)
		x = mp[x];
	for(int& x : V)
		x = mp[x];
}

struct BIT {
	int bit[maxn];
	void upd(int x, int v) {
		for(; x > 0; x -= x&-x)
			bit[x] += v;
	}
	int query(int x) {
		int ans = 0;
		for(; x < maxn; x += x&-x)
			ans += bit[x];
		return ans;
	}
	void clear() { memset(bit, 0, sizeof bit); }
} bit, bit2;
 
vector<int> pareto;

int DENTRO_NA_QUERY[maxn]; bool foi[maxn];
vector<pair<int,int>> qr[maxn];
int FORA[maxn], DENTRO[maxn];

void rebuild(const vector<int>& A) {
	pareto.clear();
	int N = (int)(A.size());
	for(int i = 0; i < N; i++) {
		if(foi[i]) continue;
		while(pareto.size() && A[i] <= A[pareto.back()])
			pareto.pop_back();
		pareto.push_back(i);
	}

	bit.clear(); bit2.clear();
	for(int i = 0; i < (int)A.size(); i++) {
		while(qr[i].size()) {
			auto [val, id] = qr[i].back();
			qr[i].pop_back();
			DENTRO_NA_QUERY[id] = bit2.query(val+1);
		}

		DENTRO[i] = bit2.query(A[i]+1);

		if(!foi[i]) seg.a[i] = bit.query(A[i]+1);
		else seg.a[i] = -inf;

		bit.upd(A[i], 1);
		if(!foi[i])
			bit2.upd(A[i], 1);
	}
	seg.build(1, 0, N-1);
}

int bs(int x, const vector<int>& A) {
	int l = 0, r = (int)(pareto.size())-1, ans = -1;
	while(l <= r) {
		int m = (l+r) >> 1;
		if(A[pareto[m]] < x)
			ans = pareto[m], l = m+1;
		else
			r = m-1;
	}
	return ans;
}

std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
	int Q = (int)X.size(), N = (int)A.size();
	compress(A, V);

	std::vector<int> answer(Q);
	for(int q = 0; q < Q; q += B) {
		vector<int> special;

		for(int add = 0; add < B && add+q < Q; add++) {
			qr[X[q+add]].push_back({V[q+add], q+add});
			if(foi[X[q+add]]) continue;
			foi[X[q+add]] = 1;
			special.push_back(X[q+add]);
		}

		rebuild(A);

		for(int x : special)
			FORA[x] = 0;
 
		sort(special.begin(), special.end());
		bit.clear();
		for(int x : special) {
			FORA[x] = bit.query(A[x]+1);
			bit.upd(A[x], 1);
		}
	
		/* for(int x : special)
			for(int y : special)
				if(x < y && A[x] > A[y]) ++FORA[y]; */

		for(int add = 0; add < B && add+q < Q; add++) {
			int pos = X[q+add], val = V[q+add];

			{
				int last = bs(A[pos], A);
				if(last > pos)
					seg.upd(1, 0, N-1, pos, last, -1);

				for(int x : special) {
					if(pos < x && A[pos] > A[x]) --FORA[x];
					if(x < pos && A[x] > A[pos]) --FORA[pos];
				}
			}

			A[pos] = val;
			DENTRO[pos] = DENTRO_NA_QUERY[q+add];

			{
				int last = bs(A[pos], A);
				if(last > pos)
					seg.upd(1, 0, N-1, pos, last, 1);

				for(int x : special) {
					if(pos < x && A[pos] > A[x]) ++FORA[x];
					if(x < pos && A[x] > A[pos]) ++FORA[pos];
				}
			}

			int especiais = 0;
			for(int x : special)
				especiais = max(especiais, DENTRO[x] + FORA[x]);

			answer[q+add] = max(seg.query(), especiais);
		}

		for(int x : special)
			foi[x] = 0;
	}
	return answer;
}

# Verdict Execution time Memory Grader output
1 Correct 24 ms 33236 KB Output is correct
2 Correct 32 ms 33232 KB Output is correct
3 Correct 49 ms 33516 KB Output is correct
4 Correct 42 ms 33508 KB Output is correct
5 Correct 40 ms 33492 KB Output is correct
6 Correct 27 ms 33440 KB Output is correct
7 Correct 39 ms 33428 KB Output is correct
8 Correct 35 ms 33512 KB Output is correct
9 Correct 40 ms 33504 KB Output is correct
10 Correct 34 ms 33488 KB Output is correct
11 Correct 32 ms 33492 KB Output is correct
12 Correct 32 ms 33484 KB Output is correct
13 Correct 30 ms 33476 KB Output is correct
14 Correct 29 ms 33436 KB Output is correct
15 Correct 31 ms 33492 KB Output is correct
16 Correct 27 ms 33364 KB Output is correct
17 Correct 30 ms 33476 KB Output is correct
18 Correct 26 ms 33392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 33236 KB Output is correct
2 Correct 32 ms 33232 KB Output is correct
3 Correct 49 ms 33516 KB Output is correct
4 Correct 42 ms 33508 KB Output is correct
5 Correct 40 ms 33492 KB Output is correct
6 Correct 27 ms 33440 KB Output is correct
7 Correct 39 ms 33428 KB Output is correct
8 Correct 35 ms 33512 KB Output is correct
9 Correct 40 ms 33504 KB Output is correct
10 Correct 34 ms 33488 KB Output is correct
11 Correct 32 ms 33492 KB Output is correct
12 Correct 32 ms 33484 KB Output is correct
13 Correct 30 ms 33476 KB Output is correct
14 Correct 29 ms 33436 KB Output is correct
15 Correct 31 ms 33492 KB Output is correct
16 Correct 27 ms 33364 KB Output is correct
17 Correct 30 ms 33476 KB Output is correct
18 Correct 26 ms 33392 KB Output is correct
19 Correct 145 ms 34380 KB Output is correct
20 Correct 151 ms 34488 KB Output is correct
21 Correct 120 ms 34492 KB Output is correct
22 Correct 132 ms 34508 KB Output is correct
23 Correct 98 ms 34340 KB Output is correct
24 Correct 103 ms 34396 KB Output is correct
25 Correct 89 ms 34324 KB Output is correct
26 Correct 92 ms 34300 KB Output is correct
27 Correct 63 ms 34272 KB Output is correct
28 Correct 57 ms 34360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 34376 KB Output is correct
2 Correct 546 ms 36300 KB Output is correct
3 Correct 1165 ms 37476 KB Output is correct
4 Correct 1214 ms 37532 KB Output is correct
5 Correct 1002 ms 37524 KB Output is correct
6 Correct 975 ms 37512 KB Output is correct
7 Correct 986 ms 37532 KB Output is correct
8 Correct 991 ms 37512 KB Output is correct
9 Correct 987 ms 37492 KB Output is correct
10 Correct 438 ms 37100 KB Output is correct
11 Correct 435 ms 37136 KB Output is correct
12 Correct 429 ms 37068 KB Output is correct
13 Correct 321 ms 36892 KB Output is correct
14 Correct 308 ms 36900 KB Output is correct
15 Correct 354 ms 36812 KB Output is correct
16 Correct 178 ms 36300 KB Output is correct
17 Correct 204 ms 36316 KB Output is correct
18 Correct 202 ms 36324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 33236 KB Output is correct
2 Correct 32 ms 33232 KB Output is correct
3 Correct 49 ms 33516 KB Output is correct
4 Correct 42 ms 33508 KB Output is correct
5 Correct 40 ms 33492 KB Output is correct
6 Correct 27 ms 33440 KB Output is correct
7 Correct 39 ms 33428 KB Output is correct
8 Correct 35 ms 33512 KB Output is correct
9 Correct 40 ms 33504 KB Output is correct
10 Correct 34 ms 33488 KB Output is correct
11 Correct 32 ms 33492 KB Output is correct
12 Correct 32 ms 33484 KB Output is correct
13 Correct 30 ms 33476 KB Output is correct
14 Correct 29 ms 33436 KB Output is correct
15 Correct 31 ms 33492 KB Output is correct
16 Correct 27 ms 33364 KB Output is correct
17 Correct 30 ms 33476 KB Output is correct
18 Correct 26 ms 33392 KB Output is correct
19 Correct 145 ms 34380 KB Output is correct
20 Correct 151 ms 34488 KB Output is correct
21 Correct 120 ms 34492 KB Output is correct
22 Correct 132 ms 34508 KB Output is correct
23 Correct 98 ms 34340 KB Output is correct
24 Correct 103 ms 34396 KB Output is correct
25 Correct 89 ms 34324 KB Output is correct
26 Correct 92 ms 34300 KB Output is correct
27 Correct 63 ms 34272 KB Output is correct
28 Correct 57 ms 34360 KB Output is correct
29 Correct 60 ms 34376 KB Output is correct
30 Correct 546 ms 36300 KB Output is correct
31 Correct 1165 ms 37476 KB Output is correct
32 Correct 1214 ms 37532 KB Output is correct
33 Correct 1002 ms 37524 KB Output is correct
34 Correct 975 ms 37512 KB Output is correct
35 Correct 986 ms 37532 KB Output is correct
36 Correct 991 ms 37512 KB Output is correct
37 Correct 987 ms 37492 KB Output is correct
38 Correct 438 ms 37100 KB Output is correct
39 Correct 435 ms 37136 KB Output is correct
40 Correct 429 ms 37068 KB Output is correct
41 Correct 321 ms 36892 KB Output is correct
42 Correct 308 ms 36900 KB Output is correct
43 Correct 354 ms 36812 KB Output is correct
44 Correct 178 ms 36300 KB Output is correct
45 Correct 204 ms 36316 KB Output is correct
46 Correct 202 ms 36324 KB Output is correct
47 Correct 4912 ms 57468 KB Output is correct
48 Execution timed out 9014 ms 101036 KB Time limit exceeded
49 Halted 0 ms 0 KB -