이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <vector>
#include <string>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <list>
using namespace std;
typedef long long ll;
ll n;
struct segtree{
vector<pair<ll,ll>> tree;
segtree(): tree(808080){}
void init(ll node, ll s, ll e, vector<ll> &v){
if(s==e)tree[node] = {v[s],s};
else{
ll mid = s+e>>1;
init(node<<1,s,mid,v); init(node<<1|1,mid+1,e,v);
tree[node] = max(tree[node<<1],tree[node<<1|1]);
}
}
pair<ll,ll> query(ll node, ll s, ll e, ll l, ll r){
if(l>r)return {-1,-1};
if(e<l or r<s)return {0,0};
if(l<=s and e<=r)return tree[node];
ll mid = s+e>>1;
return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
}
pair<ll,ll> Q(ll l, ll r){ return query(1,1,n,l,r); }
} seg;
vector<ll> v;
ll L[202020][22];
ll R[202020][22];
ll M[202020][22];
void init(int N, std::vector<int> H) {
n=N;
v.push_back(0);
for(int i = 1 ; i <= n ; i++)v.push_back(H[i-1]);
seg.init(1,1,n,v);
stack<ll> st;
for(int i = 1 ; i <= n ; i++){
while(st.size() and v[st.top()] < v[i]){
R[st.top()][0] = i;
st.pop();
}
st.push(i);
}
while(st.size())st.pop();
for(int i = n ; i >= 1 ; i--){
while(st.size() and v[st.top()] < v[i]){
L[st.top()][0] = i;
st.pop();
}
st.push(i);
}
for(int i = 1 ; i <= n ; i++){
M[i][0] = (v[L[i][0]] > v[R[i][0]] ? L[i][0] : R[i][0]);
}
for(int j = 1 ; j <= 20 ; j++){
for(int i = 1 ; i <= n ; i++){
L[i][j] = L[L[i][j-1]][j-1];
R[i][j] = R[R[i][j-1]][j-1];
M[i][j] = M[M[i][j-1]][j-1];
}
}
}
ll AB, BC, CD;
ll c;
pair<ll,ll> move_R(ll x){ //몇번 이동해야 좌표가 C 이상인가?
ll ret = 0;
for(int i = 20 ; i >= 0 ; i--){
if(R[x][i]<=0)continue;
if(R[x][i] < c)ret += (1<<i), x = R[x][i];
}
return {ret+1,R[x][0]};
}
int minimum_jumps(int A, int B, int C, int D) {
A++; B++; C++; D++;
c=C;
AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
ll ret = 1e9;
if(v[B] < BC){ //BC 미만인 가장 큰놈부터
ll t = seg.Q(B+1,C-1).second;
ll res = 0;
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(L[cur][i]>=A and v[L[cur][i]] < v[t])cur = L[cur][i];
}
for(int i = 20 ; i >= 0 ; i--){
if(M[cur][i]<=0)continue;
if(v[M[cur][i]]<v[t] and R[cur][i]>0 and R[cur][i]<t)cur = M[cur][i], res += (1<<i);
}
for(int i = 20 ; i >= 0 ; i--){
if(R[cur][i]<=0)continue;
if(R[cur][i]<t)cur = R[cur][i], res += (1<<i);
}
res++; cur = R[cur][0];
if(cur==t)ret = min(ret, res+1);
}
if(AB>BC){ //BC 초과인 가장 작은놈부터
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(v[L[cur][i]] < BC)cur = L[cur][i];
}
if(v[cur]<BC)cur = L[cur][0];
assert(cur>=A);
auto [res,e] = move_R(cur);
if(e>=C and e<=D){
ret = min(ret, res);
}
}
if(AB<BC){ //제일 큰놈
ll cur = B;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(L[cur][i]>=A)cur = L[cur][i];
}
ll res = 0;
for(int i = 20 ; i >= 0 ; i--){
if(L[cur][i]<=0)continue;
if(v[L[cur][i]] <= BC)res += (1<<i), cur = L[cur][i];
}
res++;
cur = L[cur][0];
auto [res2,e] = move_R(cur);
if(e>=C and e<=D){
ret = min(ret, res+res2);
}
}
if(ret==1e9)ret = -1;
return ret;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In member function 'void segtree::init(ll, ll, ll, std::vector<long long int>&)':
jumps.cpp:26:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | ll mid = s+e>>1;
| ~^~
jumps.cpp: In member function 'std::pair<long long int, long long int> segtree::query(ll, ll, ll, ll, ll)':
jumps.cpp:35:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | ll mid = s+e>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |