Submission #567358

# Submission time Handle Problem Language Result Execution time Memory
567358 2022-05-23T10:53:16 Z amunduzbaev Rainforest Jumps (APIO21_jumps) C++17
Compilation error
0 ms 0 KB
#include "jumps.h"

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

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

const int B = 1000;
const int N = 2e5 + 5;
const int M = N / B + 2;
const int lg = 18;
vector<int> edges[N];
int mx[N][lg], mn[N][lg], h[N], pos[N];
int st[N][lg], pre[N];
int lx[N], rx[N];

struct ST{
	int tree[N][lg];
	void build(){
		for(int i=0;i<N;i++) tree[i][0] = pre[i];
		for(int j=1;j<lg;j++){
			for(int i=0;i + (1 << (j - 1))<N;i++){
				tree[i][j] = min(tree[i][j-1], tree[i + (1 << (j - 1))][j-1]);
			}
		}
	}
	
	int get(int l, int r){
		int lg = __lg(r - l + 1);
		return min(tree[l][lg], tree[r - (1 << lg) + 1][lg]);
	}
}tree[M];

void init(int n, vector<int> H){
	for(int i=0;i<n;i++){
		h[i] = H[i];
		pos[h[i]] = i;
		st[i][0] = h[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;
	
	for(int i=0;i<n;i++){
		if(h[lx[i]] < h[rx[i]]){
			swap(lx[i], rx[i]);
		} 
		edges[lx[i]].push_back(i);
		edges[rx[i]].push_back(i);
		mn[i][0] = rx[i];
		mx[i][0] = lx[i];
		lx[i] = min(lx[i], rx[i]);
		if(i < lx[i]) lx[i] = -1;
	}
	mn[n][0] = mx[n][0] = n;
	edges[n].clear();
	
	for(int b=0;b * B<n;b++){
		memset(pre, 127, sizeof pre);
		queue<int> q;
		for(int i=0;b * B + i<n && i<B;i++){
			q.push(b * B + i);
			pre[b * B + i] = 0;
		}
		
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(auto x : edges[u]){
				if(pre[x] > pre[u] + 1){
					pre[x] = pre[u] + 1;
					q.push(x);
				}
			}
		}
		
		tree[b].build();
	}
	
	for(int j=1;j<lg;j++){
		for(int i=0;i<=n;i++){
			if(i + (1 << (j - 1)) < n) st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j-1]);
			mn[i][j] = mn[mn[i][j-1]][j-1];
			mx[i][j] = mx[mx[i][j-1]][j-1];
		}
	}
}

int get(int l, int r){
	int lg = __lg(r - l + 1);
	return max(st[l][lg], st[r - (1 << lg) + 1][lg]);
}

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 N;
	return res;
}

int minimum_jumps(int a, int b, int c, int d) {
	int bl = c / B, br = d / B;
	int res = N;
	for(int j=bl+1;j<br;j++){
		//~ assert(false);
		res = min(res, tree[j].get(a, b));
	}
	
	auto calc = [&](int l, int r){
		for(int i=l;i<=r;i++){
			int L = max(lx[i] + 1, a);
			if(L <= b){
				int mx = pos[get(L, b)];
				res = min(res, check(mx, i));
			}
		}
	};
	
	if(bl == br){
		calc(c, d);
	} else {
		if(c % B == 0) res = min(res, tree[bl].get(a, b));
		else calc(c, (bl + 1) * B - 1);
		if((d + 1) % B == 0) res = min(res, tree[br].get(a, b));
		else calc(br * B, d);
	}
	
	if(res == N) return -1;
	return res;
}

/*

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

*/

Compilation message

/tmp/ccB7TTgD.o: in function `__tcf_0':
jumps.cpp:(.text+0x9): relocation truncated to fit: R_X86_64_PC32 against symbol `edges' defined in .bss section in /tmp/ccB7TTgD.o
/tmp/ccB7TTgD.o: in function `get(int, int)':
jumps.cpp:(.text+0x5e): relocation truncated to fit: R_X86_64_PC32 against symbol `st' defined in .bss section in /tmp/ccB7TTgD.o
/tmp/ccB7TTgD.o: in function `check(int, int)':
jumps.cpp:(.text+0xaa): relocation truncated to fit: R_X86_64_PC32 against symbol `h' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0xc2): relocation truncated to fit: R_X86_64_PC32 against symbol `mx' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0xfe): relocation truncated to fit: R_X86_64_PC32 against symbol `mn' defined in .bss section in /tmp/ccB7TTgD.o
/tmp/ccB7TTgD.o: in function `minimum_jumps(int, int, int, int)':
jumps.cpp:(.text+0x373): relocation truncated to fit: R_X86_64_PC32 against symbol `lx' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0x37f): relocation truncated to fit: R_X86_64_PC32 against symbol `st' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0x3f5): relocation truncated to fit: R_X86_64_PC32 against symbol `pos' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0x412): relocation truncated to fit: R_X86_64_PC32 against symbol `st' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0x46a): relocation truncated to fit: R_X86_64_PC32 against symbol `lx' defined in .bss section in /tmp/ccB7TTgD.o
jumps.cpp:(.text+0x482): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status