Submission #843687

# Submission time Handle Problem Language Result Execution time Memory
843687 2023-09-04T12:22:43 Z jmyszka2007 Railway Trip 2 (JOI22_ho_t4) C++17
0 / 100
49 ms 3672 KB
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x) 
#endif
typedef long long ll;
#define pi pair<int, int>
#define pl pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr int LIM = 1e5;
constexpr int lg = 2;
constexpr int base = (1 << 3);
int nxtl[LIM + 10][lg];
int nxtr[LIM + 10][lg];
int trimx[2 * base][lg];
int trimn[2 * base][lg];
int trimx2[2 * base];
int trimn2[2 * base];
void updmx(int v, int x, int k) {
	v += base;
	while(v > 0) {
		trimx[v][k] = max(trimx[v][k], x);
		v /= 2;
	} 
}
int querymx(int l, int r, int k) {
	l += base;
	r += base;
	int ans = 0;
	while(l <= r) {
		if(l & 1) {
			ans = max(ans, trimx[l][k]);
		}
		if(!(r & 1)) {
			ans = max(ans, trimx[r][k]);
		}
		l = (l + 1) / 2;
		r = (r - 1) / 2;
	}
	return ans;
}
void updmn(int v, int x, int k) {
	v += base;
	while(v > 0) {
		trimn[v][k] = min(trimn[v][k], x);
		v /= 2;
	} 
}
int querymn(int l, int r, int k) {
	l += base;
	r += base;
	int ans = 1e9;
	while(l <= r) {
		if(l & 1) {
			ans = min(ans, trimn[l][k]);
		}
		if(!(r & 1)) {
			ans = min(ans, trimn[r][k]);
		}
		l = (l + 1) / 2;
		r = (r - 1) / 2;
	}
	return ans;
}
void updmx2(int l, int r, int x) {
	l += base;
	r += base;
	while(l <= r) {
		if(l & 1) {
			trimx2[l] = max(trimx2[l], x);
		}
		if(!(r & 1)) {
			trimx2[r] = max(trimx2[r], x);
		}
		l = (l + 1) / 2;
		r = (r - 1) / 2;
	}
}
int querymx2(int v) {
	v += base;
	int ans = 0;
	while(v > 0) {
		ans = max(ans, trimx2[v]);
		v /= 2;
	}
	return ans;
}
void updmn2(int l, int r, int x) {
	l += base;
	r += base;
	while(l <= r) {
		if(l & 1) {
			trimn2[l] = min(trimn2[l], x);
		}
		if(!(r & 1)) {
			trimn2[r] = min(trimn2[r], x);
		}
		l = (l + 1) / 2;
		r = (r - 1) / 2;
	}
}
int querymn2(int v) {
	v += base;
	int ans = 1e9;
	while(v > 0) {
		ans = min(ans, trimn2[v]);
		v /= 2;
	}
	return ans;
}
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	for(int i = 0; i < 2 * base; i++) {
		trimn2[i] = 1e9;
		for(int j = 0; j < lg; j++) {
			trimn[i][j] = 1e9;
		}
	}
	for(int i = 0; i <= LIM; i++) {
		for(int j = 0; j < lg; j++) {
			nxtl[i][j] = 1e9;
		}
	}
	int n, k;
	cin >> n >> k;
	int m;
	cin >> m;
	for(int i = 1; i <= m; i++) {
		int l, r;
		cin >> l >> r;
		if(l > r) {
			updmn2(max(l - k + 1, r + 1), l, r);
		}
		else {
			updmx2(l, min(l + k - 1, r - 1), r);
		}
	}
	for(int i = 1; i <= n; i++) {
		nxtl[i][0] = min(i, querymn2(i));
		nxtr[i][0] = max(i, querymx2(i));
		debug(i, nxtl[i][0], nxtr[i][0]);
		updmn(i, nxtl[i][0], 0);
		updmx(i, nxtr[i][0], 0);
	}
	for(int j = 1; j < lg; j++) {
		debug(j);
		for(int i = 1; i <= n; i++) {
			nxtl[i][j] = querymn(nxtl[i][j - 1], nxtr[i][j - 1], j - 1);
			nxtr[i][j] = querymx(nxtl[i][j - 1], nxtr[i][j - 1], j - 1);
			debug(i, nxtl[i][j], nxtr[i][j]);
			updmn(i, nxtl[i][j], j);
			updmx(i, nxtr[i][j], j);
		}
	}
	int t;
	cin >> t;
	while(t--) {
		int l, r;
		cin >> l >> r;
		if(l == r) {
			cout << 0 << '\n';
			continue;
		}
		int aktl = l, aktr = l;
		int ans = 0;
		for(int i = lg - 1; i >= 0; i--) {
			debug(i, aktl, aktr, querymn(aktl, aktr, i));;
			if(l > r && querymn(aktl, aktr, i) > r) {
				ans += (1 << i);
				int naktl = querymn(aktl, aktr, i);
				int naktr = querymx(aktl, aktr, i);
				aktl = naktl;
				aktr = naktr;
			}
			if(l < r && querymx(aktl, aktr, i) < r) {
				ans += (1 << i);
				int naktl = querymn(aktl, aktr, i);
				int naktr = querymx(aktl, aktr, i);
				aktl = naktl;
				aktr = naktr;
			}
		}
		if(l > r && querymn(aktl, aktr, 0) <= r) {
			cout << ans + 1 << '\n';
		}
		else if(l < r && querymx(aktl, aktr, 0) >= r) {
			cout << ans + 1 << '\n';
		}
		else {
			cout << -1 << '\n';
		}
	}
}
int main() {
	fastio();
	int t;
	//cin >> t;
	t = 1;	
	while(t--) {
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 2136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 2136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 3672 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 2124 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 49 ms 3660 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 2136 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -