Submission #64847

# Submission time Handle Problem Language Result Execution time Memory
64847 2018-08-05T21:50:49 Z TadijaSebez Bubble Sort 2 (JOI18_bubblesort2) C++11
100 / 100
7848 ms 144012 KB
#include "bubblesort2.h"
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stdlib.h>
#include <ctime>
using namespace std;
#define pb push_back
#define mp make_pair
int max(int a, int b){ return a>b?a:b;}
const int N=500050;
const int M=2*N;
int ls[M],rs[M],tsz,root,val[M],Max[M],lzy[M],sz[M],y[M];
pair<int,int> id[M];
int MakeNode(pair<int,int> p, int v)
{
	tsz++;
	val[tsz]=Max[tsz]=v;
	id[tsz]=p;
	lzy[tsz]=0;
	sz[tsz]=1;
	y[tsz]=rand();
	return tsz;
}
void Push(int c)
{
	if(!c || !lzy[c]) return;
	if(ls[c]) Max[ls[c]]+=lzy[c],val[ls[c]]+=lzy[c],lzy[ls[c]]+=lzy[c];
	if(rs[c]) Max[rs[c]]+=lzy[c],val[rs[c]]+=lzy[c],lzy[rs[c]]+=lzy[c];
	lzy[c]=0;
}
void Pull(int c)
{
	if(!c) return;
	Push(c);
	Max[c]=val[c];
	sz[c]=sz[ls[c]]+sz[rs[c]]+1;
	if(ls[c]) Max[c]=max(Max[c],Max[ls[c]]);
	if(rs[c]) Max[c]=max(Max[c],Max[rs[c]]);
}
void Rotate1(int &c)
{
	int a=ls[c],b=rs[a];
	Push(c);Push(a);
	ls[c]=b;
	rs[a]=c;
	Pull(c);Pull(a);
	c=a;
}
void Rotate2(int &c)
{
	int a=rs[c],b=ls[a];
	Push(c);Push(a);
	rs[c]=b;
	ls[a]=c;
	Pull(c);Pull(a);
	c=a;
}
void Del(int &c, pair<int,int> p)
{
	if(!c) return;
	Push(c);
	if(id[c]==p)
	{
		if(!ls[c]) c=rs[c];
		else if(!rs[c]) c=ls[c];
		else if(y[ls[c]]>y[rs[c]]) Rotate1(c),Del(rs[c],p);
		else Rotate2(c),Del(ls[c],p);
		Pull(c);
		return;
	}
	if(id[c]<p) Del(rs[c],p);
	else Del(ls[c],p);
	Pull(c);
}
void Add(int &c, pair<int,int> p, int x)
{
	if(!c){ c=MakeNode(p,x);return;}
	Push(c);
	if(id[c]<p)
	{
		Add(rs[c],p,x);
		if(y[rs[c]]>y[c]) Rotate2(c);
	}
	else
	{
		Add(ls[c],p,x);
		if(y[ls[c]]>y[c]) Rotate1(c);
	}
	Pull(c);
}
void Inc(int &c, pair<int,int> p, int f)
{
	if(!c) return;
	Push(c);
	if(id[c]<p) Inc(rs[c],p,f);
	else
	{
		val[c]+=f;
		if(rs[c]) val[rs[c]]+=f,Max[rs[c]]+=f,lzy[rs[c]]+=f;
		Inc(ls[c],p,f);
	}
	Pull(c);
}
int Count(int c, pair<int,int> p)
{
	if(!c) return 0;
	//printf("(%i %i)\n",id[c].first,id[c].second);
	if(id[c]<p)
	{
		int ans=sz[ls[c]]+1;
		ans+=Count(rs[c],p);
		return ans;
	}
	else return Count(ls[c],p);
}
void Print(int c)
{
	if(!c) return;
	Print(ls[c]);
	printf("(%i %i) ",id[c].first,id[c].second);
	Print(rs[c]);
}
void Print(){ Print(root);printf("\n");}
vector<int> countScans(vector<int> a, vector<int> x, vector<int> v)
{
	srand(time(0));
	int n,i,q;
	for(i=1;i<=tsz;i++) ls[i]=rs[i]=0;
	root=tsz=0;
	n=a.size();
	q=x.size();
	vector<pair<int,int> > id;
	for(i=0;i<n;i++) id.pb(mp(a[i],i));
	sort(id.begin(),id.end());
	for(i=0;i<n;i++)
	{
		int val=id[i].second-i;
		//printf("%i\n",val);
		Add(root,id[i],val);
	}
	//Print();
	vector<int> ret;
	for(i=0;i<q;i++)
	{
		Del(root,mp(a[x[i]],x[i]));
		//Print();
		Inc(root,mp(a[x[i]],x[i]),1);
		//Print();
		//printf("->%i\n",Max[root]);
		Inc(root,mp(v[i],x[i]),-1);
		//Print();
		//printf("->%i\n",Max[root]);
		int j=Count(root,mp(v[i],x[i]));
		//Print();
		Add(root,mp(v[i],x[i]),x[i]-j);
		//Print();
		//printf("%i<- %i %i\n",x[i]-j,x[i],j);
		a[x[i]]=v[i];
		ret.pb(Max[root]);
	}
	return ret;
}
//----------------------//
/*
void Swap(int &a, int &b){ a^=b,b^=a,a^=b;}
int Count(vector<int> a)
{
	int sol=0;
	bool sw=1;
	while(sw)
	{
		sw=0;
		for(int i=0;i+1<a.size();i++) if(a[i]>a[i+1]) Swap(a[i],a[i+1]),sw=1;
		if(sw) sol++;
	}
	return sol;
}
vector<int> Brute(vector<int> a, vector<int> x, vector<int> v)
{
	vector<int> ret;
	for(int i=0;i<x.size();i++)
	{
		a[x[i]]=v[i];
		ret.pb(Count(a));
	}
	return ret;
}
void StressTest()
{
	srand(time(0));
	vector<int> a,x,v;
	int c=0;
	for(int h=0;h<10000;h++)
	{
		int n=rand()%10+1,i,q=rand()%5+1;
		a.resize(n);x.resize(q);v.resize(q);
		for(i=0;i<n;i++) a[i]=rand()%100;
		for(i=0;i<q;i++) x[i]=rand()%n,v[i]=rand()%100;
		vector<int> sol1=countScans(a,x,v);
		vector<int> sol2=Brute(a,x,v);
		if(sol1!=sol2)
		{
			printf("Test case %i:\n",h+1);
			printf("%i %i\n",n,q);
			for(i=0;i<n;i++) printf("%i ",a[i]);printf("\n");
			for(i=0;i<q;i++) printf("%i %i\n",x[i],v[i]);
			printf("My sol: ");
			for(i=0;i<sol1.size();i++) printf("%i ",sol1[i]);printf("\n");
			printf("Brute Force: ");
			for(i=0;i<sol2.size();i++) printf("%i ",sol2[i]);printf("\n");
		}
		else c++;
	}
	printf("%i of %i is correct!\n",c,10000);
}
int main()
{
	StressTest();return 0;
	int n,q,i;
	vector<int> a,x,v;
	scanf("%i %i",&n,&q);
	a.resize(n);x.resize(q);v.resize(q);
	for(i=0;i<n;i++) scanf("%i",&a[i]);
	for(i=0;i<q;i++) scanf("%i %i",&x[i],&v[i]);
	vector<int> sol=countScans(a,x,v);
	for(i=0;i<sol.size();i++) printf("%i\n",sol[i]);
	return 0;
}
*/
//----------------------//
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 5 ms 484 KB Output is correct
3 Correct 8 ms 836 KB Output is correct
4 Correct 11 ms 836 KB Output is correct
5 Correct 10 ms 944 KB Output is correct
6 Correct 8 ms 996 KB Output is correct
7 Correct 8 ms 1044 KB Output is correct
8 Correct 9 ms 1108 KB Output is correct
9 Correct 8 ms 1156 KB Output is correct
10 Correct 10 ms 1224 KB Output is correct
11 Correct 7 ms 1264 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 9 ms 1416 KB Output is correct
14 Correct 10 ms 1456 KB Output is correct
15 Correct 9 ms 1496 KB Output is correct
16 Correct 8 ms 1536 KB Output is correct
17 Correct 8 ms 1576 KB Output is correct
18 Correct 12 ms 1684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 5 ms 484 KB Output is correct
3 Correct 8 ms 836 KB Output is correct
4 Correct 11 ms 836 KB Output is correct
5 Correct 10 ms 944 KB Output is correct
6 Correct 8 ms 996 KB Output is correct
7 Correct 8 ms 1044 KB Output is correct
8 Correct 9 ms 1108 KB Output is correct
9 Correct 8 ms 1156 KB Output is correct
10 Correct 10 ms 1224 KB Output is correct
11 Correct 7 ms 1264 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 9 ms 1416 KB Output is correct
14 Correct 10 ms 1456 KB Output is correct
15 Correct 9 ms 1496 KB Output is correct
16 Correct 8 ms 1536 KB Output is correct
17 Correct 8 ms 1576 KB Output is correct
18 Correct 12 ms 1684 KB Output is correct
19 Correct 28 ms 2352 KB Output is correct
20 Correct 39 ms 2592 KB Output is correct
21 Correct 29 ms 2820 KB Output is correct
22 Correct 37 ms 3020 KB Output is correct
23 Correct 29 ms 3176 KB Output is correct
24 Correct 27 ms 3340 KB Output is correct
25 Correct 27 ms 3504 KB Output is correct
26 Correct 28 ms 3664 KB Output is correct
27 Correct 34 ms 3792 KB Output is correct
28 Correct 31 ms 4108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 4772 KB Output is correct
2 Correct 130 ms 6944 KB Output is correct
3 Correct 333 ms 9784 KB Output is correct
4 Correct 370 ms 10136 KB Output is correct
5 Correct 314 ms 10764 KB Output is correct
6 Correct 288 ms 11408 KB Output is correct
7 Correct 293 ms 11860 KB Output is correct
8 Correct 269 ms 12544 KB Output is correct
9 Correct 272 ms 13032 KB Output is correct
10 Correct 212 ms 13856 KB Output is correct
11 Correct 193 ms 14256 KB Output is correct
12 Correct 202 ms 15076 KB Output is correct
13 Correct 162 ms 15672 KB Output is correct
14 Correct 207 ms 16288 KB Output is correct
15 Correct 201 ms 16908 KB Output is correct
16 Correct 119 ms 17500 KB Output is correct
17 Correct 134 ms 18264 KB Output is correct
18 Correct 125 ms 18772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 5 ms 484 KB Output is correct
3 Correct 8 ms 836 KB Output is correct
4 Correct 11 ms 836 KB Output is correct
5 Correct 10 ms 944 KB Output is correct
6 Correct 8 ms 996 KB Output is correct
7 Correct 8 ms 1044 KB Output is correct
8 Correct 9 ms 1108 KB Output is correct
9 Correct 8 ms 1156 KB Output is correct
10 Correct 10 ms 1224 KB Output is correct
11 Correct 7 ms 1264 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 9 ms 1416 KB Output is correct
14 Correct 10 ms 1456 KB Output is correct
15 Correct 9 ms 1496 KB Output is correct
16 Correct 8 ms 1536 KB Output is correct
17 Correct 8 ms 1576 KB Output is correct
18 Correct 12 ms 1684 KB Output is correct
19 Correct 28 ms 2352 KB Output is correct
20 Correct 39 ms 2592 KB Output is correct
21 Correct 29 ms 2820 KB Output is correct
22 Correct 37 ms 3020 KB Output is correct
23 Correct 29 ms 3176 KB Output is correct
24 Correct 27 ms 3340 KB Output is correct
25 Correct 27 ms 3504 KB Output is correct
26 Correct 28 ms 3664 KB Output is correct
27 Correct 34 ms 3792 KB Output is correct
28 Correct 31 ms 4108 KB Output is correct
29 Correct 24 ms 4772 KB Output is correct
30 Correct 130 ms 6944 KB Output is correct
31 Correct 333 ms 9784 KB Output is correct
32 Correct 370 ms 10136 KB Output is correct
33 Correct 314 ms 10764 KB Output is correct
34 Correct 288 ms 11408 KB Output is correct
35 Correct 293 ms 11860 KB Output is correct
36 Correct 269 ms 12544 KB Output is correct
37 Correct 272 ms 13032 KB Output is correct
38 Correct 212 ms 13856 KB Output is correct
39 Correct 193 ms 14256 KB Output is correct
40 Correct 202 ms 15076 KB Output is correct
41 Correct 162 ms 15672 KB Output is correct
42 Correct 207 ms 16288 KB Output is correct
43 Correct 201 ms 16908 KB Output is correct
44 Correct 119 ms 17500 KB Output is correct
45 Correct 134 ms 18264 KB Output is correct
46 Correct 125 ms 18772 KB Output is correct
47 Correct 1413 ms 33864 KB Output is correct
48 Correct 7710 ms 79308 KB Output is correct
49 Correct 7661 ms 96724 KB Output is correct
50 Correct 6868 ms 109656 KB Output is correct
51 Correct 7773 ms 122312 KB Output is correct
52 Correct 7183 ms 135312 KB Output is correct
53 Correct 7283 ms 143812 KB Output is correct
54 Correct 7586 ms 143812 KB Output is correct
55 Correct 7848 ms 143812 KB Output is correct
56 Correct 7291 ms 143812 KB Output is correct
57 Correct 7757 ms 143816 KB Output is correct
58 Correct 6613 ms 143816 KB Output is correct
59 Correct 5651 ms 143824 KB Output is correct
60 Correct 5645 ms 143880 KB Output is correct
61 Correct 5391 ms 143892 KB Output is correct
62 Correct 4127 ms 143892 KB Output is correct
63 Correct 4867 ms 143904 KB Output is correct
64 Correct 5001 ms 143904 KB Output is correct
65 Correct 4408 ms 143932 KB Output is correct
66 Correct 3548 ms 143932 KB Output is correct
67 Correct 3419 ms 144012 KB Output is correct