Submission #102100

# Submission time Handle Problem Language Result Execution time Memory
102100 2019-03-22T09:58:36 Z gs14004 Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3153 ms 67400 KB
#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
const int MAXN = 500005;

pi a[MAXN];
int p[2 * MAXN];
int chk[2 * MAXN];

struct seg{
	int tree[2100000], lazy[2100000];
	void init(){
		fill(tree, tree + 2100000, -1e9);
	}
	void lazydown(int p){
		lazy[2*p] += lazy[p];
		lazy[2*p+1] += lazy[p];
		tree[2*p] += lazy[p];
		tree[2*p+1] += lazy[p];
		lazy[p] = 0;
	}
	void add(int s, int e, int ps, int pe, int p, int v){
		if(e < ps || pe < s) return;
		if(s <= ps && pe <= e){
			tree[p] += v;
			lazy[p] += v;
			return;
		}
		int pm = (ps+pe)/2;
		lazydown(p);
		add(s, e, ps, pm, 2*p, v);
		add(s, e, pm+1, pe, 2*p+1, v);
		tree[p] = max(tree[2*p], tree[2*p+1]);
	}
	void upd(int pos, int s, int e, int p, int v){
		if(s == e){
			tree[p] = v;
			return;
		}
		int m = (s+e)/2;
		lazydown(p);
		if(pos <= m) upd(pos, s, m, 2*p, v);
		else upd(pos, m+1, e, 2*p+1, v);
		tree[p] = max(tree[2*p], tree[2*p+1]);
	}
	int query(int pos, int s, int e, int p){
		if(s == e) return tree[p];
		int m = (s+e)/2;
		lazydown(p);
		if(pos <= m) return query(pos, s, m, 2*p);
		else return query(pos, m+1, e, 2*p+1);
	}
}seg;

struct bit{
	int tree[2 * MAXN];
	void add(int x, int v){
		while(x < 2 * MAXN){
			tree[x] += v;
			x += x & -x;
		}
	}
	int query(int x){
		int ret = 0;
		while(x){
			ret += tree[x];
			x -= x & -x;
		}
		return ret;
	}
}bit;

std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
	int n=A.size();
	int Q=X.size();
	std::vector<int> answer(Q);	
	for(int i=0; i<n; i++) a[i] = pi(A[i], i);
	vector<pi> v;
	for(int i=0; i<n; i++) v.push_back(a[i]);
	for(int i=0; i<Q; i++) v.push_back(pi(V[i], X[i]));
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	auto getpos = [&](pi x){
		return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
	};
	seg.init();
	for(int i=0; i<n; i++){
		int pos = getpos(a[i]);
		p[pos] = i;
		chk[pos] = 1;
		bit.add(pos, 1);
	}
	for(int i=1; i<=v.size(); i++){
		if(chk[i]) p[i] -= bit.query(i) - 1;
		else p[i] = -1e9;
		seg.upd(i, 1, v.size(), 1, p[i]);
	}
	for(int i=0; i<Q; i++){
		int curpos = getpos(a[X[i]]);
		a[X[i]].first = V[i];
		int nxtpos = getpos(a[X[i]]);
		if(curpos <= nxtpos){
			int val = seg.query(curpos, 1, v.size(), 1) + bit.query(curpos) - 1;
			bit.add(curpos, -1);
			seg.add(curpos + 1, nxtpos, 1, v.size(), 1, 1);
			bit.add(nxtpos, 1);
			seg.upd(curpos, 1, v.size(), 1, -1e9);
			seg.upd(nxtpos, 1, v.size(), 1, val - bit.query(nxtpos) + 1);
		}
		else{
			int val = seg.query(curpos, 1, v.size(), 1) + bit.query(curpos) - 1;
			bit.add(curpos, -1);
			seg.add(nxtpos + 1, curpos, 1, v.size(), 1, -1);
			bit.add(nxtpos, 1);
			seg.upd(curpos, 1, v.size(), 1, -1e9);
			seg.upd(nxtpos, 1, v.size(), 1, val - bit.query(nxtpos) + 1);
		}
		int ans = max(seg.tree[1], 0);
		answer[i] = ans;
	}
	return answer;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:93:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1; i<=v.size(); i++){
               ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8704 KB Output is correct
2 Correct 10 ms 8704 KB Output is correct
3 Correct 18 ms 8832 KB Output is correct
4 Correct 14 ms 8832 KB Output is correct
5 Correct 14 ms 8832 KB Output is correct
6 Correct 12 ms 8960 KB Output is correct
7 Correct 12 ms 8832 KB Output is correct
8 Correct 13 ms 8832 KB Output is correct
9 Correct 13 ms 8832 KB Output is correct
10 Correct 14 ms 8832 KB Output is correct
11 Correct 12 ms 8832 KB Output is correct
12 Correct 39 ms 8832 KB Output is correct
13 Correct 13 ms 8960 KB Output is correct
14 Correct 13 ms 8960 KB Output is correct
15 Correct 15 ms 8832 KB Output is correct
16 Correct 14 ms 8832 KB Output is correct
17 Correct 16 ms 8832 KB Output is correct
18 Correct 14 ms 8832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8704 KB Output is correct
2 Correct 10 ms 8704 KB Output is correct
3 Correct 18 ms 8832 KB Output is correct
4 Correct 14 ms 8832 KB Output is correct
5 Correct 14 ms 8832 KB Output is correct
6 Correct 12 ms 8960 KB Output is correct
7 Correct 12 ms 8832 KB Output is correct
8 Correct 13 ms 8832 KB Output is correct
9 Correct 13 ms 8832 KB Output is correct
10 Correct 14 ms 8832 KB Output is correct
11 Correct 12 ms 8832 KB Output is correct
12 Correct 39 ms 8832 KB Output is correct
13 Correct 13 ms 8960 KB Output is correct
14 Correct 13 ms 8960 KB Output is correct
15 Correct 15 ms 8832 KB Output is correct
16 Correct 14 ms 8832 KB Output is correct
17 Correct 16 ms 8832 KB Output is correct
18 Correct 14 ms 8832 KB Output is correct
19 Correct 27 ms 9496 KB Output is correct
20 Correct 28 ms 9600 KB Output is correct
21 Correct 31 ms 9648 KB Output is correct
22 Correct 26 ms 9600 KB Output is correct
23 Correct 28 ms 9680 KB Output is correct
24 Correct 34 ms 9600 KB Output is correct
25 Correct 36 ms 9592 KB Output is correct
26 Correct 24 ms 9600 KB Output is correct
27 Correct 33 ms 9600 KB Output is correct
28 Correct 25 ms 9600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 10092 KB Output is correct
2 Correct 94 ms 11860 KB Output is correct
3 Correct 162 ms 14060 KB Output is correct
4 Correct 196 ms 14060 KB Output is correct
5 Correct 183 ms 14060 KB Output is correct
6 Correct 175 ms 14060 KB Output is correct
7 Correct 174 ms 14080 KB Output is correct
8 Correct 183 ms 14060 KB Output is correct
9 Correct 181 ms 14060 KB Output is correct
10 Correct 118 ms 13164 KB Output is correct
11 Correct 119 ms 13036 KB Output is correct
12 Correct 101 ms 13036 KB Output is correct
13 Correct 112 ms 13108 KB Output is correct
14 Correct 125 ms 13036 KB Output is correct
15 Correct 155 ms 13108 KB Output is correct
16 Correct 144 ms 13068 KB Output is correct
17 Correct 136 ms 13036 KB Output is correct
18 Correct 105 ms 13036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8704 KB Output is correct
2 Correct 10 ms 8704 KB Output is correct
3 Correct 18 ms 8832 KB Output is correct
4 Correct 14 ms 8832 KB Output is correct
5 Correct 14 ms 8832 KB Output is correct
6 Correct 12 ms 8960 KB Output is correct
7 Correct 12 ms 8832 KB Output is correct
8 Correct 13 ms 8832 KB Output is correct
9 Correct 13 ms 8832 KB Output is correct
10 Correct 14 ms 8832 KB Output is correct
11 Correct 12 ms 8832 KB Output is correct
12 Correct 39 ms 8832 KB Output is correct
13 Correct 13 ms 8960 KB Output is correct
14 Correct 13 ms 8960 KB Output is correct
15 Correct 15 ms 8832 KB Output is correct
16 Correct 14 ms 8832 KB Output is correct
17 Correct 16 ms 8832 KB Output is correct
18 Correct 14 ms 8832 KB Output is correct
19 Correct 27 ms 9496 KB Output is correct
20 Correct 28 ms 9600 KB Output is correct
21 Correct 31 ms 9648 KB Output is correct
22 Correct 26 ms 9600 KB Output is correct
23 Correct 28 ms 9680 KB Output is correct
24 Correct 34 ms 9600 KB Output is correct
25 Correct 36 ms 9592 KB Output is correct
26 Correct 24 ms 9600 KB Output is correct
27 Correct 33 ms 9600 KB Output is correct
28 Correct 25 ms 9600 KB Output is correct
29 Correct 25 ms 10092 KB Output is correct
30 Correct 94 ms 11860 KB Output is correct
31 Correct 162 ms 14060 KB Output is correct
32 Correct 196 ms 14060 KB Output is correct
33 Correct 183 ms 14060 KB Output is correct
34 Correct 175 ms 14060 KB Output is correct
35 Correct 174 ms 14080 KB Output is correct
36 Correct 183 ms 14060 KB Output is correct
37 Correct 181 ms 14060 KB Output is correct
38 Correct 118 ms 13164 KB Output is correct
39 Correct 119 ms 13036 KB Output is correct
40 Correct 101 ms 13036 KB Output is correct
41 Correct 112 ms 13108 KB Output is correct
42 Correct 125 ms 13036 KB Output is correct
43 Correct 155 ms 13108 KB Output is correct
44 Correct 144 ms 13068 KB Output is correct
45 Correct 136 ms 13036 KB Output is correct
46 Correct 105 ms 13036 KB Output is correct
47 Correct 708 ms 27572 KB Output is correct
48 Correct 2654 ms 63008 KB Output is correct
49 Correct 2845 ms 67136 KB Output is correct
50 Correct 2974 ms 67040 KB Output is correct
51 Correct 2606 ms 67060 KB Output is correct
52 Correct 2960 ms 67168 KB Output is correct
53 Correct 3153 ms 67032 KB Output is correct
54 Correct 2975 ms 67136 KB Output is correct
55 Correct 2891 ms 67228 KB Output is correct
56 Correct 2631 ms 67252 KB Output is correct
57 Correct 2888 ms 67400 KB Output is correct
58 Correct 2894 ms 67276 KB Output is correct
59 Correct 2424 ms 64312 KB Output is correct
60 Correct 2306 ms 64468 KB Output is correct
61 Correct 2284 ms 64388 KB Output is correct
62 Correct 1987 ms 63512 KB Output is correct
63 Correct 2001 ms 63440 KB Output is correct
64 Correct 2200 ms 63540 KB Output is correct
65 Correct 1848 ms 62636 KB Output is correct
66 Correct 2168 ms 62700 KB Output is correct
67 Correct 1847 ms 62572 KB Output is correct