Submission #520048

# Submission time Handle Problem Language Result Execution time Memory
520048 2022-01-28T08:29:06 Z akhan42 Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
4685 ms 183788 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }


const int INF = 1000 * 1000;


struct Seg {
	int n;
	vi add, mx;
	const int INF = 1000 * 1000 * 1000;

	Seg(int n, vi &vals): n(n) {
		add.assign(4 * n, 0);
		mx.assign(4 * n, 0);

		build(vals);
	}

	void propagate(int v, int tl, int tr) {
		if(tl == tr || add[v] == 0)
			return;

		add[2 * v] += add[v];
		add[2 * v + 1] += add[v];
		mx[2 * v] += add[v];
		mx[2 * v + 1] += add[v];
		add[v] = 0;
	}

	void pull(int v) {
		mx[v] = max(mx[2 * v], mx[2 * v + 1]);
	}

	void build(vi &vals, int v = 1, int tl = 0, int tr = -1) {
		if(tr == -1) tr = n - 1;

		if(tl == tr) {
			mx[v] = vals[tl];
			return;
		}
		int tm = (tl + tr) / 2;

		build(vals, 2 * v, tl, tm);
		build(vals, 2 * v + 1, tm + 1, tr);

		pull(v);
	}

	void update(int l, int r, int val, int v = 1, int tl = 0, int tr = -1) {
		if(tr == -1) tr = n - 1;
		propagate(v, tl, tr);
		if(l > r) return;

		if(tl == l && r == tr) {
			add[v] = val;
			mx[v] += val;
			return;
		}
		int tm = (tl + tr) / 2;

		update(l, min(r, tm), val, 2 * v, tl, tm);
		update(max(l, tm + 1), r, val, 2 * v + 1, tm + 1, tr);

		pull(v);
	}

	int query(int l, int r, int v = 1, int tl = 0, int tr = -1) {
		if(tr == -1) tr = n - 1;
		propagate(v, tl, tr);
		if(l > r) return -INF;

		if(tl == l && r == tr)
			return mx[v];
		int tm = (tl + tr) / 2;

		int ql = query(l, min(r, tm), 2 * v, tl, tm);
		int qr = query(max(l, tm + 1), r, 2 * v + 1, tm + 1, tr);

		return max(ql, qr);
	}

	int get_max() {
		return query(0, n - 1);
	}

	int at(int p) {
		return query(p, p);
	}

	void set(int p, int nval) {
		update(p, p, nval - at(p));
	}
};


struct Seg2 {
	int n;
	vi t;

	Seg2(int n): n(n) {
		t.resize(2 * n);
	}

	void set(int p, int val) {
		for(t[p += n] = val; p > 1; p /= 2)
			t[p >> 1] = t[p] + t[p ^ 1];
	}

	int sum(int l, int r) {
		int res = 0;
		for(l += n, r += n + 1; l < r; l /= 2, r /= 2) {
			if(l & 1) res += t[l++];
			if(r & 1) res += t[--r];
		}
		return res;
	}

	int at(int p) {
		return sum(0, p) - sum(0, p - 1);
	}
};


struct SegSim {
	int n;
	vi t;

	SegSim(int n, vi &vals): n(n) {
		t.resize(n);
		forn(i, n)
			t[i] = vals[i];
	}

	void update(int l, int r, int val) {
		forbn(i, l, r + 1)
			t[i] += val;
	}

	void set(int p, int val) {
		t[p] = val;
	}

	int get_max() {
		int mx = -1000 * 1000 * 1000;
		forn(i, n)
			maxeq(mx, t[i]);
		return mx;
	}
};


vi countScans(vi a, vi xs, vi vs) {
	int n = sz(a);
	int q = sz(xs);

	set<ii> s;
	vii vals;

	forn(i, n) {
		s.insert({a[i], i});
		vals.eb(a[i], i);
	}
	forn(i, q) {
		s.insert({vs[i], xs[i]});
	}
	map<ii, int> p2ind;
	int ct = 0;
	for(ii el: s) {
		p2ind[el] = ct++;
	}

	int N = sz(s);

	sort(all(vals));

	vi seg_vals(N, -INF);
	Seg2 sl(N);

	forn(i, n) {
		int ind = p2ind[vals[i]];
		seg_vals[ind] = vals[i].S - i;
		sl.set(ind, 1);
	}

	Seg seg(N, seg_vals);
	vi ans(q);

	forn(i, q) {
		int x = xs[i], v = vs[i];

		int ind_fr = p2ind[{a[x], x}];
		int ind_to = p2ind[{v, x}];

		sl.set(ind_fr, 0);
		sl.set(ind_to, 1);

		if(ind_to > ind_fr) {
			seg.update(ind_fr, ind_to, 1);
		}
		if(ind_to < ind_fr) {
			seg.update(ind_to, ind_fr, -1);
		}

		if(ind_to != ind_fr) {
			int rem = x - sl.sum(0, ind_to - 1);
			seg.set(ind_fr, -INF);
			seg.set(ind_to, rem);
		}

		a[x] = v;
		ans[i] = seg.get_max();
	}

	return ans;
}


void solve() {
	int n, q;
	cin >> n >> q;
	vi a(n);
	forn(i, n)
		cin >> a[i];

	vi xs, vs;
	forn(_, q) {
		int x, v;
		cin >> x >> v;
		xs.pb(x);
		vs.pb(v);
	}

	vi ans = countScans(a, xs, vs);

	for(int a: ans)
		cout << a << endl;

}

//
//
//int main() {
//	ios_base::sync_with_stdio(false);
//	cin.tie(nullptr);
////	cout.tie(nullptr);
//
////	freopen("optmilk.in", "r", stdin);
////	freopen("optmilk.out", "w", stdout);
//
//	int t = 1;
////	cin >> t;
//	while(t--) {
//		solve();
//	}
//}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 588 KB Output is correct
2 Correct 3 ms 588 KB Output is correct
3 Correct 6 ms 972 KB Output is correct
4 Correct 6 ms 944 KB Output is correct
5 Correct 7 ms 984 KB Output is correct
6 Correct 6 ms 1008 KB Output is correct
7 Correct 6 ms 972 KB Output is correct
8 Correct 6 ms 972 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 844 KB Output is correct
11 Correct 6 ms 924 KB Output is correct
12 Correct 6 ms 844 KB Output is correct
13 Correct 5 ms 844 KB Output is correct
14 Correct 5 ms 844 KB Output is correct
15 Correct 6 ms 888 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 5 ms 844 KB Output is correct
18 Correct 5 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 588 KB Output is correct
2 Correct 3 ms 588 KB Output is correct
3 Correct 6 ms 972 KB Output is correct
4 Correct 6 ms 944 KB Output is correct
5 Correct 7 ms 984 KB Output is correct
6 Correct 6 ms 1008 KB Output is correct
7 Correct 6 ms 972 KB Output is correct
8 Correct 6 ms 972 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 844 KB Output is correct
11 Correct 6 ms 924 KB Output is correct
12 Correct 6 ms 844 KB Output is correct
13 Correct 5 ms 844 KB Output is correct
14 Correct 5 ms 844 KB Output is correct
15 Correct 6 ms 888 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 5 ms 844 KB Output is correct
18 Correct 5 ms 844 KB Output is correct
19 Correct 25 ms 2732 KB Output is correct
20 Correct 31 ms 3276 KB Output is correct
21 Correct 35 ms 3148 KB Output is correct
22 Correct 28 ms 3148 KB Output is correct
23 Correct 26 ms 2744 KB Output is correct
24 Correct 25 ms 2764 KB Output is correct
25 Correct 25 ms 2668 KB Output is correct
26 Correct 25 ms 2636 KB Output is correct
27 Correct 25 ms 2492 KB Output is correct
28 Correct 24 ms 2508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 5340 KB Output is correct
2 Correct 118 ms 11264 KB Output is correct
3 Correct 246 ms 17484 KB Output is correct
4 Correct 250 ms 17472 KB Output is correct
5 Correct 276 ms 17236 KB Output is correct
6 Correct 236 ms 17080 KB Output is correct
7 Correct 248 ms 17088 KB Output is correct
8 Correct 237 ms 17160 KB Output is correct
9 Correct 233 ms 17084 KB Output is correct
10 Correct 128 ms 10684 KB Output is correct
11 Correct 116 ms 10572 KB Output is correct
12 Correct 117 ms 10492 KB Output is correct
13 Correct 129 ms 10656 KB Output is correct
14 Correct 173 ms 10596 KB Output is correct
15 Correct 131 ms 10620 KB Output is correct
16 Correct 141 ms 10556 KB Output is correct
17 Correct 144 ms 10680 KB Output is correct
18 Correct 146 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 588 KB Output is correct
2 Correct 3 ms 588 KB Output is correct
3 Correct 6 ms 972 KB Output is correct
4 Correct 6 ms 944 KB Output is correct
5 Correct 7 ms 984 KB Output is correct
6 Correct 6 ms 1008 KB Output is correct
7 Correct 6 ms 972 KB Output is correct
8 Correct 6 ms 972 KB Output is correct
9 Correct 6 ms 972 KB Output is correct
10 Correct 6 ms 844 KB Output is correct
11 Correct 6 ms 924 KB Output is correct
12 Correct 6 ms 844 KB Output is correct
13 Correct 5 ms 844 KB Output is correct
14 Correct 5 ms 844 KB Output is correct
15 Correct 6 ms 888 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 5 ms 844 KB Output is correct
18 Correct 5 ms 844 KB Output is correct
19 Correct 25 ms 2732 KB Output is correct
20 Correct 31 ms 3276 KB Output is correct
21 Correct 35 ms 3148 KB Output is correct
22 Correct 28 ms 3148 KB Output is correct
23 Correct 26 ms 2744 KB Output is correct
24 Correct 25 ms 2764 KB Output is correct
25 Correct 25 ms 2668 KB Output is correct
26 Correct 25 ms 2636 KB Output is correct
27 Correct 25 ms 2492 KB Output is correct
28 Correct 24 ms 2508 KB Output is correct
29 Correct 30 ms 5340 KB Output is correct
30 Correct 118 ms 11264 KB Output is correct
31 Correct 246 ms 17484 KB Output is correct
32 Correct 250 ms 17472 KB Output is correct
33 Correct 276 ms 17236 KB Output is correct
34 Correct 236 ms 17080 KB Output is correct
35 Correct 248 ms 17088 KB Output is correct
36 Correct 237 ms 17160 KB Output is correct
37 Correct 233 ms 17084 KB Output is correct
38 Correct 128 ms 10684 KB Output is correct
39 Correct 116 ms 10572 KB Output is correct
40 Correct 117 ms 10492 KB Output is correct
41 Correct 129 ms 10656 KB Output is correct
42 Correct 173 ms 10596 KB Output is correct
43 Correct 131 ms 10620 KB Output is correct
44 Correct 141 ms 10556 KB Output is correct
45 Correct 144 ms 10680 KB Output is correct
46 Correct 146 ms 10732 KB Output is correct
47 Correct 970 ms 51148 KB Output is correct
48 Correct 4362 ms 167864 KB Output is correct
49 Correct 4601 ms 183592 KB Output is correct
50 Correct 4584 ms 183504 KB Output is correct
51 Correct 4680 ms 183476 KB Output is correct
52 Correct 4671 ms 183460 KB Output is correct
53 Correct 4685 ms 183464 KB Output is correct
54 Correct 4542 ms 183648 KB Output is correct
55 Correct 4637 ms 183648 KB Output is correct
56 Correct 4373 ms 183788 KB Output is correct
57 Correct 4639 ms 183596 KB Output is correct
58 Correct 4304 ms 183640 KB Output is correct
59 Correct 3690 ms 163364 KB Output is correct
60 Correct 3839 ms 163232 KB Output is correct
61 Correct 3726 ms 163140 KB Output is correct
62 Correct 3510 ms 153496 KB Output is correct
63 Correct 3515 ms 153436 KB Output is correct
64 Correct 3484 ms 153624 KB Output is correct
65 Correct 3234 ms 143828 KB Output is correct
66 Correct 3314 ms 143760 KB Output is correct
67 Correct 3282 ms 143740 KB Output is correct