Submission #758029

# Submission time Handle Problem Language Result Execution time Memory
758029 2023-06-14T05:10:07 Z alexander707070 Ancient Books (IOI17_books) C++14
Compilation error
0 ms 0 KB
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
 
int n,perm[MAXN],br,cnt,last,maxpos,to[MAXN],curr,pos,first;
bool li[MAXN],ok,used[MAXN];
long long sum,add[MAXN],ans;

bool tree[4*MAXN];

void update(int v,int l,int r,int ll,int rr){
	if(ll>rr)return;
	if(l==ll and r==rr){
		tree[v]=true;
	}else{
		int tt=(l+r)/2;
		update(2*v,l,tt,ll,min(tt,rr));
		update(2*v+1,tt+1,r,max(tt+1,ll),rr);
	}
}

bool getpos(int v,int l,int r,int pos){
	if(l==r)return tree[v];
	int tt=(l+r)/2;
	if(pos<=tt)return getpos(2*v,l,tt,pos) or tree[v];
	else return getpos(2*v+1,tt+1,r,pos)  or tree[v];
}
 
void dfs(int x){
	li[x]=true; 
	sum+=abs(x-perm[x]); br++;

	update(1,0,n-1,min(x,perm[x]),max(x,perm[x])-1);

	if(!li[perm[x]])dfs(perm[x]);
}

void dfs2(int x){
	li[x]=true; 
	sum+=abs(x-perm[x]); br++;

	update(1,0,n-1,min(x,perm[x])+1,max(x,perm[x]));

	if(!li[perm[x]])dfs2(perm[x]);
}
 
long long minimum_walk(vector<int> p, int s){
	n=int(p.size());
 
	for(int i=0;i<n;i++)perm[i]=p[i];
 
	for(int i=0;i<n;i++){
		if(!li[i]){
			sum=br=0; 
			dfs(i); add[i]=sum;
			if(br>1)last=i;
		}
	}

	for(int i=s;i<=last;i++){
		ans+=add[i];
		if(!getpos(1,0,n-1,i) and i<last)ans+=2;
	}

	for(int i=0;i<4*n;i++)tree[i]=false;
	for(int i=0;i<n;i++)li[i]=false;

	for(int i=n-1;i>=0;i--){
		if(!li[i]){
			sum=br=0; 
			dfs2(i); add[i]=sum;
			if(br>1)first=i;
		}
	}

	for(int i=s;i>=first;i--){
		ans+=add[i];
		if(!getpos(1,0,n-1,i) and i>first)ans+=2;
	}

	return ans;
}
 

int main(){
	cout<<minimum_walk({0,2,3,1}, 1)<<"\n";
}

Compilation message

/usr/bin/ld: /tmp/ccCYS5rk.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccsG48Tl.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status