Submission #143844

# Submission time Handle Problem Language Result Execution time Memory
143844 2019-08-15T10:18:06 Z shashwatchandra Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
5705 ms 222140 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(),v.end()
#define pb push_back
#define REP(i,n) for(int i = 0;i < n;i++)
#define RE(i,n) for(int i = 1;i <= n;i++)

const int N = 1e6+1;
int seg[4*N];
int f[4*N];
int lazy[4*N];
set<int> best[N];
map<int,int> cool;
vector<int> comp;
int n;

#define mid (l+r)/2
#define child 2*node

void build(int node,int l,int r){
	lazy[node] = 0;
	if(l == r){
		f[node] = 0;
		seg[node] = -*best[l].begin();
		return;
	}
	build(child,l,mid);
	build(child+1,mid+1,r);
	seg[node] = max(seg[child+1],seg[child]);
}

inline void pushdown(int node,int l,int r,bool goat = 1){
	seg[node] -= lazy[node];
	f[node] -= lazy[node];
	if(l != r){
		lazy[child] += lazy[node];
		lazy[child+1] += lazy[node];
		if(goat){
			pushdown(child,l,mid,0);
			pushdown(child+1,mid+1,r,0);
		}
	}
	lazy[node] = 0;
	return;
}

int wow(int node,int l,int r,int ind){
	pushdown(node,l,r);
	if(l == r)return -f[node];
	if(ind <= mid)return wow(child,l,mid,ind);
	return wow(child+1,mid+1,r,ind);
}

void add(int node,int l,int r,int start,int end,int val,bool print = 0){
	if(print)cout << l << " " << r << endl;
	pushdown(node,l,r);
	if(l > end or start > r)return;
	if(start <= l and r <= end){
		if(print)cout << l << " " << r << endl;
		lazy[node] += val;
		pushdown(node,l,r);
		return;
	}
	add(child,l,mid,start,end,val,print);
	add(child+1,mid+1,r,start,end,val,print);
	seg[node] = max(seg[child+1],seg[child]);
}

void update(int node,int l,int r,int ind,bool print = 0){
	if(print)cout << l << " " << r << endl;
	pushdown(node,l,r);
	if(l == r){
		if(print)cout << -f[node] << " " << -*best[l].begin() << endl;
		seg[node] = f[node]-*best[l].begin();
		return;
	}
	if(ind <= mid)update(child,l,mid,ind,print);
	else update(child+1,mid+1,r,ind,print);
	//cout << "TELLL ME WHYY " << endl;
	//cout << node << " " << seg[node] << endl;
	seg[node] = max(seg[child+1],seg[child]);
	//cout << node << " " << seg[node] << endl;
}

int cur = 0;

void compress(){
	sort(all(comp));
	REP(i,comp.size()){
		if(!i or comp[i] != comp[i-1]){
			cur++;
			best[cur].insert(0);
			cool[comp[i]] = cur;
		}
	}
}

vector<int> countScans(vector<int> a,vector<int> x,vector<int> v){
	vector<int> answer;
	n = a.size();
	int q = x.size();
	REP(i,n)comp.pb(a[i]);
	REP(i,q)comp.pb(v[i]);
	compress();
	REP(i,n){
		a[i] = cool[a[i]];
		//cout << a[i] << " ";
		best[a[i]].insert(-(i+1));
	}
	//cout << endl;
	REP(i,q){
	//	cout << v[i] << " ";
		v[i] = cool[v[i]];
	}
	//cout << endl;
	build(1,1,cur);
	REP(i,n){
		add(1,1,cur,a[i],cur,1);
	}	
	
	REP(i,q){
		int ind = x[i];
		int val = v[i];
		if(a[ind] != val){
			int upd = (a[ind] > val)?1:-1;
			add(1,1,cur,min(a[ind],val),max(a[ind],val)-1,upd);
			//cout << "AINT NOTHING " << seg[5] << endl;
			RE(i,cur){
			//	cout << i << " " << wow(1,1,cur,i) << endl;
			}
		//	print(1,1,cur);
			best[a[ind]].erase(-(ind+1));
			best[val].insert(-(ind+1));
			update(1,1,cur,a[ind],0);
			update(1,1,cur,val,0);
		//	print(1,1,cur);
			a[ind] = val;
		}
		//pushdown(1,1,cur);
		answer.push_back(seg[1]);
	}
	return answer;
}

Compilation message

bubblesort2.cpp: In function 'void compress()':
bubblesort2.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,n) for(int i = 0;i < n;i++)
bubblesort2.cpp:92:6:
  REP(i,comp.size()){
      ~~~~~~~~~~~~~                
bubblesort2.cpp:92:2: note: in expansion of macro 'REP'
  REP(i,comp.size()){
  ^~~
# Verdict Execution time Memory Grader output
1 Correct 54 ms 47480 KB Output is correct
2 Correct 56 ms 47608 KB Output is correct
3 Correct 60 ms 47992 KB Output is correct
4 Correct 62 ms 48248 KB Output is correct
5 Correct 60 ms 47992 KB Output is correct
6 Correct 54 ms 47992 KB Output is correct
7 Correct 53 ms 47992 KB Output is correct
8 Correct 52 ms 48020 KB Output is correct
9 Correct 56 ms 48120 KB Output is correct
10 Correct 52 ms 48032 KB Output is correct
11 Correct 59 ms 47868 KB Output is correct
12 Correct 57 ms 47992 KB Output is correct
13 Correct 52 ms 47992 KB Output is correct
14 Correct 52 ms 47992 KB Output is correct
15 Correct 61 ms 47992 KB Output is correct
16 Correct 58 ms 47956 KB Output is correct
17 Correct 52 ms 47964 KB Output is correct
18 Correct 51 ms 47864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 47480 KB Output is correct
2 Correct 56 ms 47608 KB Output is correct
3 Correct 60 ms 47992 KB Output is correct
4 Correct 62 ms 48248 KB Output is correct
5 Correct 60 ms 47992 KB Output is correct
6 Correct 54 ms 47992 KB Output is correct
7 Correct 53 ms 47992 KB Output is correct
8 Correct 52 ms 48020 KB Output is correct
9 Correct 56 ms 48120 KB Output is correct
10 Correct 52 ms 48032 KB Output is correct
11 Correct 59 ms 47868 KB Output is correct
12 Correct 57 ms 47992 KB Output is correct
13 Correct 52 ms 47992 KB Output is correct
14 Correct 52 ms 47992 KB Output is correct
15 Correct 61 ms 47992 KB Output is correct
16 Correct 58 ms 47956 KB Output is correct
17 Correct 52 ms 47964 KB Output is correct
18 Correct 51 ms 47864 KB Output is correct
19 Correct 81 ms 49876 KB Output is correct
20 Correct 85 ms 50208 KB Output is correct
21 Correct 81 ms 50268 KB Output is correct
22 Correct 94 ms 50088 KB Output is correct
23 Correct 92 ms 49912 KB Output is correct
24 Correct 83 ms 49912 KB Output is correct
25 Correct 87 ms 49852 KB Output is correct
26 Correct 81 ms 49784 KB Output is correct
27 Correct 89 ms 49656 KB Output is correct
28 Correct 79 ms 49656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 49144 KB Output is correct
2 Correct 120 ms 50688 KB Output is correct
3 Correct 174 ms 52336 KB Output is correct
4 Correct 175 ms 52208 KB Output is correct
5 Correct 172 ms 52340 KB Output is correct
6 Correct 169 ms 52336 KB Output is correct
7 Correct 168 ms 52436 KB Output is correct
8 Correct 169 ms 52208 KB Output is correct
9 Correct 166 ms 52212 KB Output is correct
10 Correct 116 ms 52336 KB Output is correct
11 Correct 115 ms 52372 KB Output is correct
12 Correct 114 ms 52336 KB Output is correct
13 Correct 124 ms 52336 KB Output is correct
14 Correct 139 ms 52336 KB Output is correct
15 Correct 131 ms 52464 KB Output is correct
16 Correct 133 ms 52452 KB Output is correct
17 Correct 143 ms 52364 KB Output is correct
18 Correct 130 ms 52336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 47480 KB Output is correct
2 Correct 56 ms 47608 KB Output is correct
3 Correct 60 ms 47992 KB Output is correct
4 Correct 62 ms 48248 KB Output is correct
5 Correct 60 ms 47992 KB Output is correct
6 Correct 54 ms 47992 KB Output is correct
7 Correct 53 ms 47992 KB Output is correct
8 Correct 52 ms 48020 KB Output is correct
9 Correct 56 ms 48120 KB Output is correct
10 Correct 52 ms 48032 KB Output is correct
11 Correct 59 ms 47868 KB Output is correct
12 Correct 57 ms 47992 KB Output is correct
13 Correct 52 ms 47992 KB Output is correct
14 Correct 52 ms 47992 KB Output is correct
15 Correct 61 ms 47992 KB Output is correct
16 Correct 58 ms 47956 KB Output is correct
17 Correct 52 ms 47964 KB Output is correct
18 Correct 51 ms 47864 KB Output is correct
19 Correct 81 ms 49876 KB Output is correct
20 Correct 85 ms 50208 KB Output is correct
21 Correct 81 ms 50268 KB Output is correct
22 Correct 94 ms 50088 KB Output is correct
23 Correct 92 ms 49912 KB Output is correct
24 Correct 83 ms 49912 KB Output is correct
25 Correct 87 ms 49852 KB Output is correct
26 Correct 81 ms 49784 KB Output is correct
27 Correct 89 ms 49656 KB Output is correct
28 Correct 79 ms 49656 KB Output is correct
29 Correct 69 ms 49144 KB Output is correct
30 Correct 120 ms 50688 KB Output is correct
31 Correct 174 ms 52336 KB Output is correct
32 Correct 175 ms 52208 KB Output is correct
33 Correct 172 ms 52340 KB Output is correct
34 Correct 169 ms 52336 KB Output is correct
35 Correct 168 ms 52436 KB Output is correct
36 Correct 169 ms 52208 KB Output is correct
37 Correct 166 ms 52212 KB Output is correct
38 Correct 116 ms 52336 KB Output is correct
39 Correct 115 ms 52372 KB Output is correct
40 Correct 114 ms 52336 KB Output is correct
41 Correct 124 ms 52336 KB Output is correct
42 Correct 139 ms 52336 KB Output is correct
43 Correct 131 ms 52464 KB Output is correct
44 Correct 133 ms 52452 KB Output is correct
45 Correct 143 ms 52364 KB Output is correct
46 Correct 130 ms 52336 KB Output is correct
47 Correct 1350 ms 105092 KB Output is correct
48 Correct 5061 ms 208036 KB Output is correct
49 Correct 5583 ms 221880 KB Output is correct
50 Correct 5705 ms 221940 KB Output is correct
51 Correct 5516 ms 222028 KB Output is correct
52 Correct 5462 ms 221996 KB Output is correct
53 Correct 5549 ms 221972 KB Output is correct
54 Correct 5075 ms 222076 KB Output is correct
55 Correct 5374 ms 222020 KB Output is correct
56 Correct 4957 ms 222040 KB Output is correct
57 Correct 5273 ms 221916 KB Output is correct
58 Correct 5022 ms 222140 KB Output is correct
59 Correct 4562 ms 209208 KB Output is correct
60 Correct 4522 ms 209048 KB Output is correct
61 Correct 4651 ms 209156 KB Output is correct
62 Correct 4369 ms 203096 KB Output is correct
63 Correct 4354 ms 203140 KB Output is correct
64 Correct 4299 ms 203028 KB Output is correct
65 Correct 4187 ms 197036 KB Output is correct
66 Correct 4117 ms 197180 KB Output is correct
67 Correct 4454 ms 197132 KB Output is correct