Submission #1071899

# Submission time Handle Problem Language Result Execution time Memory
1071899 2024-08-23T12:13:12 Z Faisal_Saqib Mechanical Doll (IOI18_doll) C++17
68.758 / 100
82 ms 25112 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define vll vector<int>
#define ll int
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y);
vector<int> XP,YP;
const int NM=1e6+10;
ll cp[NM],final[NM],label=0,father_of_all=-1;
vll fast_order(ll n)
{
    if(n==2)
        return {1,2};
    vll ans;
    ll hn=n/2;
    for(int i=1;i<=n;i+=2)
        ans.pb(i);
    vll hans(hn);
    vll np=fast_order(n/2);
    for(int j=0;j<hn;j++)
        hans[np[j]-1]=ans[j];
    ans.clear();
    for(auto i:hans)
        ans.pb(i);
    for(auto i:hans)
        ans.pb(i+1);
    return ans;
}
vll childx,childy,order_,npp;
int label_it(vll&p,int l,int r,bool lp=0)
{
	// cout<<"Range "<<l<<' '<<r<<' '<<" p: ";
	// for(int i=l;i<=r;i++)
	// {
	// 	cout<<p[i]<<' ';
	// }
	// cout<<endl;
	if((l-r)==0){
		// cout<<"Assigned ";
		if(p[l]==-NM)
		{
			// cout<<father_of_all<<endl;
			return father_of_all;
		}
		// cout<<p[l]<<endl;
		return p[l];
	}
	int mid=(l+r)/2;
	bool use=0;
	for(int i=l;i<=r;i++)
	{
		if(p[i]!=-NM)
		{
			use=1;
			break;
		}
	}
	label++;
	int usep=-label;
	int fk=-1;
	if(!use)
	{
		// all minus one is subtree
		// cout<<"Range "<<l<<' '<<r<<endl;
		// cout<<"Child: "<<father_of_all<<' '<<father_of_all<<endl;
		fk=childx.size();
		childx.pb(father_of_all);
		childy.pb(father_of_all);
	}
	else
	{
		int lc=label_it(p,l,mid,1);
		int rc=label_it(p,mid+1,r,1);
		fk=childx.size();
		childx.pb(lc);
		childy.pb(rc);
		// cout<<"Range "<<l<<' '<<r<<endl;
		// cout<<"Child: "<<XP.back()<<' '<<YP.back()<<endl;
	}
	// cout<<"Assigned to range "<<l<<' '<<r<<' ';
	// cout<<usep<<endl;
	order_.pb(fk);
	npp.pb(usep);
	return usep;
}
void muskil(int m,vector<int> a)
{
XP.clear();
	YP.clear();
	label=0;
	vector<int> ans(m+1);
	vector<int> child[m+2];
	vector<int> cur;
	cur.pb(0);
	for(int j=0;j<a.size();j++)
		cur.pb(a[j]);
	cur.pb(0);
	for(int j=1;j<cur.size();j++)
		child[cur[j-1]].push_back(cur[j]);
	int lps=0;
	for(int i=0;i<=m;i++)
	{
		if(child[i].size()==0)continue;
		if(child[i].size()==1)
		{
			ans[i]=child[i][0];
		}
		else
		{
			ll pw=1;
			while(child[i].size()>pw)
				pw*=2;
			int zxp=child[i].size(),cpp=0;
			vll order=fast_order(pw);
			vll fl(pw,1);
			// cout<<"before\n";
			for(int j=zxp;j<pw;j++)
				fl[j-zxp]=-NM;
			for(int j=0;j<pw;)
			{
				order[j]--;
				while(fl[order[j]]==-NM)
					j++;
				fl[order[j]]=child[i][cpp];
				j++;
				cpp++;
			}
			// cout<<"After\n";
			// for(int j=0;j<pw;j++)
			// {
			// 	cout<<fl[j]<<' ';
			// }
			// cout<<endl;
			ll og=label;
			father_of_all=-label-1;
			childx.clear();
			childy.clear();
			order_.clear();
			npp.clear();
			label_it(fl,0,pw-1);
			ll last=XP.size();
			for(int i=0;i<order_.size();i++)
			{
				XP.pb(0);
				YP.pb(0);
			}
			for(int i=0;i<order_.size();i++)
			{
				ll cur=-og-npp[i]-1;
				XP[last+cur]=(childx[i]);
				YP[last+cur]=(childy[i]);
			}
			ans[i]=father_of_all;
		}
	}
	answer(ans,XP,YP);
}
void create_circuit(int m, std::vector<int> a)
{
	if(m==1)
	{
		muskil(m,a);
		return;
	}
	XP.clear();
	YP.clear();
	label=0;
	vector<int> ans(m+1);
	vector<int> child[m+2];
	vector<int> cur;
	if(a.size()==16)
	{
		int v=a[0];
		child[0].push_back(v);
		for(int j=1;j<a.size();j++)
			child[v].push_back(a[j]);
		child[v].push_back(0);
		int lps=0;
		for(int j=0;j<=m;j++)
			ans[j]=-1;
		// cout<<"SZ: "<<child[v].size()<<endl;
		for(auto i:{0,v})
		{
			if(child[i].size()==0)continue;
			if(child[i].size()==1)
			{
				ans[i]=child[i][0];
			}
			else
			{
				ll pw=1;
				while(child[i].size()>pw)
					pw*=2;
				int zxp=child[i].size(),cpp=0;
				for(int j=zxp;j<pw;j++)
				{
					cp[cpp]=-NM;
					cpp++;
				}
				for(int j=0;j<child[i].size();j++)
				{
					cp[cpp]=child[i][j];
					cpp++;
				}
				vll order=fast_order(pw);
				vll fl(pw);
				for(int j=0;j<pw;j++)
				{
					order[j]--;
					fl[order[j]]=cp[j];
					final[order[j]]=cp[j];
				}
				// cout<<"final: ";
				// for(int j=0;j<pw;j++)
				// 	cout<<final[j]<<' ';
				// cout<<endl;
				// what switches?
				ll og=label;
				father_of_all=-label-1;
				// pw then
				// cout<<"Father "<<father_of_all<<endl;
				// cout<<"Do label\n";
				childx.clear();
				childy.clear();
				order_.clear();
				npp.clear();
				label_it(fl,0,pw-1);
				// cout<<"Order: ";
				// for(auto i:order_)
				// 	cout<<i<<' ';
				// cout<<endl;
				// cout<<"npp: ";
				// for(auto i:npp)
				// 	cout<<i<<' ';
				// cout<<endl;
				ll last=XP.size();
				for(int i=0;i<order_.size();i++)
				{
					XP.pb(0);
					YP.pb(0);
				}
				for(int i=0;i<order_.size();i++)
				{
					ll cur=-og-npp[i]-1;
					XP[last+cur]=(childx[i]);
					YP[last+cur]=(childy[i]);
				}
				ans[i]=father_of_all;
			}
			/*
			else if(child[i].size()==2) // solve atmost 2
			{
				// We need
				s++;
				ans[i]=-s;
				x.pb(child[i][0]);
				y.pb(child[i][1]);
			}
			else if(child[i].size()==3)
			{
				int s1=s+1;
				int s2=s+2;
				int s3=s+3;
				s+=3;
				ans[i]=-s1;
				// s1
					x.pb(-s2);
					y.pb(-s3);
				//s2
					x.pb(-s1);
					y.pb(child[i][1]);
				//s3
					x.pb(child[i][0]);
					y.pb(child[i][2]);
			}
			else if(child[i].size()==4)
			{
				int s1=s+1;
				int s2=s+2;
				int s3=s+3;
				s+=3;
				ans[i]=-s1;
				// s1
					x.pb(-s2);
					y.pb(-s3);
				//s2
					x.pb(child[i][0]);
					y.pb(child[i][2]);
				//s3
					x.pb(child[i][1]);
					y.pb(child[i][3]);
			}
			*/
		}

		// ans[cur.back()]=0;
		// cout<<"Ans: ";
		// for(int i=0;i<=m;i++)cout<<ans[i]<<' ';
		// cout<<endl;
		// for(int j=0;j<label;j++)
		// {
		// 	cout<<"Child of "<<-j-1<<' '<<XP[j]<<' '<<YP[j]<<endl;
		// }
		answer(ans,XP,YP);
	}
	else
	{
		cur.pb(0);
		for(int j=0;j<a.size();j++)
			cur.pb(a[j]);
		cur.pb(0);
		for(int j=1;j<cur.size();j++)
			child[cur[j-1]].push_back(cur[j]);
		int lps=0;
		for(int i=0;i<=m;i++)
		{
			if(child[i].size()==0)continue;
			if(child[i].size()==1)
			{
				ans[i]=child[i][0];
			}
			else
			{
				ll pw=1;
				while(child[i].size()>pw)
					pw*=2;
				int zxp=child[i].size(),cpp=0;
				for(int j=zxp;j<pw;j++)
				{
					cp[cpp]=-NM;
					cpp++;
				}
				for(int j=0;j<child[i].size();j++)
				{
					cp[cpp]=child[i][j];
					cpp++;
				}
				vll order=fast_order(pw);
				vll fl(pw);
				for(int j=0;j<pw;j++)
				{
					order[j]--;
					fl[order[j]]=cp[j];
					final[order[j]]=cp[j];
				}
				// cout<<"final: ";
				// for(int j=0;j<pw;j++)
				// 	cout<<final[j]<<' ';
				// cout<<endl;
				// what switches?
				ll og=label;
				father_of_all=-label-1;
				// pw then
				// cout<<"Father "<<father_of_all<<endl;
				// cout<<"Do label\n";
				childx.clear();
				childy.clear();
				order_.clear();
				npp.clear();
				label_it(fl,0,pw-1);
				// cout<<"X: ";
				// for(auto i:childx)
				// {
				// 	cout<<i<<' ';
				// }
				// cout<<endl;
				// cout<<"Y: ";
				// for(auto i:childy)
				// {
				// 	cout<<i<<' ';
				// }
				// cout<<endl;
				// cout<<"Order: ";
				// for(auto i:order_)
				// 	cout<<i<<' ';
				// cout<<endl;
				// cout<<"npp: ";
				// for(auto i:npp)
				// 	cout<<i<<' ';
				// cout<<endl;
				ll last=XP.size();
				for(int i=0;i<order_.size();i++)
				{
					XP.pb(0);
					YP.pb(0);
				}
				for(int i=0;i<order_.size();i++)
				{
					ll cur=-og-npp[i]-1;
					XP[last+cur]=(childx[i]);
					YP[last+cur]=(childy[i]);
				}
				ans[i]=father_of_all;
			}
			/*
			else if(child[i].size()==2) // solve atmost 2
			{
				// We need
				s++;
				ans[i]=-s;
				x.pb(child[i][0]);
				y.pb(child[i][1]);
			}
			else if(child[i].size()==3)
			{
				int s1=s+1;
				int s2=s+2;
				int s3=s+3;
				s+=3;
				ans[i]=-s1;
				// s1
					x.pb(-s2);
					y.pb(-s3);
				//s2
					x.pb(-s1);
					y.pb(child[i][1]);
				//s3
					x.pb(child[i][0]);
					y.pb(child[i][2]);
			}
			else if(child[i].size()==4)
			{
				int s1=s+1;
				int s2=s+2;
				int s3=s+3;
				s+=3;
				ans[i]=-s1;
				// s1
					x.pb(-s2);
					y.pb(-s3);
				//s2
					x.pb(child[i][0]);
					y.pb(child[i][2]);
				//s3
					x.pb(child[i][1]);
					y.pb(child[i][3]);
			}
			*/
		}
		// cout<<"ans\n";
		// for(int i=0;i<=m;i++)
		// {
		// 	cout<<ans[i]<<' ';
		// }
		// cout<<endl;
		// cout<<"xy\n";
		// cout<<XP.size()<<endl;
		// cout<<YP.size()<<endl;
		// for(auto i:XP)
		// {
		// 	cout<<i<<' ';
		// }
		// cout<<endl;
		// for(auto i:YP)
		// {
		// 	cout<<i<<' ';
		// }
		// cout<<endl;
		// for(int i=0;i<label;i++)
		// {
		// 	cout<<XP[i]<<' '<<YP[i]<<endl;
		// }
		answer(ans,XP,YP);
	}
}

Compilation message

doll.cpp: In function 'void muskil(int, std::vector<int>)':
doll.cpp:95:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |  for(int j=0;j<a.size();j++)
      |              ~^~~~~~~~~
doll.cpp:98:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |  for(int j=1;j<cur.size();j++)
      |              ~^~~~~~~~~~~
doll.cpp:111:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  111 |    while(child[i].size()>pw)
      |          ~~~~~~~~~~~~~~~^~~
doll.cpp:142:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |    for(int i=0;i<order_.size();i++)
      |                ~^~~~~~~~~~~~~~
doll.cpp:147:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |    for(int i=0;i<order_.size();i++)
      |                ~^~~~~~~~~~~~~~
doll.cpp:100:6: warning: unused variable 'lps' [-Wunused-variable]
  100 |  int lps=0;
      |      ^~~
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:175:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |   for(int j=1;j<a.size();j++)
      |               ~^~~~~~~~~
doll.cpp:192:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  192 |     while(child[i].size()>pw)
      |           ~~~~~~~~~~~~~~~^~~
doll.cpp:200:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |     for(int j=0;j<child[i].size();j++)
      |                 ~^~~~~~~~~~~~~~~~
doll.cpp:237:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  237 |     for(int i=0;i<order_.size();i++)
      |                 ~^~~~~~~~~~~~~~
doll.cpp:242:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  242 |     for(int i=0;i<order_.size();i++)
      |                 ~^~~~~~~~~~~~~~
doll.cpp:178:7: warning: unused variable 'lps' [-Wunused-variable]
  178 |   int lps=0;
      |       ^~~
doll.cpp:309:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  309 |   for(int j=0;j<a.size();j++)
      |               ~^~~~~~~~~
doll.cpp:312:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  312 |   for(int j=1;j<cur.size();j++)
      |               ~^~~~~~~~~~~
doll.cpp:325:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  325 |     while(child[i].size()>pw)
      |           ~~~~~~~~~~~~~~~^~~
doll.cpp:333:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  333 |     for(int j=0;j<child[i].size();j++)
      |                 ~^~~~~~~~~~~~~~~~
doll.cpp:382:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  382 |     for(int i=0;i<order_.size();i++)
      |                 ~^~~~~~~~~~~~~~
doll.cpp:387:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  387 |     for(int i=0;i<order_.size();i++)
      |                 ~^~~~~~~~~~~~~~
doll.cpp:314:7: warning: unused variable 'lps' [-Wunused-variable]
  314 |   int lps=0;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 17 ms 6612 KB Output is correct
3 Correct 14 ms 5588 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 6 ms 5980 KB Output is correct
6 Correct 22 ms 8136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 17 ms 6612 KB Output is correct
3 Correct 14 ms 5588 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 6 ms 5980 KB Output is correct
6 Correct 22 ms 8136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 30 ms 9936 KB Output is correct
9 Correct 33 ms 11204 KB Output is correct
10 Correct 46 ms 14280 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 17 ms 6612 KB Output is correct
3 Correct 14 ms 5588 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 6 ms 5980 KB Output is correct
6 Correct 22 ms 8136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 30 ms 9936 KB Output is correct
9 Correct 33 ms 11204 KB Output is correct
10 Correct 46 ms 14280 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 61 ms 13908 KB Output is correct
15 Correct 33 ms 8760 KB Output is correct
16 Correct 50 ms 11508 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 53 ms 13688 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 348 KB Output is partially correct
2 Correct 27 ms 9276 KB Output is correct
3 Partially correct 31 ms 9816 KB Output is partially correct
4 Correct 44 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 348 KB Output is partially correct
2 Correct 27 ms 9276 KB Output is correct
3 Partially correct 31 ms 9816 KB Output is partially correct
4 Correct 44 ms 12632 KB Output is correct
5 Partially correct 70 ms 15248 KB Output is partially correct
6 Partially correct 82 ms 16108 KB Output is partially correct
7 Partially correct 73 ms 15900 KB Output is partially correct
8 Partially correct 80 ms 16280 KB Output is partially correct
9 Partially correct 44 ms 19268 KB Output is partially correct
10 Partially correct 75 ms 25112 KB Output is partially correct
11 Partially correct 65 ms 18320 KB Output is partially correct
12 Partially correct 46 ms 12480 KB Output is partially correct
13 Partially correct 45 ms 11288 KB Output is partially correct
14 Partially correct 45 ms 11124 KB Output is partially correct
15 Partially correct 45 ms 10776 KB Output is partially correct
16 Partially correct 2 ms 2652 KB Output is partially correct
17 Partially correct 36 ms 9844 KB Output is partially correct
18 Partially correct 38 ms 10516 KB Output is partially correct
19 Partially correct 40 ms 10304 KB Output is partially correct
20 Partially correct 54 ms 12524 KB Output is partially correct
21 Partially correct 58 ms 16236 KB Output is partially correct
22 Partially correct 47 ms 12024 KB Output is partially correct