회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
12795번 - 반평면 땅따먹기
kjp4155
C++14
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <memory.h> #include <math.h> #include <assert.h> #include <queue> #include <map> #include <set> #include <string> #include <algorithm> #include <functional> #include <vector> #include <stack> #include <unordered_map> #include <random> using namespace std; typedef long long ll; typedef pair<ll, ll> Line; struct LiChaoTree{ ll f(Line l, ll x){ return l.first * x + l.second; } struct Node{ int lnode, rnode; ll xl, xr; Line l; }; vector<Node> nodes; void init(ll xmin, ll xmax){ nodes.push_back({-1,-1,xmin,xmax,{0,-1e18}}); } void insert(int n, Line newline){ ll xl = nodes[n].xl, xr = nodes[n].xr; ll xm = (xl + xr) >> 1; // 구간의 왼쪽 끝 기준으로 llow, lhigh를 결정한다 Line llow = nodes[n].l, lhigh = newline; if( f(llow, xl) >= f(lhigh,xl) ) swap(llow, lhigh); // 1. 한 직선이 다른 직선보다 항상 위에 있는 경우 if( f(llow, xr) <= f(lhigh, xr) ){ nodes[n].l = lhigh; return; } // 2-a. 교점이 구간의 오른쪽 절반에 존재하는 경우 // lhigh를 저장하고 오른쪽 노드로 llow를 이용해 재귀호출 else if( f(llow, xm) <= f(lhigh, xm) ){ nodes[n].l = lhigh; if( nodes[n].rnode == -1 ){ nodes[n].rnode = nodes.size(); nodes.push_back({-1,-1,xm+1,xr,{0,-1e18}}); } insert(nodes[n].rnode, llow); } // 2-b. 교점이 구간의 왼쪽 절반에 존재하는 경우 // llow를 저장하고 왼쪽 노드로 lhigh를 이용해 재귀호출 else{ nodes[n].l = llow; if( nodes[n].lnode == -1 ){ nodes[n].lnode = nodes.size(); nodes.push_back({-1,-1,xl,xm,{0,-1e18}}); } insert(nodes[n].lnode, lhigh); } } ll get(int n, ll xq){ if( n == -1 ) return -1e18; ll xl = nodes[n].xl, xr = nodes[n].xr; ll xm = (xl + xr) >> 1; if( xq <= xm ) return max(f(nodes[n].l, xq), get(nodes[n].lnode, xq)); else return max(f(nodes[n].l, xq), get(nodes[n].rnode, xq)); } }; int main() { LiChaoTree tree; tree.init(-2e12, 2e12); int Q; scanf("%d",&Q); for(int q=0;q<Q;q++){ ll op, a, b; scanf("%lld",&op); if( op == 1 ){ scanf("%lld%lld",&a,&b); tree.insert(0, {a,b}); } if( op == 2 ){ scanf("%lld",&a); printf("%lld\n",tree.get(0, a)); } } }
결과
메모리
시간
코드 길이
맞았습니다!!
9684
196
2744