Submission #64628

# Submission time Handle Problem Language Result Execution time Memory
64628 2018-08-05T07:32:50 Z zscoder Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
7913 ms 339216 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int INF = int(1e9);

struct node
{
	int mn,lazy;
};

node st[4222222];

void push(int id, int l, int r)
{
	if(st[id].lazy!=0)
	{
		st[id].mn+=st[id].lazy;
		if(r-l>=2)
		{
			st[id*2].lazy+=st[id].lazy;
			st[id*2+1].lazy+=st[id].lazy;
		}
		st[id].lazy=0;
	}
}

void combine(int id)
{
	st[id].mn=min(st[id*2].mn,st[id*2+1].mn);
}

void build(int id, int l, int r)
{
	if(r-l<2)
	{
		st[id].lazy=0;
		st[id].mn=INF;
		return ;
	}	
	int mid=(l+r)>>1;
	build(id*2,l,mid); build(id*2+1,mid,r);
	combine(id);
}

void add(int id, int l, int r, int ql, int qr, int v)
{
	push(id,l,r);
	if(ql>=r||l>=qr) return ;
	if(ql<=l&&r<=qr)
	{
		st[id].lazy+=v;
		push(id,l,r);
		return ;
	}
	int mid=(l+r)>>1;
	add(id*2,l,mid,ql,qr,v); add(id*2+1,mid,r,ql,qr,v);
	combine(id);
}

void update(int id, int l, int r, int pos, int v)
{
	push(id,l,r);
	if(pos>=r||pos<l) return ;
	if(r-l<2)
	{
		st[id].lazy=0; st[id].mn=v;
		return ;
	}
	int mid=(l+r)>>1;
	update(id*2,l,mid,pos,v); update(id*2+1,mid,r,pos,v);
	combine(id);
}

int query(int id, int l, int r, int ql, int qr)
{
	push(id,l,r);
	if(ql>=r||l>=qr) return INF;
	if(ql<=l&&r<=qr) return st[id].mn;
	int mid=(l+r)>>1;
	return min(query(id*2,l,mid,ql,qr),query(id*2+1,mid,r,ql,qr));
}

pbds T;
int ma[511111];
int maq[511111];

std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V)
{
	int Q=X.size();
	std::vector<int> answer(Q);
	int n = A.size(); T.clear();
	int timer=0;
	for(int i=0;i<n;i++) T.insert(mp(A[i],++timer));
	vector<pair<int,ii> > vec;
	for(int i=0;i<n;i++) vec.pb(mp(A[i],mp(i,-1)));
	for(int i=0;i<Q;i++) vec.pb(mp(V[i],mp(X[i],i)));
	sort(vec.begin(),vec.end());
	int N = n+Q;
	for(int i=0;i<N;i++)
	{
		if(vec[i].se.se==-1)
		{
			ma[vec[i].se.fi] = i;
		}
	}
	for(int i=0;i<N;i++)
	{
		if(vec[i].se.se>=0)
		{
			maq[vec[i].se.se] = i;
		}
	}
	for(int i=0;i<n;i++)
	{
		update(1,0,N,ma[i],-(i+1)+T.order_of_key(mp(A[i]+1,-2)));
		//cerr<<"P : "<<i<<' '<<(i+1)-T.order_of_key(mp(A[i]+1,-2))<<'\n';
	}
	for(int i=0;i<Q;i++) 
	{
		auto it = T.lower_bound(mp(A[X[i]],-1)); T.erase(it); T.insert(mp(V[i],++timer));
		update(1,0,N,ma[X[i]],INF);
		ma[X[i]] = maq[i];
		//remove A[X[i]] from memories
		//add V[i] to memories
		int lb = lower_bound(vec.begin(),vec.end(),mp(A[X[i]],mp(-2,-2))) - vec.begin();
		add(1,0,N,lb,N,-1);
		lb = lower_bound(vec.begin(),vec.end(),mp(V[i],mp(-2,-2))) - vec.begin();
		add(1,0,N,lb,N,1);
		update(1,0,N,ma[X[i]],-(X[i]+1)+int(T.order_of_key(mp(V[i]+1,-2))));
		//cerr<<"SPECIAL : "<<-(X[i]+1)+int(T.order_of_key(mp(V[i]+1,-2)))<<'\n';
		answer[i] = -query(1,0,N,0,N);
		A[X[i]] = V[i];
	}
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 612 KB Output is correct
3 Correct 13 ms 792 KB Output is correct
4 Correct 10 ms 844 KB Output is correct
5 Correct 14 ms 892 KB Output is correct
6 Correct 11 ms 892 KB Output is correct
7 Correct 13 ms 892 KB Output is correct
8 Correct 10 ms 892 KB Output is correct
9 Correct 12 ms 924 KB Output is correct
10 Correct 11 ms 924 KB Output is correct
11 Correct 13 ms 924 KB Output is correct
12 Correct 11 ms 924 KB Output is correct
13 Correct 12 ms 924 KB Output is correct
14 Correct 9 ms 992 KB Output is correct
15 Correct 10 ms 992 KB Output is correct
16 Correct 11 ms 1016 KB Output is correct
17 Correct 11 ms 1016 KB Output is correct
18 Correct 13 ms 1044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 612 KB Output is correct
3 Correct 13 ms 792 KB Output is correct
4 Correct 10 ms 844 KB Output is correct
5 Correct 14 ms 892 KB Output is correct
6 Correct 11 ms 892 KB Output is correct
7 Correct 13 ms 892 KB Output is correct
8 Correct 10 ms 892 KB Output is correct
9 Correct 12 ms 924 KB Output is correct
10 Correct 11 ms 924 KB Output is correct
11 Correct 13 ms 924 KB Output is correct
12 Correct 11 ms 924 KB Output is correct
13 Correct 12 ms 924 KB Output is correct
14 Correct 9 ms 992 KB Output is correct
15 Correct 10 ms 992 KB Output is correct
16 Correct 11 ms 1016 KB Output is correct
17 Correct 11 ms 1016 KB Output is correct
18 Correct 13 ms 1044 KB Output is correct
19 Correct 43 ms 2020 KB Output is correct
20 Correct 55 ms 2168 KB Output is correct
21 Correct 49 ms 2488 KB Output is correct
22 Correct 47 ms 2592 KB Output is correct
23 Correct 43 ms 2820 KB Output is correct
24 Correct 36 ms 2988 KB Output is correct
25 Correct 48 ms 3188 KB Output is correct
26 Correct 39 ms 3468 KB Output is correct
27 Correct 42 ms 3520 KB Output is correct
28 Correct 35 ms 3656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 5408 KB Output is correct
2 Correct 171 ms 8092 KB Output is correct
3 Correct 344 ms 11612 KB Output is correct
4 Correct 399 ms 12188 KB Output is correct
5 Correct 394 ms 12616 KB Output is correct
6 Correct 409 ms 13192 KB Output is correct
7 Correct 326 ms 13900 KB Output is correct
8 Correct 326 ms 14352 KB Output is correct
9 Correct 416 ms 14932 KB Output is correct
10 Correct 308 ms 15512 KB Output is correct
11 Correct 297 ms 16276 KB Output is correct
12 Correct 289 ms 16952 KB Output is correct
13 Correct 262 ms 17480 KB Output is correct
14 Correct 281 ms 18272 KB Output is correct
15 Correct 250 ms 18828 KB Output is correct
16 Correct 261 ms 19556 KB Output is correct
17 Correct 271 ms 20300 KB Output is correct
18 Correct 252 ms 20708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 612 KB Output is correct
3 Correct 13 ms 792 KB Output is correct
4 Correct 10 ms 844 KB Output is correct
5 Correct 14 ms 892 KB Output is correct
6 Correct 11 ms 892 KB Output is correct
7 Correct 13 ms 892 KB Output is correct
8 Correct 10 ms 892 KB Output is correct
9 Correct 12 ms 924 KB Output is correct
10 Correct 11 ms 924 KB Output is correct
11 Correct 13 ms 924 KB Output is correct
12 Correct 11 ms 924 KB Output is correct
13 Correct 12 ms 924 KB Output is correct
14 Correct 9 ms 992 KB Output is correct
15 Correct 10 ms 992 KB Output is correct
16 Correct 11 ms 1016 KB Output is correct
17 Correct 11 ms 1016 KB Output is correct
18 Correct 13 ms 1044 KB Output is correct
19 Correct 43 ms 2020 KB Output is correct
20 Correct 55 ms 2168 KB Output is correct
21 Correct 49 ms 2488 KB Output is correct
22 Correct 47 ms 2592 KB Output is correct
23 Correct 43 ms 2820 KB Output is correct
24 Correct 36 ms 2988 KB Output is correct
25 Correct 48 ms 3188 KB Output is correct
26 Correct 39 ms 3468 KB Output is correct
27 Correct 42 ms 3520 KB Output is correct
28 Correct 35 ms 3656 KB Output is correct
29 Correct 59 ms 5408 KB Output is correct
30 Correct 171 ms 8092 KB Output is correct
31 Correct 344 ms 11612 KB Output is correct
32 Correct 399 ms 12188 KB Output is correct
33 Correct 394 ms 12616 KB Output is correct
34 Correct 409 ms 13192 KB Output is correct
35 Correct 326 ms 13900 KB Output is correct
36 Correct 326 ms 14352 KB Output is correct
37 Correct 416 ms 14932 KB Output is correct
38 Correct 308 ms 15512 KB Output is correct
39 Correct 297 ms 16276 KB Output is correct
40 Correct 289 ms 16952 KB Output is correct
41 Correct 262 ms 17480 KB Output is correct
42 Correct 281 ms 18272 KB Output is correct
43 Correct 250 ms 18828 KB Output is correct
44 Correct 261 ms 19556 KB Output is correct
45 Correct 271 ms 20300 KB Output is correct
46 Correct 252 ms 20708 KB Output is correct
47 Correct 2087 ms 43108 KB Output is correct
48 Correct 7364 ms 98816 KB Output is correct
49 Correct 7404 ms 118372 KB Output is correct
50 Correct 7417 ms 131136 KB Output is correct
51 Correct 7913 ms 144152 KB Output is correct
52 Correct 7435 ms 156888 KB Output is correct
53 Correct 7381 ms 169996 KB Output is correct
54 Correct 6655 ms 182924 KB Output is correct
55 Correct 7630 ms 196088 KB Output is correct
56 Correct 7215 ms 209156 KB Output is correct
57 Correct 7300 ms 222192 KB Output is correct
58 Correct 6515 ms 235128 KB Output is correct
59 Correct 6106 ms 246992 KB Output is correct
60 Correct 5996 ms 258536 KB Output is correct
61 Correct 5961 ms 270300 KB Output is correct
62 Correct 5283 ms 281688 KB Output is correct
63 Correct 5747 ms 293284 KB Output is correct
64 Correct 5480 ms 304816 KB Output is correct
65 Correct 4819 ms 316448 KB Output is correct
66 Correct 4823 ms 327600 KB Output is correct
67 Correct 4750 ms 339216 KB Output is correct