Submission #102590

# Submission time Handle Problem Language Result Execution time Memory
102590 2019-03-26T03:37:38 Z DrumpfTheGodEmperor Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
2696 ms 58844 KB
#include <bits/stdc++.h>
#include "bubblesort2.h"
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 5e5 + 5;
int n, q;
struct Query {
	int val, pos, id;
	bool operator<(const Query &rhs) const {
		if (val == rhs.val) {
			if (pos == rhs.pos) return id < rhs.id;
			return pos < rhs.pos;
		}
		return val < rhs.val;
	}
};
vector<Query> vec;
class SegTree {
private:
	int mx[N << 3], cnt[N << 3], lazy[N << 3];
	//cnt saves if a query is "active"
	void build(int l, int r, int x) {
		if (l == r) {
			cnt[x] = 0;
			mx[x] = -inf;
			lazy[x] = 0;
			return;
		}
		int m = (l + r) >> 1;
		build(l, m, x << 1), build(m + 1, r, x << 1 | 1);
		cnt[x] = 0, mx[x] = -inf, lazy[x] = 0;
	}
	void push(int l, int r, int x) {
		mx[x] += lazy[x];
		if (l != r) {
			lazy[x << 1] += lazy[x];
			lazy[x << 1 | 1] += lazy[x];
		}
		lazy[x] = 0;
	}
	void add(int l, int r, int s, int e, int x, int val) {
		push(l, r, x);
		if (l > e || r < s) return;
		if (s <= l && r <= e) {
			lazy[x] += val;
			push(l, r, x);
			return;
		}
		int m = (l + r) >> 1;
		add(l, m, s, e, x << 1, val), add(m + 1, r, s, e, x << 1 | 1, val);
		mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
	}
	void set(int l, int r, int pos, int mxval, int cntval, int x) {
		push(l, r, x);
		if (l > pos || r < pos) return;
		if (l == r) {
			mx[x] = mxval;
			cnt[x] = cntval;
			return;
		}
		int m = (l + r) >> 1;
		set(l, m, pos, mxval, cntval, x << 1), set(m + 1, r, pos, mxval, cntval, x << 1 | 1);
		cnt[x] = cnt[x << 1] + cnt[x << 1 | 1];
		mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
	}
	int get(int l, int r, int s, int e, int x) {
		push(l, r, x);
		if (l > e || r < s) return 0;
		if (s <= l && r <= e) return cnt[x];
		int m = (l + r) >> 1;
		return get(l, m, s, e, x << 1) + get(m + 1, r, s, e, x << 1 | 1);
	}
public:
	int n;
	void init(int _n) { n = _n; build(0, n - 1, 1); }
	void add(int l, int r, int val) { add(0, n - 1, l, r, 1, val); }
	int get(int l, int r) { return get(0, n - 1, l, r, 1); }
	void set(int pos, int mxval, int cntval) { set(0, n - 1, pos, mxval, cntval, 1); }
	int getans() { return mx[1]; }
} st;
int curid[N], realpos[N];
vector<int> countScans(vector<int> a, vector<int> x, vector<int> v) {
	n = a.size(), q = x.size();
	fw (i, 0, n) vec.pb({a[i], i, i});
	fw (i, 0, q) vec.pb({v[i], x[i], i + n});
	//Save segment tree of queries and the answer for the modified index within the query
	sort(vec.begin(), vec.end());
	st.init(n + q);
	fw (i, 0, vec.size()) {
		int pos = vec[i].pos, id = vec[i].id;
		if (id >= n) realpos[id - n] = i;
		else {
			int tmp = pos - st.get(0, i);
			curid[pos] = i;
			st.set(i, tmp, 1);
			//Don't need to lazy since queries are processed in order here
		}
	}
	vector<int> res;
	fw (i, 0, q) {
		st.add(curid[x[i]], n + q - 1, 1);
		st.set(curid[x[i]], -inf, 0);
		curid[x[i]] = realpos[i];
		int tmp = x[i] - st.get(0, realpos[i]);
		st.add(curid[x[i]], n + q - 1, -1);
		st.set(curid[x[i]], tmp, 1);
		res.pb(st.getans());
	}
	return res;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
bubblesort2.cpp:100:6:
  fw (i, 0, vec.size()) {
      ~~~~~~~~~~~~~~~~                  
bubblesort2.cpp:100:2: note: in expansion of macro 'fw'
  fw (i, 0, vec.size()) {
  ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 7 ms 640 KB Output is correct
5 Correct 8 ms 640 KB Output is correct
6 Correct 7 ms 640 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 9 ms 652 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 7 ms 640 KB Output is correct
17 Correct 8 ms 612 KB Output is correct
18 Correct 8 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 7 ms 640 KB Output is correct
5 Correct 8 ms 640 KB Output is correct
6 Correct 7 ms 640 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 9 ms 652 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 7 ms 640 KB Output is correct
17 Correct 8 ms 612 KB Output is correct
18 Correct 8 ms 640 KB Output is correct
19 Correct 19 ms 1216 KB Output is correct
20 Correct 22 ms 1388 KB Output is correct
21 Correct 22 ms 1464 KB Output is correct
22 Correct 24 ms 1336 KB Output is correct
23 Correct 20 ms 1344 KB Output is correct
24 Correct 27 ms 1464 KB Output is correct
25 Correct 24 ms 1240 KB Output is correct
26 Correct 23 ms 1344 KB Output is correct
27 Correct 19 ms 1344 KB Output is correct
28 Correct 22 ms 1344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 1916 KB Output is correct
2 Correct 88 ms 4112 KB Output is correct
3 Correct 233 ms 7340 KB Output is correct
4 Correct 187 ms 7404 KB Output is correct
5 Correct 182 ms 7164 KB Output is correct
6 Correct 174 ms 7352 KB Output is correct
7 Correct 146 ms 7172 KB Output is correct
8 Correct 174 ms 7276 KB Output is correct
9 Correct 216 ms 7188 KB Output is correct
10 Correct 140 ms 7276 KB Output is correct
11 Correct 179 ms 7248 KB Output is correct
12 Correct 138 ms 7248 KB Output is correct
13 Correct 145 ms 7288 KB Output is correct
14 Correct 133 ms 7264 KB Output is correct
15 Correct 135 ms 7352 KB Output is correct
16 Correct 116 ms 7276 KB Output is correct
17 Correct 161 ms 7404 KB Output is correct
18 Correct 128 ms 7292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 7 ms 640 KB Output is correct
5 Correct 8 ms 640 KB Output is correct
6 Correct 7 ms 640 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 9 ms 652 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 7 ms 640 KB Output is correct
17 Correct 8 ms 612 KB Output is correct
18 Correct 8 ms 640 KB Output is correct
19 Correct 19 ms 1216 KB Output is correct
20 Correct 22 ms 1388 KB Output is correct
21 Correct 22 ms 1464 KB Output is correct
22 Correct 24 ms 1336 KB Output is correct
23 Correct 20 ms 1344 KB Output is correct
24 Correct 27 ms 1464 KB Output is correct
25 Correct 24 ms 1240 KB Output is correct
26 Correct 23 ms 1344 KB Output is correct
27 Correct 19 ms 1344 KB Output is correct
28 Correct 22 ms 1344 KB Output is correct
29 Correct 23 ms 1916 KB Output is correct
30 Correct 88 ms 4112 KB Output is correct
31 Correct 233 ms 7340 KB Output is correct
32 Correct 187 ms 7404 KB Output is correct
33 Correct 182 ms 7164 KB Output is correct
34 Correct 174 ms 7352 KB Output is correct
35 Correct 146 ms 7172 KB Output is correct
36 Correct 174 ms 7276 KB Output is correct
37 Correct 216 ms 7188 KB Output is correct
38 Correct 140 ms 7276 KB Output is correct
39 Correct 179 ms 7248 KB Output is correct
40 Correct 138 ms 7248 KB Output is correct
41 Correct 145 ms 7288 KB Output is correct
42 Correct 133 ms 7264 KB Output is correct
43 Correct 135 ms 7352 KB Output is correct
44 Correct 116 ms 7276 KB Output is correct
45 Correct 161 ms 7404 KB Output is correct
46 Correct 128 ms 7292 KB Output is correct
47 Correct 707 ms 23580 KB Output is correct
48 Correct 2094 ms 55876 KB Output is correct
49 Correct 2298 ms 58580 KB Output is correct
50 Correct 2696 ms 58768 KB Output is correct
51 Correct 2654 ms 58576 KB Output is correct
52 Correct 2593 ms 58844 KB Output is correct
53 Correct 2624 ms 58824 KB Output is correct
54 Correct 2529 ms 58700 KB Output is correct
55 Correct 2619 ms 58572 KB Output is correct
56 Correct 2452 ms 58788 KB Output is correct
57 Correct 2535 ms 58828 KB Output is correct
58 Correct 2413 ms 58556 KB Output is correct
59 Correct 2180 ms 58700 KB Output is correct
60 Correct 2204 ms 58664 KB Output is correct
61 Correct 2170 ms 58700 KB Output is correct
62 Correct 1988 ms 58700 KB Output is correct
63 Correct 1927 ms 58716 KB Output is correct
64 Correct 1970 ms 58700 KB Output is correct
65 Correct 1839 ms 58676 KB Output is correct
66 Correct 1788 ms 58524 KB Output is correct
67 Correct 1897 ms 58584 KB Output is correct