Submission #117198

# Submission time Handle Problem Language Result Execution time Memory
117198 2019-06-15T10:11:12 Z imyujin Treasure Hunt (CEOI11_tre) C++14
100 / 100
2848 ms 97400 KB
#include<stdio.h>

#define MAXN 400010

int n;
int num[MAXN], dep[MAXN], pidx[MAXN][20], pnum[MAXN][20], pdep[MAXN][20];

int bsnum(int x){
	int s=0, e=n-1;
	while(s<e){
		int m=(s+e)/2;
		if(x<=num[m]) e=m;
		else s=m+1;
	}
	return s;
}

int bsdep(int d, int idx){
	for(int i=19; i>=0; i--)
		if(pdep[idx][i]>=d) idx=pidx[idx][i];
	return idx;
}

void init(){
	n=1;
	num[0]=1;
	dep[0]=0;
	for(int i=0; i<20; i++){
		pidx[0][i]=pdep[0][i]=0;
		pnum[0][i]=1;
	}
}

void path(int a, int s){
	num[n]=num[n-1]+s;
	pidx[n][0]=bsnum(a);
	pdep[n][0]=dep[pidx[n][0]]-num[pidx[n][0]]+a;
	pnum[n][0]=a;
	dep[n]=pdep[n][0]+s;
	for(int i=1; i<20; i++){
		pidx[n][i]=pidx[pidx[n][i-1]][i-1];
		pdep[n][i]=pdep[pidx[n][i-1]][i-1];
		pnum[n][i]=pnum[pidx[n][i-1]][i-1];
	}
	n++;
}

int dig(int a, int b){
	int aidx, bidx, adep, bdep, atidx, btidx;
	aidx=bsnum(a), bidx=bsnum(b);
	adep=dep[aidx]-num[aidx]+a, bdep=dep[bidx]-num[bidx]+b;

	int s=0, e=adep<bdep?adep:bdep;
	while(s<e){
		int m=(s+e+1)/2;
		atidx=bsdep(m, aidx);
		btidx=bsdep(m, bidx);
		if(atidx==btidx) s=m;
		else e=m-1;
	}
	//printf("{%d %d}\n", bsdep(1, aidx), bsdep(1, bidx));
	//printf("[%d]\n", s);
	int d, idx;
	if(adep<bdep){
		d=s+(bdep-adep)/2;
		idx=bsdep(d, bidx);
	}
	else{
		d=s+(adep-bdep+1)/2;
		idx=bsdep(d, aidx);
	}
	return num[idx]-dep[idx]+d;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 504 KB Output is correct
2 Correct 6 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 545 ms 78708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1908 ms 11304 KB Output is correct
2 Correct 471 ms 50936 KB Output is correct
3 Correct 413 ms 50868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 773 ms 51036 KB Output is correct
2 Correct 788 ms 6708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2848 ms 50980 KB Output is correct
2 Correct 1098 ms 50996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 435 ms 13964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 827 ms 50936 KB Output is correct
2 Correct 515 ms 97376 KB Output is correct
3 Correct 285 ms 97400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1856 ms 50828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2108 ms 50964 KB Output is correct