답안 #566886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
566886 2022-05-23T05:33:41 Z amunduzbaev 밀림 점프 (APIO21_jumps) C++17
0 / 100
373 ms 71412 KB
#include "jumps.h"

#ifndef EVAL
#include "stub.cpp"
#endif

#include "bits/stdc++.h"
using namespace std;

const int B = 205;
const int N = 2e5 + 5;
const int lg = 18;
int mx[N][lg], mn[N][lg], h[N], pos[N];
int lx[N], rx[N], prec[N/B][N];

struct ST{
	vector<int> tree[N << 2];
	int mx[N << 2];
	void build(int lx = 0, int rx = N, int x = 1){
		if(lx == rx) { tree[x].push_back(h[lx]); return; }
		int m = (lx + rx) >> 1;
		build(lx, m, x << 1);
		build(m + 1, rx, x << 1 | 1);
		tree[x].insert(tree[x].end(), tree[x<<1].begin(), tree[x<<1].end());
		tree[x].insert(tree[x].end(), tree[x<<1|1].begin(), tree[x<<1|1].end());
		sort(tree[x].begin(), tree[x].end());
		mx[x] = max(mx[x<<1], mx[x<<1|1]);
	}
	
	int get_(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(mx[x] <= v) return -1;
			if(lx == rx) return lx;
			int m = (lx + rx) >> 1;
			if(mx[x << 1 | 1] > v) return get_(l, r, v, m + 1, rx, x<<1 | 1);
			else return get_(l, r, v, lx, m, x << 1);
		} int m = (lx + rx) >> 1;
		int R = get_(l, r, v, m + 1, rx, x << 1 | 1);
		if(~R) return R;
		return get_(l, r, v, lx, m, x << 1);
	}
	
	int get(int l, int r, int v, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r) return mx[x];
		int m = (lx + rx) >> 1;
		return max(get(l, r, v, lx, m, x<<1), get(l, r, v, m+1, rx, x<<1|1));
	}
}tree;

void init(int n, vector<int> H){
	for(int i=0;i<n;i++){
		h[i] = H[i];
		pos[h[i]] = i;
	}
	vector<int> ss;
	for(int i=0;i<n;i++){
		while(!ss.empty() && h[ss.back()] < h[i]) ss.pop_back();
		if(!ss.empty()) lx[i] = ss.back();
		else lx[i] = n;
		ss.push_back(i);
	} ss.clear();
	for(int i=n-1;~i;i--){
		while(!ss.empty() && h[ss.back()] < h[i]) ss.pop_back();
		if(!ss.empty()) rx[i] = ss.back();
		else rx[i] = n;
		ss.push_back(i);
	} ss.clear();
	h[n] = N;
	tree.build();
	for(int i=0;i<n;i++){
		if(h[lx[i]] < h[rx[i]]){
			swap(lx[i], rx[i]);
		} 
		mn[i][0] = rx[i];
		mx[i][0] = lx[i];
	}
	mn[n][0] = mx[n][0] = n;
	//~ for(int i=0;i<n;i++) cout<<mn[i][0]<<" ";
	//~ cout<<"\n";
	//~ for(int i=0;i<n;i++) cout<<mx[i][0]<<" ";
	//~ cout<<"\n";
	
	for(int j=1;j<lg;j++){
		for(int i=0;i<=n;i++){
			mn[i][j] = mn[mn[i][j-1]][j-1];
			mx[i][j] = mx[mx[i][j-1]][j-1];
		}
	}
	
	
}

int check(int a, int c){
	int res = 0;
	for(int i=lg-1;~i;i--){
		if(h[mx[a][i]] <= h[c]){
			//~ cout<<mx[a][i]<<" "<<h[mx[a][i]]<<" "<<h[c]<<"\n";
			//~ cout<<i<<"\n";
			a = mx[a][i], res += (1 << i);
		}
	}
	for(int i=lg-1;~i;i--){
		if(h[mn[a][i]] <= h[c]){
			//~ cout<<mx[a][i]<<" "<<h[mx[a][i]]<<" "<<h[c]<<"\n";
			a = mn[a][i], res += (1 << i);
		}
	}
	
	if(a != c) return -1;
	return res;
}

int minimum_jumps(int a, int b, int c, int d) {
	//~ int res = N;
	int l = tree.get_(a, b, h[c]) + 1;
	if(l > b) return -1;
	a = tree.get(max(a, l), b, h[c]);
	return check(pos[a], c);
}

/*

7 2
3 2 1 6 4 5 7
4 4 6 6
0 1 2 2

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 44676 KB Output is correct
2 Correct 96 ms 44644 KB Output is correct
3 Incorrect 258 ms 71412 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 89 ms 44556 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 89 ms 44556 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 107 ms 44660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 44676 KB Output is correct
2 Correct 99 ms 44604 KB Output is correct
3 Correct 105 ms 44692 KB Output is correct
4 Incorrect 373 ms 59252 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 44676 KB Output is correct
2 Correct 99 ms 44604 KB Output is correct
3 Correct 105 ms 44692 KB Output is correct
4 Incorrect 373 ms 59252 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 44676 KB Output is correct
2 Correct 96 ms 44644 KB Output is correct
3 Incorrect 258 ms 71412 KB Output isn't correct
4 Halted 0 ms 0 KB -