<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>AlgoShitPo</title>
    <description>알고리즘 블로그</description>
    <link>http://algoshitpo.github.io/</link>
    <atom:link href="http://algoshitpo.github.io/rss" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 10 Aug 2021 22:26:58 +0900</pubDate>
    <lastBuildDate>Tue, 10 Aug 2021 22:26:58 +0900</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>구사과와 함께하는 PS-PT 2주차</title>
        <description>&lt;h3 id=&quot;intro&quot;&gt;Intro&lt;/h3&gt;

&lt;ul id=&quot;problem-list&quot;&gt;&lt;/ul&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;https://code.jquery.com/jquery-3.1.1.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
  const problem_list = '18339,20297,21811,17366,11738'.split(',');

  function showProblem(id){
    $.ajax({
      type: 'GET',
      url: 'https://solved.ac/api/v3/problem/show?problemId=' + id,
      dataType: 'json',
      timeout: 10000,
      cache: false,
      success: function(json) {
        const title = json['titleKo'];
        const tier = json['level'];
        const img_tag = document.getElementById(`tier-${id}`);
        const a_tag = document.getElementById(`title-${id}`);
        img_tag.src = `https://static.solved.ac/tier_small/${tier}.svg`;
        a_tag.href = `http://icpc.me/${id}`;
        a_tag.innerText = `BOJ${id} ${title}`;
      }
    });
  }
  function showProblems(){
    let inner = '';
    for(const i of problem_list){
      inner += `&lt;li&gt;&lt;img id='tier-${i}' width='15px' src='#'&gt; &lt;a id='title-${i}' target='_blank' href='#'&gt;&lt;/a&gt;&lt;/li&gt;\n`;
    }
    document.getElementById('problem-list').innerHTML = inner;
    for(const i of problem_list){
      showProblem(i);
    }
  }
  showProblems();
&lt;/script&gt;

&lt;h3 id=&quot;greedy-termite-icpc-tehran-site-2019&quot;&gt;Greedy Termite (ICPC Tehran Site 2019)&lt;/h3&gt;

&lt;p&gt;다음으로 갈 위치를 빠르게 구할 수 있으면 문제를 풀 수 있습니다. 현재 $i$번째 막대기에 있다고 할 때, 다음으로 갈 막대기 $j$의 조건은 다음과 같습니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$H_j - \vert X_i - X_j \vert$가 최대&lt;/li&gt;
  &lt;li&gt;$\vert X_i - X_j \vert$가 최소&lt;/li&gt;
  &lt;li&gt;$j$가 최소&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;절댓값 기호는 사회악이므로 $X_i$와 $X_j$의 대소 관계에 따라 식을 조건을 다시 정리해봅시다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$X_j &amp;lt; X_i$인 경우 (왼쪽으로 이동하는 경우)
    &lt;ol&gt;
      &lt;li&gt;$H_j + X_j - X_i$가 최대&lt;/li&gt;
      &lt;li&gt;$X_i - X_j$가 최소&lt;/li&gt;
      &lt;li&gt;$j$가 최소&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;$X_j &amp;gt; X_i$인 경우 (오른쪽으로 이동하는 경우)
    &lt;ol&gt;
      &lt;li&gt;$H_j - X_j + X_i$가 최대&lt;/li&gt;
      &lt;li&gt;$X_j - X_i$가 최소&lt;/li&gt;
      &lt;li&gt;$j$가 최소&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;이때, $i$는 상수라고 생각할 수 있으므로 $j$에 대한 항만 최대화/최소화하면 됩니다. 그러므로 왼쪽으로 이동하는 경우에는 $H_j + X_j$를 최대화/$-X_j$를 최소화하면 되고, 오른쪽으로 이동하는 경우에는 $H_j - X_j$를 최대화/$X_j$를 최소화하면 됩니다.&lt;/p&gt;

&lt;p&gt;어떤 구간에서 우선순위가 가장 높은 원소를 찾는 연산은 세그먼트 트리를 이용해서 $O(\log N)$ 시간에 처리할 수 있습니다. 두 막대기 간의 우선순위 비교는 상수 시간에 할 수 있으므로 가장 우선순위가 높은 막대기를 $O(\log N)$ 시간에 찾을 수 있고, 전체 문제를 $O(N \log N)$에 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/31e53315e9d146a5811bf3741d601668&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;confuzzle-2020-spc-champion&quot;&gt;Confuzzle (2020 SPC Champion)&lt;/h3&gt;

&lt;p&gt;다양한 풀이가 존재합니다. 이 글에서 소개하는 4가지 풀이 외에도 다양한 풀이가 존재할 수 있습니다.&lt;/p&gt;

&lt;h4 id=&quot;sol-1-센트로이드-분할&quot;&gt;Sol 1. 센트로이드 분할&lt;/h4&gt;

&lt;p&gt;트리의 모든 경로를 고려하는 문제의 대부분은 센트로이드 디컴포지션을 이용해서 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;이 문제는 끝점이 같은 색깔인 가장 짧은 경로를 구하는 문제이므로, 각 색깔 별로 &lt;strong&gt;“센트로이드와 가장 가까운”&lt;/strong&gt; 경로만 저장하면 됩니다. $O(N \log N)$ 시간에 문제를 풀 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/99677deb8a8f473ebbf1a3ae091e1192&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;sol-2-루트&quot;&gt;Sol 2. 루트&lt;/h4&gt;

&lt;p&gt;Naive한 풀이 2가지는 쉽게 생각할 수 있습니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;색깔이 같은 모든 $x \choose 2$가지 정점 쌍에 대해 거리 구하기&lt;/li&gt;
  &lt;li&gt;각 정점에서 시작하는 Multi-Source BFS로 최소 거리 구하기&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(1)은 정점이 많아질수록 느려지고, (2)는 색깔의 종류가 많아질수록 느려집니다. 정점의 개수와 색깔의 종류에 대해 유동적으로 두 가지 풀이를 선택해서 사용한다면 문제를 풀 수 있다는 생각을 할 수 있습니다.&lt;/p&gt;

&lt;p&gt;각 색깔마다 정점이 $X$보다 적다면 1번 풀이를, $X$보다 많다면 2번 풀이를 적용한다고 생각해봅시다.&lt;/p&gt;

&lt;p&gt;정점이 $X$개 이하이므로 각 색깔마다 확인할 정점 쌍은 최대 $X^2$가지입니다. LCA를 $O(\log N)$ 시간에 구하면 $O(X^2 \log N)$, $O(1)$에 구하면 $O(X^2)$만큼 걸립니다. 정점이 $X$개씩 $N/X$묶음 주어지는 것이 최악의 경우이고, 이때 시간 복잡도는 $O(NX \log N)$ 혹은 $O(NX)$입니다.&lt;/p&gt;

&lt;p&gt;정점이 $X$개 이상인 색깔은 최대 $N/X$가지 밖에 존재할 수 없습니다. 각 색깔마다 Multi-Source BFS를 수행하는 시간은 $O(N)$이므로 이 경우의 시간 복잡도는 $O(N^2/X)$입니다.&lt;/p&gt;

&lt;p&gt;$NX + N^2/X$를 최소화하는 $X$는 $\sqrt N$입니다. 이때 시간 복잡도는 $O(N \sqrt N)$이 되어서 시간 제한 안에 문제를 해결할 수 있습니다. LCA를 $O(\log N)$에 구하는 경우, $X = \sqrt{\frac{N}{\log N}}$으로 잡으면 $O(N \sqrt{N \log N})$이지만, $X = \sqrt N$으로 잡아서 $O(N \sqrt N \log N)$으로 풀어도 큰 차이는 없습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/7f8edebd779b49c0bf341b2604bc7c39&quot;&gt;$O(N \sqrt{N \log N})$ 코드&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/1f4bcc2da8fd4f369f34d1493780b49d&quot;&gt;$O(N\sqrt N)$ 코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;sol-3-트리-압축&quot;&gt;Sol 3. 트리 압축&lt;/h4&gt;

&lt;p&gt;색깔이 같은 정점끼리 모아서 트리 압축을 합시다. 색깔이 $c$인 정점을 모아서 압축한 트리에서 “현재 정점에서 가장 가까운/두번째로 가까운 색깔이 $c$인 정점”을 구하는 트리 DP를 하면 정답을 구할 수 있습니다.&lt;/p&gt;

&lt;p&gt;트리 압축을 하는데 $O(N \log N)$이 걸리고, 트리 DP는 $O(N)$이므로 $O(N \log N)$에 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/55c0bda17225464584a12d0fa8498d0c&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;sol-4-small-to-large&quot;&gt;Sol 4. Small to Large&lt;/h4&gt;

&lt;p&gt;트리 압축에서의 풀이를 조금 변형하면 색깔 별로 트리를 분리하지 않아도 문제를 풀 수 있습니다.&lt;/p&gt;

&lt;p&gt;트리 압축에서 “현재 정점에서 가장 가까운/두번째로 가까운 색깔이 $c$인 정점”을 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::pair&lt;/code&gt;로 관리했는데, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt;을 사용하면 모든 색깔에 대해 pair를 모두 저장할 수 있습니다. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D[v][c]&lt;/code&gt; : $v$를 루트로 하는 서브 트리에서 $v$와 가장/두번째로 가까운 색깔이 $c$인 정점)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt;을 합칠 때 작은 컨테이너에 있는 원소를 큰 컨테이너로 옮겨주는 방식으로 합치면 원소들이 최대 $O(N \log N)$번 이동하므로 $O(N \log^2 N)$에 문제를 풀 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/570bbd771450466c8bfeaa327e3a6c18&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;united-cows-of-farmer-john-usaco-2021-us-open-platinum&quot;&gt;United Cows of Farmer John (USACO 2021 US Open Platinum)&lt;/h3&gt;

&lt;p&gt;USACO Platinum 문제로 출제된 문제입니다. 이 문제의 풀이를 알아보기 전에, Gold에 출제된 &lt;a href=&quot;https://www.acmicpc.net/problem/21814&quot;&gt;동일한 이름의 문제&lt;/a&gt;를 먼저 풀어봅시다. 리더를 2마리 뽑는다는 점이 다릅니다.&lt;/p&gt;

&lt;p&gt;소를 차례대로 보면서, $i$번째 소가 두 번째 리더가 되는 경우의 수를 각각 구해서 더할 것입니다. 즉, $i$번째 소가 두 번째 리더일 때 첫 번째 리더가 될 수 있는 소의 수를 구하면 됩니다.&lt;br /&gt;$i$번째 소가 두 번째 리더가 된다고 했을 때, 첫 번째 리더가 될 수 없는 소를 소거하는 방식으로 진행할 것입니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$i$ 보다 왼쪽에 있는 점 중 품종이 같은 가장 오른쪽에 있는 소를 $last_i$라고 합시다. 리더의 품종은 유일해야 하기 때문에 $last_i$를 포함할 수 없습니다. 즉, $1 \cdots last_i$는 리더가 될 수 없습니다.&lt;/li&gt;
  &lt;li&gt;각 품종 별로 가장 최근에 본(아직 누군가의 $last$가 된 적 없는) 소들만 대표가 될 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;아직 누군가의 $last$가 된 적 없는 소를 active 상태라고 하면, $i$마다 구간 $[last_i+1, i-1]$에서 active된 소의 수를 구하는 문제가 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/qw3ntrH.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/305eaaf943034cbe9e4ba0154f4ba4a5&quot;&gt;Gold 문제 코드&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;다시 원래 문제, 즉 리더 3마리를 선택하는 문제로 돌아옵시다. 소를 차례대로 보면서 $i$가 세 번째 리더가 될 수 있는 경우의 수를 센다는 풀이의 방향은 그대로 유지합니다. 하지만 $i$가 세 번째 리더일 때 가능한 &lt;strong&gt;“(첫 번째 리더, 두 번째 리더) 순서쌍의 수”&lt;/strong&gt;를 직접 세는 것은 어려워 보입니다.&lt;/p&gt;

&lt;p&gt;세 번째 리더를 고정한 것처럼, 한 마리를 더 고정해봅시다. 가운데 리더를 고정하는 것보다는 첫 번째 리더를 고정하는 것이 편해보이므로, $X_i(j)$를 $i$가 세 번째 리더 / $j$가 첫 번째 리더일 때 가능한 두 번째 리더의 수라고 정의해봅시다. Gold 문제와 마찬가지로, 두 번째 대표가 될 수 있는 조건은 $last_i$보다 뒤에 있고, 아직 누군가의 $last$가 된 적 없는(active 상태인) 소입니다.&lt;/p&gt;

&lt;p&gt;$X_{i-1}(j) \rightarrow X_{i}(j)$의 상태 전이만 빠르게 계산할 수 있다면, 세그먼트 트리를 이용해 $X_i(last_i+1) \cdots X_i(i-1)$의 합을 구하는 것으로 문제를 풀 수 있습니다. 구체적으로 어떤 값이 바뀌는지 생각해봅시다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$last_i$와 $i$의 active 여부가 변경되었으니 이 정보를 갱신해야 합니다. (Point Update 2번)&lt;/li&gt;
  &lt;li&gt;$last_i$는 더 이상 두 번째 소가 될 수 없으므로 구간 $[last_{last_i}, last_i-1]$의 값을 1 감소시켜야 합니다. (Range Update 1번)&lt;/li&gt;
  &lt;li&gt;$i$번째 소를 본 이후부터는 $i$가 두 번째 소가 될 수 있으므로 구간 $[Last_i+1, i-1]$의 값을 1 증가시켜야 합니다. (Range Update 1번)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;이 연산들은 세그먼트 트리와 레이지 프로퍼게이션을 이용해서 $O(\log N)$에 처리할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/9725367c71384760b892434a72020fbf&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;-ucpc-2019-qualification&quot;&gt;% (UCPC 2019 Qualification)&lt;/h3&gt;

&lt;p&gt;올바른 괄호 문자열은 &lt;strong&gt;“괄호가 중첩되어 있다는 점”&lt;/strong&gt;을 활용해 트리 구조로 나타낼 수 있습니다. 이 문제 역시 트리의 관점에서 바라볼 수 있으며, 이를 활용한 풀이는 &lt;a href=&quot;https://drive.google.com/file/d/1lEkJ4sW5s2bD8SXHh2nYVp8MgXf2nkNg/view&quot;&gt;UCPC 2019 공식 풀이 슬라이드 61페이지&lt;/a&gt;에 자세히 나와있습니다.&lt;br /&gt;이 글에서는 공식 풀이와 다른 관점에서 바라본 풀이를 소개합니다.&lt;/p&gt;

&lt;p&gt;길이가 13인 괄호 문자열 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.(.()..(.).).&lt;/code&gt;는 아래 그림처럼 정점 13개, 가중치가 1인 간선 12개(검은색 간선), 가중치가 2인 간선 3개(보라색 간선)으로 나타낼 수 있습니다. 이때 파란색 괄호(열고 닫는 괄호가 인접한 쌍)를 연결하는 보라색 간선은 만들지 않아도 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/mfKt6EN.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;아래 그림처럼 가중치가 INF인 간선을 몇 개 추가하면 $N$각형을 삼각분할한 형태로 바꿀 수 있습니다. 즉, &lt;a href=&quot;https://www.acmicpc.net/problem/11738&quot;&gt;NEERC 2015 Distance on Triangulation&lt;/a&gt;과 동일한 문제로 바꿀 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/inqpcRT.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;distance-on-triangulation-neerc-2015&quot;&gt;Distance on Triangulation (NEERC 2015)&lt;/h4&gt;

&lt;p&gt;계산 기하를 공부하다보면 삼각분할과 함께 붙어다니는 &lt;strong&gt;“듀얼 트리(Dual Tree)”&lt;/strong&gt;를 볼 수 있습니다. &lt;a href=&quot;/2020/03/23/dual/&quot;&gt;평면 그래프의 듀얼 그래프&lt;/a&gt;와 비슷하게, 각 삼각형을 정점으로, 인접한 삼각형을 간선으로 연결한 트리를 듀얼 트리라고 합니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/BRmEZOA.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;각 정점의 차수가 최대 3이기 때문에 Centroid Decomposition과의 궁합도 좋습니다. 이 문제도 Centroid Decomposition을 이용해서 해결할 수 있습니다. Dual Tree에서 Centroid를 잡고, Centroid에 해당하는 삼각형을 지나는 모든 쿼리를 오프라인으로 처리합니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/YsmuLum.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;작성 예정…&lt;/p&gt;
</description>
        <pubDate>Tue, 10 Aug 2021 22:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2021/08/10/koopspt2/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2021/08/10/koopspt2/</guid>
        
        
      </item>
    
      <item>
        <title>구사과와 함께하는 PS-PT 1주차</title>
        <description>&lt;h3 id=&quot;intro&quot;&gt;Intro&lt;/h3&gt;

&lt;p&gt;justiceHui와 Ryute가 같이 작성했습니다.&lt;/p&gt;

&lt;ul id=&quot;problem-list&quot;&gt;&lt;/ul&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;https://code.jquery.com/jquery-3.1.1.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
  const problem_list = '17667,2919,18842,22031'.split(',');

  function showProblem(id){
    $.ajax({
      type: 'GET',
      url: 'https://solved.ac/api/v3/problem/show?problemId=' + id,
      dataType: 'json',
      timeout: 10000,
      cache: false,
      success: function(json) {
        const title = json['titleKo'];
        const tier = json['level'];
        const img_tag = document.getElementById(`tier-${id}`);
        const a_tag = document.getElementById(`title-${id}`);
        img_tag.src = `https://static.solved.ac/tier_small/${tier}.svg`;
        a_tag.href = `http://icpc.me/${id}`;
        a_tag.innerText = `BOJ${id} ${title}`;
      }
    });
  }
  function showProblems(){
    let inner = '';
    for(const i of problem_list){
      inner += `&lt;li&gt;&lt;img id='tier-${i}' width='15px' src='#'&gt; &lt;a id='title-${i}' target='_blank' href='#'&gt;&lt;/a&gt;&lt;/li&gt;\n`;
    }
    document.getElementById('problem-list').innerHTML = inner;
    for(const i of problem_list){
      showProblem(i);
    }
  }
  showProblems();
&lt;/script&gt;

&lt;h3 id=&quot;virus-experiment-joioc-19-3번&quot;&gt;Virus Experiment (JOIOC 19 3번)&lt;/h3&gt;

&lt;h4 id=&quot;subtask-2-6점&quot;&gt;Subtask 2 (6점)&lt;/h4&gt;

&lt;p&gt;적절한 전처리를 통해 $(r, c)$가 감염되기 위해서 이웃들이 감염되어야 하는 조합을 $O(16RC)$ 시간에 모두 구할 수 있습니다. (ex. 이웃이 감염된 상태가 $\left{ N, SW, WE, SE \right}$ 중 하나 이상을 포함하면 $(r, c)$도 감염됨)&lt;/p&gt;

&lt;p&gt;위와 같은 전처리를 모두 수행했다면, $U_{r,c} \neq 0$인 모든 $(r, c)$에 대해 BFS 등을 이용해서 시뮬레이션을 하면 $O(R^2C^2)$ 시간에 문제를 풀 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://oj.uz/submission/451576&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;subtask-3-100점&quot;&gt;Subtask 3 (100점)&lt;/h4&gt;

&lt;p&gt;깔끔한 풀이와 멋진 풀이가 있습니다. 차례대로 소개합니다.&lt;/p&gt;

&lt;h5 id=&quot;subtask-3---randomize-solution&quot;&gt;Subtask 3 - Randomize Solution&lt;/h5&gt;

&lt;p&gt;Subtask 2는 &lt;strong&gt;“완전 탐색”&lt;/strong&gt;이기 때문에 시간이 오래 걸립니다. 즉, 탐색하는 횟수를 줄이면 실행 시간도 줄일 수 있습니다. 탐색 횟수를 줄일 수 있는 방법을 생각해봅시다.&lt;/p&gt;

&lt;p&gt;만약 &lt;strong&gt;“u가 감염되면 v도 감염된다”&lt;/strong&gt;는 관계를 단방향 간선 $(u, v)$로 표현한 그래프를 만들 수 있다면, outdegree가 0인 가장 작은 SCC와 그러한 SCC의 개수가 문제의 정답이 될 것입니다. 결국 우리가 정답을 구하기 위해 찾아야 하는 것은 사이클인데, 만약 완전 탐색을 한다면 크기가 $X$인 &lt;strong&gt;“한 개의 사이클”&lt;/strong&gt;을 $X$번 탐색하므로 효율적이지 않습니다. 같은 정점들로 구성된 사이클은 되도록 중복해서 탐색하는 일이 없도록 하는 방법을 찾으면 됩니다.&lt;/p&gt;

&lt;p&gt;그 방법은 생각보다 간단합니다. Subtask 2의 풀이를 그대로 사용하되, 시작 지점이 $(r, c)$인 시뮬레이션 과정에서 $(i, j) &amp;lt; (r, c)$인 $(i, j)$를 방문한다면 즉시 시뮬레이션을 중단합니다. 이렇게 하면 한 사이클 전체를 다 보는 경우는 &lt;strong&gt;“좌표가 사전순으로 가장 작은 지점에서 시작하는 시뮬레이션”&lt;/strong&gt; 한 번 밖에 없습니다.&lt;/p&gt;

&lt;p&gt;하지만 이렇게 커팅을 해도 여전히 $O(R^2C^2)$가 되는 경우가 존재합니다.&lt;br /&gt;
이 커팅이 통하지 않는 데이터는 $(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow \cdots \rightarrow (1, C) \rightarrow \cdots \rightarrow (1, 1)$처럼 사이클에 속한 정점의 대부분이 오름차순으로 배열된 상태일 것입니다. 이 데이터의 경우, 시뮬레이션 진행 횟수는 $C, C-1, C-2, \cdots , 1$처럼 되어서 총 수행 횟수가 제곱 스케일로 증가하게 됩니다. 기존에 비해 상수 /2가 붙긴 하겠지만요.&lt;/p&gt;

&lt;p&gt;이것을 피하는 방법은 매우 간단합니다. 정점의 순서를 섞어주면 됩니다! 구체적으로, $U_{r,c} \neq 0$인 모든 $(r, c)$를 모아서 &lt;strong&gt;무작위로 섞은 순열&lt;/strong&gt;을 $A$라고 합시다. 시작 지점이 $A_i$일 때 BFS 도중에 $j &amp;lt; i$인 $A_j$를 방문한다면 즉시 탐색을 중단하는 식으로 구현을 하면 위에서 본 반례를 피할 수 있습니다.&lt;/p&gt;

&lt;p&gt;이상한 야매 풀이같지만 &lt;a href=&quot;http://s3-ap-northeast-1.amazonaws.com/data.cms.ioi-jp.org/open-2019/virus/2019-open-virus-sol-en.pdf&quot;&gt;공식 솔루션&lt;/a&gt;에 나와있는 풀이입니다. 시간 복잡도는 $O(RC \log RC)$ 정도가 된다고 하던데, 증명은 안 해봤지만 아마도 &lt;a href=&quot;https://algoshitpo.github.io/2020/03/23/random/&quot;&gt;여기&lt;/a&gt;에 나와있는 “재밌는 인터랙티브 문제”와 관련이 있을 것 같습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://oj.uz/submission/451594&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;subtask-3---graph-theory&quot;&gt;Subtask 3 - Graph Theory&lt;/h5&gt;

&lt;p&gt;방향 그래프 $G = (V, E)$에서 SCC를 구하는 $O(\vert V \vert + \vert E \vert)$ 알고리즘은 잘 알려져 있으므로 그래프를 효율적으로 만드는 방법을 중점적으로 고민해봅시다.&lt;/p&gt;

&lt;p&gt;추후 작성 예정…&lt;/p&gt;

&lt;h3 id=&quot;카드-놀이-coci-0910-contest-2-6번&quot;&gt;카드 놀이 (COCI 09/10 Contest #2 6번)&lt;/h3&gt;

&lt;p&gt;Exchange Argument처럼 그리디하게 순서를 정할 수 있다는 생각이 듭니다. 카드 더미가 두 개만 있다고 가정했을 때, 어떤 카드를 먼저 써야 하는지 알아봅시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;만약 맨 위에 있는 카드가 다르다면, 더 작은 카드를 사용하는 것이 이득입니다.&lt;/li&gt;
  &lt;li&gt;맨 위에 있는 카드가 똑같다면 사전순으로 앞서는 더미의 카드를 사용하는 것이 이득입니다. 최장 공통 접두사를 생각해보면 어렵지 않게 증명할 수 있습니다.
    &lt;ul&gt;
      &lt;li&gt;만약 어떤 카드 더미가 다른 카드 더미의 접두사가 된다면, 카드가 더 많은 카드 더미의 카드를 사용하는 것이 이득입니다.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;앞글자가 제거되는 상황에서 문자열의 사전순 비교를 하는 것은 결국 모든 문자열의 suffix들을 정렬한 결과를 알고 있으면 $O(1)$에 할 수 있습니다. 이는 Suffix Array를 통해 할 수 있습니다. 모든 카드 더미를 순서대로 이어붙입시다. 그리고 나서 SA를 만들면 합친 문자열의 각 Suffix의 Prefix가 각 카드 더미의 Suffix가 되므로(말이 어렵지만 결국 우리가 비교할 때 사용하는 하나의 카드 더미의 Prefix가 합친 문자열의 Suffix에 일대일로 대응된다는 것입니다), 서로 다른 두 카드 더미의 Suffix의 대소관계를 모두 알 수 있습니다. 각 카드 더미 사이에 구분자로 매우 큰 수를 넣으면 SA의 정렬 결과를 망치지 않으면서 편하게 구현할 수 있습니다.&lt;/p&gt;

&lt;p&gt;따라서 우선순위 큐를 이용해 모든 카드 더미 중 가장 좋은 것을 반복적으로 뽑으면서, 뽑을때마다 첫 한 글자를 제거한(바로 다음 Suffix) 카드 더미로 바꿔서 넣어주면 문제를 해결할 수 있습니다. 원소를 총 $O(\sum \vert S\vert)$번 뽑아야 하고 뽑을 때마다 $O(\log N)$ 시간이 드므로 총 시간복잡도는 Suffix Array를 계산하는 시간 $O(\vert S\vert \log \vert S\vert)$ 또는 $O(\vert S\vert \log^2 \vert S\vert)$에 $O(\vert S\vert \log N)$를 더한 만큼입니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/aac7c898168d40f8893e2fc3518bc0bd&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;별해로, &lt;a href=&quot;https://algoshitpo.github.io/2020/02/09/hashingtechnique/&quot;&gt;해싱&lt;/a&gt;을 이용해 $O(\vert S \vert)$ 전처리를 하면, $O(\log \vert S \vert)$ 시간에 비교를 할 수 있습니다. 그러므로, 우선순위 큐를 이용해 카드 더미를 관리하면 $O(\sum \vert S \vert \log^2 \sum \vert S \vert)$에 문제를 해결할 수 있습니다. 착한 어린이는 해싱을 사용하지 않고 문제를 해결해보도록 노력합시다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/c74e421caa494eebb83dce952284f81d&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;chameleons-love-joisc-1920-day2-1번&quot;&gt;Chameleon’s Love (JOISC 19/20 Day2 1번)&lt;/h3&gt;

&lt;h4 id=&quot;subtask-2-3-40점&quot;&gt;Subtask 2, 3 (40점)&lt;/h4&gt;

&lt;p&gt;카멜레온의 성별이 두 가지라는 점에서 이분 그래프를 생각해볼 수 있습니다. 각 카멜레온을 정점으로 생각하고, 서로 색깔이 같을 가능성이 있는 두 카멜레온을 간선으로 연결한 이분 그래프를 생각해봅시다. 두 가지 작업을 수행해야 합니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;색깔이 같을 가능성이 있는 두 카멜레온을 찾는 작업&lt;/li&gt;
  &lt;li&gt;(1)에서 찾은 후보 중 실제 정답을 구하는 작업&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;어떤 두 정점 $u, v$가 같은 색을 갖고 있다면 $\text{Query}(u, v) = 1$입니다. $\text{Query}(u, v) = 1$인 카멜레온을 간선으로 연결하면, 아래와 같이 각 정점의 차수가 최대 3이기 때문에 간선은 최대 $3N$개 만들어집니다. 여기까지 $2n \choose 2$번의 쿼리를 사용합니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;색깔이 같은 두 정점($C_u = C_v$) 을 연결하는 간선 (각 정점마다 정확히 1개)&lt;/li&gt;
  &lt;li&gt;짝사랑을 하는 관계($C_u = v \lor C_v = u$)를 연결하는 간선 (각 정점마다 0개 혹은 2개)
    &lt;ul&gt;
      &lt;li&gt;만약 짝사랑을 하는 관계가 없는 경우(서로 좋아하는 경우, $C_u = v \land C_v = u$)에는 $\text{Query}(u, v) = 2$이기 때문에 정보를 얻을 수 없습니다. 따라서 간선이 만들어지지 않습니다.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;만약 어떤 정점의 차수가 1이라면 매칭을 찾은 것이므로 곧바로 $\text{Answer}$를 호출하면 됩니다. 이제, 차수가 3인 정점들만 생각하면 됩니다.&lt;/p&gt;

&lt;p&gt;차수가 3인 정점 $u$와 연결된 정점 $v$는 $C_u = C_v$, $C_u = v$, $C_v = u$ 중 정확히 하나를 만족합니다. 여기에서 $C_u = C_v$인 $v$를 바로 찾는 방법과, $C_u \neq C_v$인 $v$를 하나씩 찾아서 제거하는 방법 중 한 가지를 시도해야 합니다. 일단 $u$와 연결된 정점들에 대해서 $\text{Query}$를 호출해서 얻을 수 있는 결과를 살펴봅시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$u$, $C_u = C_v$인 정점, $C_u = v$인 정점 : 2 반환&lt;/li&gt;
  &lt;li&gt;$u$, $C_u = C_v$인 정점, $C_v = u$인 정점 : &lt;strong&gt;1 반환&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;$u$, $C_u = v$인 정점, $C_v = u$인 정점 : 2 반환&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;세 번의 쿼리 중 두 번의 쿼리는 2를 반환하고, 나머지 한 번의 쿼리는 1을 반환합니다. 그리고 1을 반환하는 쿼리에서 사용하지 않는 정점 $v$는 $C_u \neq C_v$를 만족합니다. 그러므로 “$C_u \neq C_v$인 $v$를 제거하는 전략”을 성공적으로 수행할 수 있습니다. 이 과정에서 최대 $4n$번의 쿼리를 사용합니다.&lt;/p&gt;

&lt;p&gt;${2n \choose 2} + 4n = n(2n+3) \leq 5\,150$번의 쿼리로 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://oj.uz/submission/448100&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;subtask-1-4점-총-44점&quot;&gt;Subtask 1 (4점, 총 44점)&lt;/h4&gt;

&lt;p&gt;$L_{L_i} = i$이기 때문에 모든 정점의 차수가 1입니다. $n \leq 500$이기 때문에 간선을 Naive하게 찾으면 안 되고, 조금 더 똑똑한 방법을 생각해야 합니다.&lt;/p&gt;

&lt;p&gt;$i$번 정점을 처리하기 전에 $1 \cdots i-1$번 정점 사이에서 생기는 매칭을 모두 처리했다고 가정합시다. $i$번째 정점과 매칭할 수 있는 정점이 “아직 매칭이 안 된 정점 집합 $S$”에 존재하는지 탐색하는 방식으로 진행할 것입니다. 현재 $S$에 속한 모든 정점의 색깔이 서로 다르다는 점에 주목합시다.&lt;/p&gt;

&lt;p&gt;만약 $\text{Query} (S + i) = \vert S \vert + 1$이라면 겹치는 색이 존재하지 않으므로 $i$번째 정점과 매칭할 수 있는 정점이 $S$에 존재하지 않습니다. $\text{Query}(S + i) &amp;lt; \vert S \vert + 1$인 경우를 생각해봅시다.&lt;/p&gt;

&lt;p&gt;$\text{Query} (S) = \vert S \vert$라는 점을 이용하면 이분 탐색을 이용해서 $i$와 매칭되는 정점을 찾을 수 있습니다. 구체적으로, $S$의 원소들의 “순서”를 정한 뒤, 처음으로 $\text{Query}(S_0, S_1, \cdots , S_k, i) &amp;lt; k + 1$이 되는 $k$를 찾으면 됩니다.&lt;/p&gt;

&lt;p&gt;만약 Subtask 2, 3을 먼저 풀고 오지 않고 앞에서부터 차근차근 따라 해결한다면 다음과 같은 관점으로 생각할 수 있습니다. 항상 두 카멜레온이 서로 좋아하므로, ‘좋아하는 관계’가 $\text{Query}$에 절대 영향을 주지 않습니다(항상 색이 서로 바뀌어 색의 집합은 그대로이므로). 따라서 $\text{Query}(S) = \text{Query}(S \cup x)$이면 $x$와 색이 같은 카멜레온이 $S$ 내에 존재합니다. 따라서 처음에 전체집합에서 $x$를 뺀 집합에서 시작해, 집합을 절반으로 계속 나누어가면서 색이 같은 카멜레온을 찾으면 됩니다. 이 풀이의 경우 더 간단하지만 위에서 언급한 다양한 이분 그래프의 성질을 잃어버리게 되므로, 뒤쪽 서브태스크를 해결하는 데 혼란이 있을 수 있습니다.&lt;/p&gt;

&lt;p&gt;두 관점 모두 $O(n \log n)$번의 쿼리를 사용하므로 Subtask 1을 통과할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://oj.uz/submission/448104&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;subtask-4-20점-총-64점&quot;&gt;Subtask 4 (20점, 총 64점)&lt;/h4&gt;

&lt;p&gt;각 카멜레온의 성별을 미리 알고 있습니다. 따라서 어떤 카멜레온 $x$가 있으면, $x$가 좋아하거나, $x$를 좋아하거나, $x$와 색이 같을 수 있는 카멜레온의 집합을 미리 성별이 모두 같도록 구성할 수 있습니다.&lt;/p&gt;

&lt;p&gt;Subtask 1의 아이디어를 살짝 빌려와, $x$와 성별이 다른 모든 카멜레온의 집합에서 이분 탐색을 수행해 봅시다. 이 집합에는 서로 좋아하는 카멜레온이나 색이 같은 카멜레온이 없으므로(간선이 존재하지 않으므로) 집합의 절반을 $T$라고 하면  $\text{Query}(T \cup x)$가 $\vert T\vert + 1$인지를 확인함으로서 $\exists p \in T$와 $x$ 사이에 간선이 존재하는지를 확인할 수 있습니다. 따라서 한 번 이분탐색을 진행할 때마다 하나의 간선을 찾아낼 수 있습니다.&lt;/p&gt;

&lt;p&gt;각 정점에 대해 degree는 최대 3이므로 위 과정을 세 번 반복하면 모든 간선을 찾아낼 수 있습니다. 그 이후는 Subtask 3과 같이 간선으로 적당히 좋아하는 관계를 찾고 이를 바탕으로 색깔이 같은 쌍을 찾아낼 수 있습니다.&lt;/p&gt;

&lt;h4 id=&quot;subtask-5-100점&quot;&gt;Subtask 5 (100점)&lt;/h4&gt;

&lt;p&gt;이제는 카멜레온의 성별을 모릅니다. 하지만 카멜레온들이 이분 그래프를 이룬다는 것은 알고 있기 때문에, 카멜레온의 성별을 임의로 지정하되 간선으로 이어진 카멜레온들의 성별을 다르게 유지함으로서 카멜레온의 성별을 알고 있는 것처럼 행동할 수 있습니다. 정확히는 $i$번 카멜레온을 이분 그래프에 추가할 때 $1 \dots i-1$번 카멜레온과의 간선을 찾아줌으로서 $i$번 카멜레온의 색깔을 귀납적으로 결정할 수 있습니다(결정할 수 없을 수도 있는데, 그러면 후술하겠지만 그냥 아무 색깔로 두어도 상관이 없습니다).&lt;/p&gt;

&lt;p&gt;$1 \dots i-1$과의 간선은 어떻게 찾아야 할까요? 현재 카멜레온의 색깔을 모르므로 Subtask 4와 같이 해결하기는 힘듭니다. 그러나 카멜레온의 성별을 임의로 지정해(이분 그래프를 만들어) 유지하고 있으므로, 이분 그래프의 각 부분 자체는 Subtask 4와 같이 서로 성별이 같습니다. 따라서 두 부분에 대해 모두 Subtask 4와 같은 방법을 수행할 수 있습니다.&lt;/p&gt;

&lt;p&gt;다만 문제는, &lt;strong&gt;우리가 관리하고 있는 이분 그래프가 연결 그래프가 아닐 수 있기 때문에&lt;/strong&gt; 두 부분에 간선이 &lt;strong&gt;나뉘어&lt;/strong&gt; 들어갈 수 있다는 뜻입니다. 이로 인해 처리해주어야 하는 첫 번째 포인트는 쿼리 개수를 낭비하지 않기 위해 각 부분에 추가할 간선이 존재하는지를 미리 확인해주어야 한다는 점입니다. 두 번째 포인트는 색깔의 파괴입니다. 요약하면 아래 그림과 같습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/chameleon-1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;새로 정점 하나를 추가할 때 초록색 간선들이 달렸다고 생각해봅시다. 이때 각 컴포넌트는 이분 그래프가 맞지만, 임의로 지정한 색깔이 제대로 맞아들어가지 않습니다. 따라서 이런 경우 둘 중 하나의 컴포넌트를 골라 컴포넌트의 색을 통째로 뒤집어주어야 합니다. 이분 그래프는 보장이 되기 때문에 항상 잘 뒤집을 수 있습니다.&lt;/p&gt;

&lt;p&gt;위 두 문제를 모두 해결하면 정답을 받을 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://oj.uz/submission/448142&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;yet-another-interval-graph-problem-tst-2021-day2-2번&quot;&gt;Yet Another Interval Graph Problem (TST 2021 Day2 2번)&lt;/h3&gt;

&lt;p&gt;제거하는 원소의 가중치를 최소화하는 것은 사용하는 원소의 가중치를 최대화하는 것이라고 생각할 수 있고, 대부분 이 방향으로 생각하는 것이 구현하기 편합니다. &lt;strong&gt;남겨놓는 원소의 가중치를 최대화&lt;/strong&gt;하는 문제로 생각해봅시다.&lt;/p&gt;

&lt;h4 id=&quot;subtask-2-17점&quot;&gt;Subtask 2 (17점)&lt;/h4&gt;

&lt;p&gt;$K = 1$이면 &lt;strong&gt;가중치 있는 회의실 배정 문제&lt;/strong&gt;가 되고, 끝점 기준으로 정렬하고 DP를 하면 $O(N \log N)$ 혹은 $O(N^2)$시간에 해결할 수 있습니다. 회의실 배정 문제를 푼 뒤, 전체 가중치 합에서 뺀 값을 출력하면 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/7db9f07a8e79445d9fe078123936e8ec&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;subtask-3-0점&quot;&gt;Subtask 3 (0점)&lt;/h4&gt;

&lt;p&gt;Priority Queue 정렬 기준을 잘못 설정하면 이 서브태스크를 맞을 수 있습니다. 반성하면 됩니다.&lt;/p&gt;

&lt;h4 id=&quot;subtask-4-36점-총-53점&quot;&gt;Subtask 4 (36점, 총 53점)&lt;/h4&gt;

&lt;p&gt;$N \leq 250$인 걸 보니 $O(N^3)$ 정도에 풀어야 한다는 것을 예상할 수 있습니다.&lt;br /&gt;정해로 발전할 수 없는 풀이와 발전할 수 있는 풀이가 존재합니다. 순서대로 설명하겠습니다.&lt;/p&gt;

&lt;h5 id=&quot;subtask-4-1&quot;&gt;Subtask 4 (1)&lt;/h5&gt;

&lt;p&gt;정해에 전혀 접근하지 못하는 풀이(그러나 처음 생각하기는 훨씬 쉬운 풀이) 입니다. 선분들을 시작점 순서대로 번호 붙이고, $DP[i][j][k]$를 $1$번부터 $i$번째 선분까지를 고려했을 때, 가장 오른쪽 컴포넌트에 포함된 간선 개수가 $j$이고 종료점이 가장 오른쪽에 있는 선분이 $k$번 선분일 때 삭제해야 하는 가중치 합의 최소라고 정의합시다. $i$의 범위는 $1\cdots N$, $j$의 범위는 $1\cdots K$, $k$의 범위는 $1\cdots N$이므로 고려해야 하는 상태의 수는 $O(N^3)$입니다.&lt;/p&gt;

&lt;p&gt;그러면 $i$번 간선을 삭제할건지, 그렇지 않을 건지에 입각해서 상태 전이를 구성할 수 있습니다. 삭제한다면 $DP[i-1][j][k]$로부터 당겨올 수 있겠고, 그렇지 않다면 $DP[i-1][j-1][\text{max}]$로부터 당겨올 수 있겠습니다. 만약 $k$번 선분의 종료점보다 현재 선분의 시작점이 오른쪽에 있다면 공짜로 선분 하나를 남길 수 있다는 점까지 고려하면 각 상태 전이가 $O(1)$이므로 $O(N^3)$에 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;h5 id=&quot;subtask-4-2&quot;&gt;Subtask 4 (2)&lt;/h5&gt;

&lt;p&gt;끝점이 $i$ 이하인 선분들만 사용해서 만들 수 있는 가중치의 최댓값을 $D(i)$라고 정의합시다. 좌표 압축을 하면 $i$는 최대 $2N$입니다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“끝점이 $i$ 이하인 선분들만 사용하는 것”&lt;/strong&gt;은 $j &amp;lt; i$인 $j$에 대해 $D(j)$를 구성한 뒤, $(j, i]$ 구간에 완전히 포함되는 최대 $K$개의 선분을 선택하는 것과 동치입니다. 그러므로 $C(a,b)$를 $[a,b]$ 구간에 완전히 포함되는 최대 $K$개의 선분의 가중치 합의 최댓값이라고 정의하면, $D(i) = \max_{j &amp;lt; i}(D_j + C(j+1, i))$입니다.&lt;/p&gt;

&lt;p&gt;$C(\ast,\ast)$는 $O(N^3)$에 모두 전처리할 수 있고, 점화식은 $O(N^2)$에 계산할 수 있으므로 전체 시간 복잡도는 $O(N^3)$입니다.&lt;/p&gt;

&lt;h4 id=&quot;subtask-5-150점&quot;&gt;Subtask 5 (150점)&lt;/h4&gt;

&lt;p&gt;출제자가 왜 입력 제한을 $N \leq 5\,000$ 대신 $N \leq 2\,500$으로 정했을까요? $O(N^2)$ 뿐만 아니라 $O(N^2 \log N)$도 통과시키려는 의도라고 추측할 수 있습니다.&lt;/p&gt;

&lt;p&gt;점화식을 계산하는 과정 자체는 이미 $O(N^2)$으로 충분히 빠르기 때문에, $C$를 전처리하는 과정을 최적화 해야 합니다. 고정된 $i$에 대해 $j$를 증가시키면서 $C(i, j)$를 구할 때, 선분이 추가되는 상황에서 가장 큰 $K$를 관리하는 작업은 min-heap을 이용해 수행할 수 있습니다. 즉, $C(i, j)$ 하나를 구할 때 $O(\log N)$이 걸리므로 $C(\ast,\ast)$를 모두 전처리하는 것은 $O(N^2 \log N)$ 시간에 할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://boj.kr/c6859e9ffc844697b701734cc3a54795&quot;&gt;코드&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 02 Aug 2021 02:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2021/08/02/koopspt1/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2021/08/02/koopspt1/</guid>
        
        
      </item>
    
      <item>
        <title>Linear Recurrence</title>
        <description>&lt;p&gt;이번 글에서는 선형 점화식의 $n$번째 항을 빠르게 구하는 방법에 대해 알아보도록 하겠습니다.&lt;/p&gt;

&lt;h3 id=&quot;문제&quot;&gt;문제&lt;/h3&gt;
&lt;p&gt;다음과 같이 정의되는 무한수열 ${a_n}$이 있습니다.&lt;/p&gt;

\[a_n = c_1 a_{n-1} + c_2 a_{n-2} + \ldots + c_k a_{n-k}\]

&lt;p&gt;$n$과 초항 $a_1, a_2, \ldots, a_k$, 계수 $c_1, c_2, \ldots, c_k$ 이 주어지면 $a_n$을 구하는 것이 목표입니다.&lt;/p&gt;

&lt;p&gt;가장 간단한 방법부터, 이번 글의 최종 목표인 $O(k log k log n)$ 만에 구하는 방법까지 알아보도록 하겠습니다.&lt;/p&gt;

&lt;h3 id=&quot;정의를-대입&quot;&gt;정의를 대입&lt;/h3&gt;
&lt;p&gt;가장 간단한 방법으로, 점화식 그대로 $a_{k+1}, a_{k+2}, \ldots, a_n$을 순서대로 구하는 방법이 있습니다. 이 방법의 시간복잡도는 $O(nk)$로, 구현이나 생각이 아주 단순한 만큼 효율적인 방법은 아닙니다.&lt;/p&gt;

&lt;p&gt;비슷한 방법으로, 점화식을 이용해 $a_n$을 $a_{n-1}, a_{n-2}, \ldots, a_{n-k}$로 표현하고, 다시 $a_{n-1}$을 $a_{n-2}, a_{n-3}, \ldots, a_{n-k-1}$로 표현하는 과정을 반복하여 대입해주면, 최종적으로 $a_n$을 다음과 같은 식으로 표현할 수 있게 됩니다.&lt;/p&gt;

\[a_n = d_1 a_1 + d_2 a_2 + \ldots + d_k a_k\]

&lt;p&gt;마지막으로 주어진 초항을 대입하면 역시 $O(nk)$ 시간으로 $a_n$을 구할 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;행렬-곱셈&quot;&gt;행렬 곱셈&lt;/h3&gt;

\[a_n = c_1 a_{n-1} + c_2 a_{n-2} + \ldots + c_k a_{n-k}\]

\[a_{n-1} = a_{n-1}\]

\[a_{n-2} = a_{n-2}\]

\[\vdots\]

\[a_{n-k+1} = a_{n-k+1}\]

&lt;p&gt;좌변의 $k$개 항이 모두 $a_{n-1}, a_{n-2}, …, a_{n-k}$의 선형 결합으로 표현되므로, 행렬의 곱셈을 이용해 표현할 수 있고, 따라서 행렬의 거듭제곱을 빠르게 계산하여 $a_n$을 계산할 수 있습니다. 행렬 곱셈 한 번에 $O(k^3)$ 시간이 걸리므로, 총 시간복잡도는 $O(k^3 log n)$이 됩니다. 잘 알려져 있는 기법이니 자세한 설명은 생략합니다.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;연습문제&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2749&quot;&gt;BOJ 2749 피보나치 수 3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15570&quot;&gt;BOJ 15570 블록 2&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;키타마사법&quot;&gt;키타마사법&lt;/h3&gt;
&lt;p&gt;위에서 이용한 방법을 다시 관찰해봅시다.&lt;/p&gt;

\[a_n = d_1 a_1 + d_2 a_2 + \ldots + d_k a_k\]

&lt;p&gt;이 꼴을 만들기 위해 우리가 하는 반복적으로 하는 작업은 점화식을 대입하는 것이고, $a_n$에 $0 = a_n - c_1 a_{n-1} - c_2 a_{n-2} - \ldots - c_k a_{n-k}$ 의 상수배를 더하는 것과 같습니다. 이 과정을 차수가 가장 높은 항부터 반복적으로 적용하여 최고차항의 차수를 계속 낮추어 $k$차 이하의 항만 남게 만들면, 우리가 아는 초항을 대입해 $a_n$을 구할 수 있습니다.&lt;/p&gt;

&lt;p&gt;이 과정의 계수 변화를 관찰해보면, &lt;em&gt;다항식의 나눗셈&lt;/em&gt; 과 완벽하게 동일하다는 것을 알 수 있습니다.&lt;/p&gt;

&lt;p&gt;$x^n$이라는 다항식을 $f(x) = x^k - c_1 x^{k-1} - c_2 x^{k-2} - \ldots - c^k x^0$로 나눗셈을 하면, 우리가 구하고 싶은 계수들인 $d_1, d_2, \ldots, d_k$는 $x^n$을 $f(x)$로 나눈 나머지인 다항식의 계수에 해당합니다.&lt;/p&gt;

&lt;p&gt;따라서 우리가 결국 해야하는 건 $x^n$를 $mod$ $f(x)$ 상에서 계산하는 것이고, 다항식의 곱셈, 나눗셈을 $O(log n)$ 번 하게 됩니다. 곱셈과 나눗셈을 단순하게 $O(k^2)$로 구현하면, $a_n$을 구하는 데 $O(k^2 log n)$ 시간 만에 구할 수 있게 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;연습문제&lt;/em&gt; (스포 주의)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15572&quot;&gt;BOJ 15570 블록 4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/10730&quot;&gt;BOJ 10730 업적의 노예 2&lt;/a&gt; - $a_n$을 구하는 형태는 아니지만, 결국 위에서 설명한 내용으로 구해지는 형태임을 쉽게 관찰할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/18539&quot;&gt;BOJ 18539 Basis Change&lt;/a&gt; - 충분히 큰 적당한 $n$을 잡고, $x^n$을 $f(x)$로 나누는데, $x^0, x^1, \ldots, x^{k-1}$을 남기는 것이 아니라 $x^{n-b_1}, x^{n-b_2}, \ldots, x^{n-b_k}$를 남기는 형태입니다. 역시 위의 내용을 이용해야 하고, Gaussian Elimination을 이용하여 답을 구할 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;다항식의-나눗셈&quot;&gt;다항식의 나눗셈&lt;/h3&gt;
&lt;p&gt;우리는 $k$차 다항식의 곱셈을 FFT를 이용하여 $O(klogk)$ 만에 할 수 있음을 잘 알고 있습니다. 나눗셈 역시 더 빠르게 계산할 수 있다면 전체 시간복잡도도 향상시킬 수 있을 것입니다. 다항식 나눗셈을 빠르게 하는 방법을 알아봅시다.&lt;/p&gt;

&lt;p&gt;$n$차 다항식 $a(x)$를 $m$차 다항식 $b(x)$로 나누어 다음과 같이 표현됩니다.&lt;/p&gt;

\[a(x) = b(x)q(x) + r(x), deg r &amp;lt; m\]

&lt;p&gt;우리는 이러한 $q(x)$나 $r(x)$를 빠르게 찾는 것이 목표입니다. $n \geq m$인 경우만 생각하면, $q(x)$는 $n-m$차 다항식이고, $r(x)$는 $m-1$차 (이하) 다항식입니다.&lt;/p&gt;

&lt;p&gt;위 식은 항등식이므로, $x$ 대신 ${1 \over x}$ 를 대입해도 성립합니다.&lt;/p&gt;

\[a({1 \over x}) = b({1 \over x}) q({1 \over x}) + r({1 \over x})\]

&lt;p&gt;양변을 다항식으로 만들어주기 위해 양변에 $x^n$을 곱해주면 다음과 같습니다.&lt;/p&gt;

\[x^n a({1 \over x}) = x^m b({1 \over x}) \cdot x^{n-m} q({1 \over x}) + x^{m-1} r({1 \over x}) \cdot x^{n-m+1}\]

&lt;p&gt;일반적으로, $n$차 다항식 $f(x)$에 대해 $x^n f({1 \over x})$는 $f(x)$의 계수 순서를 반대로 뒤집은 다항식이 됩니다. 이런 다항식을 대문자로 표시하면 다음과 같습니다.&lt;/p&gt;

\[A(x) = B(x)Q(x) + R(x) \cdot x^{n-m+1}\]

&lt;p&gt;$R(x)$는 $n-m$차 다항식이므로, $mod \space x^{n-m+1}$ 상에서 계산해도 정확한 값을 얻을 수 있습니다.&lt;/p&gt;

\[A(x) \equiv B(x)Q(x) (mod \space x^{n-m+1})\]

&lt;p&gt;우리가 $Q(x)$를 구하기 위해서는 $(mod \space x^{n-m+1})$ 상에서의 $B(x)$의 역원을 찾아 $A(x)$에 곱해주면 됩니다.&lt;/p&gt;

&lt;p&gt;그러면 이런 $B(x)$의 역원 $B^{-1} (x)$이 항상 존재하고, 효율적으로 구할 수 있을까요? &lt;em&gt;그렇습니다.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;$mod \space x^{n-m+1}$ 상에서의 계산은 어려우니 $mod \space x$에서 시작해봅시다.&lt;/p&gt;

&lt;p&gt;$B(x) B^{-1} (x) \equiv 1 (mod \space x)$인 $B^{-1} (x)$은 어떤가요? $B(x)$의 상수항은 $b(x)$의 최고차항의 계수와 같으므로, 0이 아님이 분명합니다. 따라서 $B^{-1} (x) \equiv (B(x)의 \space 상수항)^{-1} (mod \space x)$가 됩니다.&lt;/p&gt;

&lt;p&gt;$B(x)C(x) \equiv 1 (mod \space x^k)$인 $C(x)$를 알고 있다고 가정합시다. 자명하게 다음이 성립합니다.&lt;/p&gt;

\[B(x)C(x)-1 \equiv 0 (mod \space x^k)\]

&lt;p&gt;양번을 제곱하면 다음과 같습니다.&lt;/p&gt;

\[(B(x)C(x)-1)^2 \equiv 0 (mod \space x^{2k})\]

&lt;p&gt;전개하고 정리하면 다음과 같은 식을 얻을 수 있습니다.&lt;/p&gt;

\[B(x) \cdot C(x)(2-B(x)C(x)) \equiv 1 (mod \space x^{2k})\]

&lt;p&gt;따라서 $(mod \space x^{2k})$ 상에서의 역원은 $C(x)(2-B(x)C(x))$임을 알 수 있습니다.&lt;/p&gt;

&lt;p&gt;따라서 $mod \space x^k$에서의 역원을 알면 $mod \space x^{2k}$에서의 역원을 $k$차 다항식의 곱셈 두번으로 알 수 있으므로, 이 과정을 반복해 $mod x^{n-m+1}$까지 끌어올리면 됩니다. 일반적으로 $n-m+1$이 항상 $2$의 거듭제곱은 아니니 $n-m+1$보다 큰 $2$의 거듭제곱까지 계산하면 충분합니다.&lt;/p&gt;

&lt;p&gt;$k$차식의 나눗셈에 걸리는 시간을 $T(k)$라 하면, $mod \space x$에서의 역원은 $O(1)$만에 알 수 있으므로 $T(k) = T({k \over 2}) + O(m(k)) $ 가 되고, $T(k) = O(m(k))$가 됩니다. 이때 $m(k)$는 다항식의 곱셈에 걸리는 시간으로, FFT를 사용하여 $O(klogk)$만에 할 수 있습니다.&lt;/p&gt;

&lt;p&gt;따라서 위의 과정을 이용해 $B^{-1} (x)$를 구하고, $A(x)$에 곱하고, 차수가 $n-m$ 이하인 항만 취한 뒤 계수 순서를 다시 뒤집어주면 $q(x)$를 구해낼 수 있습니다.&lt;/p&gt;

&lt;p&gt;이렇게 구현된 $O(klogk)$ 다항식 곱셈/나눗셈을 통해 키타마사법을 구현하면, $a_n$을 $O(klogklogn)$ 시간에 구할 수 있습니다. FFT를 이용한 다항식 곱셈 부분만 복붙 등으로 해결한다면, 나눗셈이나 키타마사법 등은 쉽게 구현할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;연습문제&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/13725&quot;&gt;BOJ 13725 RNG&lt;/a&gt; 위에서 설명한 내용을 그대로 구현하면 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/13178&quot;&gt;BOJ 13178 목공&lt;/a&gt; 점화식을 세우면, 선형점화식인 듯 하지만 상수항이 거슬리는 형태입니다. 상수항을 제외한 나눗셈 과정에서 상수항이 미치는 영향을 관찰하면 풀 수 있습니다.&lt;/p&gt;
</description>
        <pubDate>Wed, 20 May 2020 16:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/05/20/linear-recurrence/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/05/20/linear-recurrence/</guid>
        
        <category>math</category>
        
        
      </item>
    
      <item>
        <title>정확도 높은 FFT와 NTT</title>
        <description>&lt;p&gt;FFT에서는 실수 자료형을 사용하기 때문에 실수 오차가 발생할 수 있고, 이는 즐거운 PS생활에 큰 지장을 줄 수 있습니다. 특히 FFT 문제에서 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;수가 너무 크기 때문에 M으로 나눈 나머지를 출력한다.&lt;/code&gt;와 유사한 문장이 나온다면 실수 오차를 만나는 것을 각오해야 합니다.&lt;br /&gt;이 글에서는 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M으로 나눈 나머지를 출력한다.&lt;/code&gt;같은 문장이 나오는 문제에서 약간의 속도를 희생해 FFT의 정확도를 높이는 방법과 정수만 이용해서 FFT를 하는 방법에 대해 다룹니다.&lt;/p&gt;

&lt;p&gt;※ 이 글은 독자가 분할 정복을 이용한 FFT 알고리즘(쿨리-튜키 알고리즘)을 알고있다는 전제 하에 설명합니다.&lt;br /&gt;쿨리-튜키 알고리즘을 이용해 다항식 곱셈을 하는 방법은 &lt;a href=&quot;https://justicehui.github.io/hard-algorithm/2019/09/04/FFT/&quot;&gt;여기&lt;/a&gt;에서 볼 수 있습니다.&lt;br /&gt;쿨리-튜키 알고리즘을 이용한 다항식 곱셈의 구현 예시는 &lt;a href=&quot;http://boj.kr/a441c917803748ffa0834c97116317d3&quot;&gt;여기&lt;/a&gt;에서 볼 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;정확도-높은-fft&quot;&gt;정확도 높은 FFT&lt;/h3&gt;

&lt;p&gt;몇몇 문제에서는 두 개의 다항식을 곱한 결과가 너무 크기 때문에 어떤 수 $M$으로 나눈 나머지를 요구하는 문제들이 있습니다.&lt;br /&gt;일반적인 FFT는 실수 자료형(complex&amp;lt;&lt;strong&gt;double&lt;/strong&gt;&amp;gt;)을 이용하기 때문에 수가 커지면 실수 오차가 생길 수 밖에 없고, 모듈러를 취하는 대부분의 문제들은 실수 오차 이슈가 발생합니다. 이 단락에서는 다항식을 곱한 뒤 모듈러를 취할 때 흔히 발생하는 실수 오차를 피하는 방법을 다룹니다.&lt;/p&gt;

&lt;p&gt;실수 오차가 생기는 이유는 실수 자료형이 정확하게 표현할 수 없을 정도로 수가 &lt;strong&gt;너무 커지기 때문&lt;/strong&gt;입니다. 그러므로 계산 과정에서 나오는 수들의 크기가 작아지도록 조절해주면 실수 오차를 피할 수 있을 것입니다.&lt;/p&gt;

&lt;p&gt;다항식 $A(x)$와 $B(x)$는 각각 아래와 같이 작은 다항식 2개로 쪼개줄 수 있습니다.&lt;/p&gt;

\[A(x) = A_1(x) + A_2(x) \times C \\ B(x) = B_1(x) + B_2(x) \times C\]

&lt;p&gt;두 n차 다항식 $A(x), B(x)$를 곱하는 것은 $A(x)B(x) = A_1(x)B_1(x)+C\times(A_1(x)B_2(x)+A_2(x)B_1(x))+C^2\times(A_2(x)B(x))$로 나타낼 수 있습니다.&lt;/p&gt;

&lt;p&gt;$C \approx \sqrt M$ 정도로 정해주면 $A_1(x), A_2(x), B_1(x), B_2(x)$의 계수들의 최댓값이 $\sqrt M$ 정도이기 때문에 곱셈을 해도 계수들의 최댓값이 $Mn$ 정도가 되고, 기존의 방법보다 훨씬 작은 수들을 이용해 계산을 하기 때문에 실수 오차를 피할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://ideone.com/J1ZzNg&quot;&gt;여기&lt;/a&gt; 있는 코드는 $C = 2^{15}$일 때 위 알고리즘을 구현한 것입니다. 4번의 dft와 3번의 idft를 사용합니다.&lt;/p&gt;

&lt;p&gt;koosaga님 github에 있는 다항식 라이브러리(&lt;a href=&quot;https://github.com/koosaga/olympiad/blob/master/Library/codes/math/polynomial.cpp&quot;&gt;링크&lt;/a&gt;)의 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;multiply_mod&lt;/code&gt;를 보면 2번의 dft와 2번의 idft만 이용해 하는 방법이 있는 것으로 보입니다.&lt;/p&gt;

&lt;h3 id=&quot;정수-fft&quot;&gt;정수 FFT&lt;/h3&gt;

&lt;p&gt;다항식 곱셈 결과를 특정 몇몇 소수로 나눈 나머지를 구할 때는 NTT라는 또 다른 방법을 사용할 수 있습니다.&lt;/p&gt;

&lt;p&gt;$n = 2^k$차 다항식의 FFT를 구할 때 우리는 $w^n = 1$인 복소수 $w = \cos(2\pi/n) + i\times \sin(2\pi/n)$를 사용합니다. 이런 $w$는 sin, cos 등 실수로 구성된 복소수를 사용하기 때문에 실수 오차의 위험이 따라올 수 밖에 없습니다.&lt;/p&gt;

&lt;p&gt;여기에서 생각해볼 수 있는 것은, $w^n ≡ 1\mod p$이면서 $w^0, w^1, \dots w^{n-1}$이 모두 서로 다른 $w$(원시근)와 $p$(소수)가 있고 이때 n이 $2^k$꼴의 배수라면, 원시근 $w$가 FFT에서 사용하는 복소수 $w$와 동일한 역할을 할 수 있게 됩니다.&lt;br /&gt;결국, 적당히 큰 $2^k$꼴의 배수인 $n$과 조건을 만족하는 $w, p$를 찾을 수 있다면 $A(x)\times B(x)\mod p$를 정수만 이용해서 계산할 수 있습니다.&lt;/p&gt;

&lt;p&gt;페르마의 소정리에 따르면 $w^{p-1} = 1\mod p$입니다. $n = p-1$은 $2^k$꼴의 배수가 되어야 하기 때문에, $p = a\times2^b +1$인 소수 $p$를 잡을 것입니다. 이런 $p$의 원시근 $w$를 찾았다면 $\cos(2\pi/n)+i\times\sin(2\pi/n)$ 대신 $w^{(p-1)/n}$을 이용해 FFT를 돌려줄 수 있습니다. $n ≤ 2^b$인 크기 $n$짜리 다항식에 대해 FFT mod P를 수행할 수 있습니다.&lt;/p&gt;

&lt;p&gt;아래 표는 NTT에서 자주 사용하는 유명한 소수와 원시근입니다.&lt;/p&gt;

&lt;table&gt;
    &lt;tr&gt; &lt;th&gt;p&lt;/th&gt; &lt;th&gt;a&lt;/th&gt; &lt;th&gt;b&lt;/th&gt; &lt;th&gt;w&lt;/th&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;998,244,353&lt;/td&gt; &lt;td&gt;119&lt;/td&gt; &lt;td&gt;23&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;2,281,701,377&lt;/td&gt; &lt;td&gt;17&lt;/td&gt; &lt;td&gt;27&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;2,483,027,969&lt;/td&gt; &lt;td&gt;37&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;2,113,929,217&lt;/td&gt; &lt;td&gt;63&lt;/td&gt; &lt;td&gt;25&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;104,857,601&lt;/td&gt; &lt;td&gt;25&lt;/td&gt; &lt;td&gt;22&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt; &lt;td&gt;1,092,616,193&lt;/td&gt; &lt;td&gt;521&lt;/td&gt; &lt;td&gt;21&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
    &lt;!--
	&lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt;
	--&gt;
&lt;/table&gt;

&lt;p&gt;998,244,353과 비슷하게 생긴 993,244,853은 $248311213 \times 2^2 + 1$로, NTT를 하기에는 좋지 않은 수입니다. 문제에 가끔씩 나오기 때문에 주의해야 합니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/source/share/5f604fba09ee46099107595863571b89&quot;&gt;여기&lt;/a&gt; 있는 코드는 10진수 자연수 곱셈을 NTT를 이용해 구현한 코드입니다.&lt;/p&gt;

&lt;h3 id=&quot;ntt의-다양한-활용&quot;&gt;NTT의 다양한 활용&lt;/h3&gt;

&lt;h4 id=&quot;fft의-결과-mod-square-free-number&quot;&gt;(FFT의 결과) mod (square free number)&lt;/h4&gt;

&lt;p&gt;NTT는 $p = a\times 2^b + 1$이고 원시근 $w$가 존재할 때 (FFT의 결과) mod P를 정수만 이용해 계산할 수 있게 해줍니다. NTT와 중국인의 나머지 정리를 이용하면 (FFT의 결과) mod (square free number)도 해줄 수 있습니다.&lt;/p&gt;

&lt;p&gt;$x \mod p_1 = a_1 \ x \mod p_2 = a_2 \ x \mod p_3 = a_3$&lt;br /&gt;$p$가 square free number이고 이런 $(p_i, a_i)$쌍들이 주어졌을 때, $x$를 구할 수 있다면 $x \mod p$도 구할 수 있을 것입니다. 그리고 $x$는 &lt;strong&gt;중국인의 나머지 정리&lt;/strong&gt;를 이용해 구할 수 있습니다.&lt;/p&gt;

&lt;p&gt;NTT를 할 수 있는 여러 가지 소수로 NTT를 한 다음, 중국인의 나머지 정리를 이용해 결과를 합쳐주면 (FFT의 결과) mod (square free number)를 할 수 있습니다.&lt;/p&gt;

&lt;h4 id=&quot;자연수-계수-계수--1-63&quot;&gt;자연수 계수, 계수 &amp;lt; (1 « 63)&lt;/h4&gt;

&lt;p&gt;다항식의 계수가 자연수이고 등장하는 계수들이 long long 범위 안에 있으면서 mod를 하지 않는 경우, 다항식 곱셈을 NTT를 이용해서 할 수 있습니다.&lt;/p&gt;

&lt;p&gt;계수가 $2^{63}$보다 작다는 것은 $\mod 2^{63}$을 해도 값이 달라지지 않는다는 것을 의미합니다. 그러므로 위에 나와있는 (FFT의 결과) mod (임의의 자연수)를 그대로 사용하면 됩니다.&lt;/p&gt;

&lt;p&gt;10억~20억 근처의 소수 2개 정도 사용하면 답을 구할 수 있습니다.&lt;/p&gt;

&lt;p&gt;C++로 구현한 코드는 &lt;a href=&quot;https://ideone.com/wIJVrN&quot;&gt;여기&lt;/a&gt;에서 볼 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;참고-자료&quot;&gt;참고 자료&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://codeforces.com/blog/entry/48417&quot;&gt;Codeforces Blog - General ideas&lt;/a&gt; (17. Modulo product of two polynomial with real-valued FFT)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cp-algorithms.com/algebra/fft.html#toc-tgt-7&quot;&gt;CP-Algorithms - FFT&lt;/a&gt; (FFT - Multiplication with arbitrary modulus)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://koosaga.com/139&quot;&gt;koosaga Blog - Fast Fourier Transform&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cubelover.tistory.com/12&quot;&gt;cubelover Blog - Number Theoretic Fast Fourier Transform&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 20 May 2020 16:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/05/20/fft-ntt/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/05/20/fft-ntt/</guid>
        
        <category>FFT</category>
        
        
      </item>
    
      <item>
        <title>2-dimension Euclidean Minimum Spanning Tree</title>
        <description>\[\newcommand{\PhiInsert}{\Phi_{insert}}
\newcommand{\PhiDelete}{\Phi_{delete}}
\newcommand{\size}{\mbox{size}}
\newcommand{\capa}{\mbox{capacity}}
\newcommand{\di}{D_{i}}
\newcommand{\dim}{D_{i-1}}
\newcommand{\zig}{\mbox{Zig}}
\newcommand{\zzig}{\mbox{Zig-zig}}
\newcommand{\zzag}{\mbox{Zig-zag}}
\newcommand{\bf}{\mbox{Before}}
\newcommand{\aft}{\mbox{After}}
\newcommand{\subt}{\mbox{Subtrees}}\]

&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;

&lt;p&gt;Minimum Spanning Tree, 우리말로는 최소 신장 트리는 잊을 만 하면 등장하는 주제 중 하나입니다. 최소 신장 트리를 구하기 위해서는 보통 두 가지 알고리즘이 사용됩니다. 하나는 크루스칼 알고리즘으로 $O(E \log E)$의 복잡도를 가지고, 나머지 하나는 프림 알고리즘으로 $O(E \log V)$의 시간복잡도를 가집니다. 하지만 이 두 알고리즘 모두 한 가지 단점을 가지고 있는데, 일단 알고리즘을 수행하려면 모든 간선을 읽어야 한다는 것입니다. 이게 왜 문제인지 의아해 하실 수도 있지만, 그래프가 완전 그래프에 가깝고, 가중치를 매기는 방식이 별도로 존재하는 것과 같이 특수한 상황에서는 모든 간선을 다 보는 것만으로도 시간 초과가 나서 일부 간선만을 보아야만 문제를 해결할 수 있는 경우가 있습니다. 예를 들어서 간선이 정점에 부여된 가중치의 XOR 값 만큼의 비용을 가지는 경우를 들 수 있겠습니다. 하지만 오늘 다룰 케이스는 그 중에서 간선의 가중치가 정점 사이의 유클리디안 거리인 케이스인 Eucliden MST, 줄여서 EMST입니다. 이 글에서는 정점이 $n$개 있는 &lt;strong&gt;2차원&lt;/strong&gt; EMST 문제를 Naive한 시간복잡도인 $O(n^2 \log n)$보다 빠르게 해결하는 법에 대해 서술합니다.&lt;/p&gt;

&lt;h2 id=&quot;emst를-해결하기-위해서&quot;&gt;EMST를 해결하기 위해서&lt;/h2&gt;

&lt;p&gt;EMST를 해결하기 위해서 우리는 다음과 같은 내용들을 공부해야 합니다. 아니, 사실 단 하나만 공부하면 됩니다. 바로 델루네 삼각분할입니다. 델루네 삼각분할을 간단하게 요약하면 2차원 평면에 분포하는 점들을 선분으로 적절하게 이어 삼각형으로 쪼개는데, 각 삼각형들이 꼭짓점을 제외한 다른 점을 포함하지 않는 분할입니다. 왜 이 델루네 삼각분할이 EMST에 큰 영향을 끼치느냐 하면, EMST에 포함되는 간선들의 집합은 반드시 &lt;strong&gt;델루네 삼각분할을 통해 찾을 수 있는 간선 집합의 부분집합&lt;/strong&gt;이기 때문입니다. 델루네 삼각분할을 통해 찾아지는 간선 집합의 크기가 충분히 작다면, 이를 이용해서 전체 시간복잡도 또한 작게 bound시킬 수 있을 것입니다. 그러면 이 사실을 증명하기 위해서, 델루네 삼각분할에 대해서 간단하게 먼저 공부해봅시다.&lt;/p&gt;

&lt;h2 id=&quot;델루네-삼각분할&quot;&gt;델루네 삼각분할&lt;/h2&gt;

&lt;p&gt;델루네 삼각분할(Delaunay Triangulation)은 보로노이 다이어그램과 &lt;a href=&quot;https://algoshitpo.github.io/2020/03/23/dual/&quot;&gt;듀얼 그래프&lt;/a&gt; 관계입니다. 델루네 삼각분할의 각 삼각형의 외심은 각각 보로노이 다이어그램의 꼭짓점이 됩니다. 또 두 삼각형이 델루네 삼각형에서 변을 공유한다면, 그 두 외심은 보로노이 다이어그램에서 간선으로 이어져 있습니다.&lt;/p&gt;

&lt;p&gt;델루네 삼각분할을 수행하면, 다음과 같은 성질들을 찾을 수 있게 됩니다. 재미를 위해서 델루네 삼각분할의 많은 성질들을 적어 놓았습니다. 그런 고로 이 성질 중 EMST를 해결할 때 사용하는 성질은 극히 일부라는 점에 유의하세요.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;당연하겠지만, 삼각분할 후 만들어지는 모든 조각을 모으면 이는 정점들의 볼록 껍질이 됩니다.&lt;/li&gt;
  &lt;li&gt;델루네 삼각분할은 정점들을 $d$차원 공간에서 $O(n^ {\lceil d/2 \rceil)})$개의 조각으로 쪼갭니다. &lt;strong&gt;이는 매우 중요한 성질로, 우리는 2차원 공간을 다루기 때문에 정점들은 최대 $O(n)$개의 조각으로 쪼개집니다.&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;특히 2차원 공간에서, convex hull이 $b$개의 선분으로 이루어져 있다면 정점들의 삼각분할은 최대 $2n-2-b$개의 삼각형을 가집니다. 이는 Euler characteristic으로 증명할 수 있습니다.&lt;/li&gt;
  &lt;li&gt;평면에서 델루네 삼각분할은 최소 각도를 최대화합니다. 다른 방법으로 어떻게 삼각분할을 하더라도, 그 삼각분할의 최소 각도는 델루네 삼각분할의 최소 각도보다 클 수 없습니다. (최대 각도를 최소화 하지는 않습니다.)&lt;/li&gt;
  &lt;li&gt;어떠한 델루네 삼각분할로 만들어진 삼각형도 그 외접원이 다른 정점을 내부에 포함하지 않습니다.&lt;/li&gt;
  &lt;li&gt;비슷하게, 만약 어떤 원이 두 정점을 지나면서 내부에 아무 정점을 포함하지 않는다면, 그 두 정점을 잇는 간선은 델루네 삼각분할을 구성하는 선분 중 하나입니다. &lt;strong&gt;이는 EMST 증명에 있어서 핵심적인 성질입니다.&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Nearest Neighbor Graph는 델루네 삼각분할의 부분그래프입니다. 이 성질을 이용해서 &lt;a href=&quot;https://algoshitpo.github.io/2020/02/09/kdtree/&quot;&gt;K-d Tree&lt;/a&gt;가 하는 일을 일부 대체할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;델루네 삼각분할을 실제로 Problem Solving에서 구현해야 한다면, 다음과 같은 &lt;a href=&quot;http://www.secmem.org/blog/2019/01/11/Deluanay_Triangulation/&quot;&gt;알고리즘&lt;/a&gt;을 사용할 수 있습니다. 구현이 쉽지 않고 선형대수와 계산 기하에 관한 지식도 요구하지만, 원리 자체는 이해하기 힘든 수준이 아니므로 한번쯤 읽어보셔도 좋을 것 같습니다.&lt;/p&gt;

&lt;h2 id=&quot;emst와-델루네-삼각분할의-접합&quot;&gt;EMST와 델루네 삼각분할의 접합&lt;/h2&gt;

&lt;p&gt;앞에서 EMST에 포함되는 간선들의 집합은 반드시 델루네 삼각분할을 통해 찾을 수 있는 간선 집합의 부분집합이라고 언급한 바가 있습니다. 고로 EMST는 델루네 삼각분할의 부분그래프라는 말인데, 언뜻 보기에는 자명하지 않아 보입니다. 하지만 다음과 같은 과정을 통해 생각보다 간단하게 증명할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Claim: 점 집합 $P$의 Euclidean MST $E$는 $P$의 델루네 삼각분할 $D(P)$의 부분그래프이다.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Proof:&lt;/p&gt;

&lt;p&gt;귀류법을 사용합니다. $p_i, p_j \in P$에 대해서 $\overline{p_i p_j} \in E$이고 $\overline{p_i p_j} \notin D(P)$라고 합시다. 위에서 언급한 델루네 삼각분할의 성질 중 만약 어떤 원이 두 정점을 지나면서 내부에 아무 정점을 포함하지 않는다면, 그 두 정점을 잇는 간선은 델루네 삼각분할을 구성하는 선분 중 하나라는 성질을 사용합시다. 그러면 $\overline{p_i p_j  }$을 지름으로 하는 원 안에는 반드시 하나 이상의 점이 있음을 알 수 있습니다. 그 중 하나를 임의로 골라 $p_k$라고 합시다.&lt;/p&gt;

&lt;p&gt;$\overline{p_i p_j}$를 $E$에서 제거하면, $E$는 트리이므로 2개의 컴포넌트로 쪼개지게 됩니다. 그 중 하나는 $p_i$를 포함하고 나머지 하나는 $p_j$를 포함합니다. 일반성을 잃지 않고 $p_k$가 두 컴포넌트 중 하나인 $p_i$가 존재하는 컴포넌트에 속한다고 합시다. 이 때 $\overline{p_j p_k}$를 $E$에 추가한다면 어떻게 될까요? $p_i$와 $p_k$는 같은 컴포넌트에 속해 있으니 이 간선을 추가하면 두 컴포넌트는 다시 합쳐지게 됩니다. 그런데 $p_k$는 $\overline{p_i p_j  }$을 지름으로 하는 원 안에 있으므로, 자명히 $\vert \overline{p_i p_j} \vert &amp;gt; \vert \overline{p_j p_k}\vert $입니다. 결국 원래 트리는 MST가 아니었으니 가정에 모순이고, Claim이 성립합니다.&lt;/p&gt;

&lt;p&gt;이제 EMST가 델루네 삼각분할의 부분그래프라는 것을 알았으니, 델루네 삼각분할을 진행한 후 삼각분할에 속하는 간선들에 한해서만 크루스칼과 같은 통상적인 MST 알고리즘을 수행해도 전체 간선에 대해서 수행한 것과 동일한 결과를 얻을 수 있다는 것 또한 알 수 있습니다. 델루네 삼각분할은 최적화된 알고리즘을 통해 $O(n \log n)$ 시간에 찾아줄 수 있고, 랜덤 알고리즘을 얹으면 평균적으로 $O(n \log \log n)$시간에도 찾을 수 있습니다. 그렇다면 이 삼각분할을 수행한 간선들에 대해서 크루스칼 알고리즘을 수행하는 데는 얼마의 시간이 걸릴까요? 델루네 삼각분할은 2차원 평면에서 최대 $O(n)$개의 삼각형을 만들고 각 삼각형은 최대 3개의 변을 가지기 때문에 간선의 개수도 $O(n)$개가 됩니다. 따라서 크루스칼 알고리즘을 수행하는 데도 $O(n \log n)$ 시간이 걸립니다.&lt;/p&gt;

&lt;p&gt;결과적으로 위에서 서술한 모든 과정을 $O(n \log n)$ 시간 안에 빠르게 수행할 수 있습니다.&lt;/p&gt;

&lt;h2 id=&quot;더-나아가기&quot;&gt;더 나아가기&lt;/h2&gt;

&lt;p&gt;델루네 삼각분할 외에도 다음과 같은 방법들을 사용할 수 있습니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Well-seperated pair decomposition을 사용하여 찾아 줄 수 있습니다. 기본적인 접근은 쿼드 트리를 이용해 클러스터를 분리한 뒤, 분할 정복처럼 각각의 클러스터를 이어 MST로 만들어 주는 방식입니다.&lt;/li&gt;
  &lt;li&gt;2차원 뿐만이 아닌 더 고차원인 Euclidean MST 문제에서, 보르부카 알고리즘을 조금 더 효율적으로 활용하여 문제를 해결할 수 있습니다. 보르부카 알고리즘은 잘 알려지지 않았지만 특수한 경우의 MST 문제를 효율적으로 해결할 수 있는 방법 중 하나입니다. &lt;a href=&quot;https://mlpack.org/papers/emst.pdf&quot;&gt;다음 논문&lt;/a&gt;에서 Dual-Tree 보르부카 알고리즘을 활용하여 EMST 문제를 해결하는 과정을 볼 수 있습니다. 매우 복잡하고 난해하기 때문에, Problem Solving으로의 적용 가능성은 없을 것 같습니다.&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Wed, 20 May 2020 16:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/05/20/EMST/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/05/20/EMST/</guid>
        
        <category>Geometry</category>
        
        
      </item>
    
      <item>
        <title>랜덤으로 문제 풀기</title>
        <description>&lt;h3 id=&quot;서론&quot;&gt;서론&lt;/h3&gt;

&lt;p&gt;알고리즘 문제를 풀 때 랜덤을 사용해 문제를 해결하는 경우가 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;최악의 시간 복잡도와 평균 시간 복잡도가 많이 차이나는 알고리즘을 사용하는 경우&lt;/strong&gt;에 최악의 상황을 피할 때 사용하기도 하고, 무작위 선택을 여러 번 시도하는 것으로 &lt;strong&gt;틀릴 확률을 줄이는 용도&lt;/strong&gt;로 사용하기도 합니다.&lt;/p&gt;

&lt;p&gt;이 글에서는 랜덤을 이용해 여러가지 문제를 통해 문제 풀이에 랜덤을 적용하는 방법을 다룹니다.&lt;/p&gt;

&lt;h3 id=&quot;목차&quot;&gt;목차&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;랜덤을 이용해 평균 복잡도 보장시키기
    &lt;ul&gt;
      &lt;li&gt;퀵정렬&lt;/li&gt;
      &lt;li&gt;Smallest Enclosing Circle&lt;/li&gt;
      &lt;li&gt;재밌는 인터랙티브 문제&lt;/li&gt;
      &lt;li&gt;BOJ18192 보고 정렬&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;랜덤을 여러 번 시도해 틀릴 확률 줄이기
    &lt;ul&gt;
      &lt;li&gt;BOJ10523 직선 찾기&lt;/li&gt;
      &lt;li&gt;BOJ2912 백설공주와 난쟁이&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;더 알아보기&lt;/li&gt;
  &lt;li&gt;참고 문헌&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;퀵정렬&quot;&gt;퀵정렬&lt;/h3&gt;

&lt;p&gt;최악의 경우와 평균적인 경우에 시간 복잡도가 차이나는 대표적인 알고리즘입니다.  최악의 경우에는 시간 복잡도가 $O(N^2)$이 되고, 평균적인 경우에는 $O(N log N)$이 됩니다. 대부분의 정렬 문제에서는 최악의 경우가 발생하는 채점 데이터를 사용해 퀵정렬을 이용한 풀이를 틀리게 합니다.&lt;/p&gt;

&lt;p&gt;퀵정렬을 이용하면서 TLE를 피할 방법은 생각해보면 매우 간단합니다. 최악의 경우가 발생하지 않도록 하면 되는 것이고, 이것은 배열을 섞어주는 것으로 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;C++ STL에 배열을 섞어주는 random_shuffle(&lt;a href=&quot;http://www.cplusplus.com/reference/algorithm/random_shuffle/&quot;&gt;reference&lt;/a&gt;)이라는 함수가 존재하므로, 이 함수를 이용하면 쉽게 최악의 경우를 피할 수 있습니다.&lt;/p&gt;

&lt;p&gt;아래 코드는 BOJ2751 수 정렬하기 2 (&lt;a href=&quot;http://icpc.me/2751&quot;&gt;링크&lt;/a&gt;)에서 AC를 받은 코드입니다.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]){&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ios_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sync_with_stdio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;random_shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;배열을 안 섞어주면 TLE를 받게 됩니다. (&lt;a href=&quot;https://www.acmicpc.net/source/share/f1e48bb836ba4b878ee66122ea3009a6&quot;&gt;코드&lt;/a&gt;)&lt;/p&gt;

&lt;h3 id=&quot;smallest-enclosing-circle&quot;&gt;Smallest Enclosing Circle&lt;/h3&gt;

&lt;p&gt;최소 외접원 문제(Smallest Enclosing Circle)은 2차원 평면 상에 N개의 점이 주어지면, 이 점들을 모두 포함하는 가장 작은 원을 찾는 문제입니다.&lt;br /&gt;$O(N^4)$풀이를 찾고 이를 발전시켜 $O(N^3)$풀이를 찾은 뒤, $O(N)$으로 줄여보도록 하겠습니다.&lt;/p&gt;

&lt;p&gt;본격적으로 문제를 풀기 전에 몇 가지 관찰을 해야합니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;2개 이상의 점이 최소 외접원의 둘레 위에 있습니다.&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;0개 혹은 1개만 있는 경우, 반지름을 조금 줄이고 원의 중심을 적절히 이동해 2개 이상을 포함하도록 만들 수 있습니다.&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;최소 외접원의 둘레 위에 정확히 2개의 점만 있는 경우, 원의 중심은 그 두 점의 중점에 위치합니다.&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;1번과 마찬가지로 반지름을 조금 줄이고 중심을 적절히 이동하면 더 작은 원을 만들 수 있습니다.&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;최소 외접원의 둘레 위에 3개 이상의 점이 있는 경우, 최소 외접원은 임의의 세 점으로 만든 삼각형의 외접원이 됩니다.&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;점 3개를 알면 외접원을 구할 수 있고, 유일합니다.&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;위 3가지 관찰을 이용해 2개의 점으로 만든 원 $_NC_2$가지와 3개의 점으로 만든 $_NC_3$가지를 각각 모두 검사해서 $O(N^4)$에 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;~&lt;/p&gt;

&lt;p&gt;우리가 고려해야하는 원의 개수는 $O(N^3)$입니다. 여기서 점 하나를 고정하면 $O(N^2)$개가 되고, 하나를 더 고정하면 $O(N)$개가 됩니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/IRNstWN.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;여기에서 한 가지 관찰을 더 할 수 있습니다.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;두 점을 고정했을 때 봐야하는 원의 중심은 한 직선(두 점을 잇는 선분의 수직 이등분선) 위에 있다.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;두 점 p, q를 고정해서 해당 점들을 포함하는 외접원을 구한다고 하면, p와 q를 잇는 직선과 가장 중심이 멀리 떨어진 것만 보면 됩니다.&lt;/p&gt;

&lt;p&gt;이것은 두 점을 고정한 다음, 점들을 하나씩 추가하면서 원을 확장하는 방식으로 구현할 수 있습니다. 원에 포함되지 않은 점이 나올 때마다 원을 키워주면 됩니다.&lt;br /&gt;두 점을 고정하는 $O(N^2)$가지 경우에 대해 각각 $O(N)$, 총 $O(N^3)$에 최소 외접원을 찾을 수 있습니다.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//내적&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//외적&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getCenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//두 점의 중점&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getCenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//세 점의 외접원의 중심&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Circle&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;solve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pt&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//break point 1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//break point 2&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getCenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//break point 3&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getCenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;solve함수의 각 for문에는 각각 if문이 하나씩 같이 달려있고, 3개의 if문에 의해 걸러져 3개의 for문을 수행하지 않은 경우가 꽤 많습니다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;break point 2&lt;/strong&gt;에 있는 조건 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if(dst(p, v[j]) &amp;gt; r)&lt;/code&gt;를 통과해 안쪽으로 들어갈 확률은 평균 $O(\frac {1}{j})$이고, 들어간 경우에 &lt;strong&gt;break point 3&lt;/strong&gt;에 있는 반복문에서 $O(j)$시간이 걸립니다. 즉, &lt;strong&gt;break point 2&lt;/strong&gt;에 있는 반복문의 평균 시간 복잡도는 $O(1)$이 됩니다.&lt;br /&gt;&lt;strong&gt;break point 1&lt;/strong&gt;에 있는 반복문의 내부에서 평균 시간 복잡도가 $O(1)$이므로, 전체의 평균 시간 복잡도는 $O(N)$입니다.&lt;/p&gt;

&lt;p&gt;퀵정렬에서 사용했던 것처럼, 평균 시간 복잡도에 동작하도록 배열을 섞어주면 TLE를 피할 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;재밌는-인터랙티브-문제&quot;&gt;재밌는 인터랙티브 문제&lt;/h3&gt;

&lt;p&gt;다음과 같은 인터렉티브 문제를 생각해봅시다.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;1e18 이하의 양의 정수로 이루어진 길이 N(1≤N≤1e5)짜리 배열 A가 있을 때, 여러분은 아래 question함수를 최대 103000번 이용해 배열의 최댓값을 구해야합니다.&lt;/p&gt;

  &lt;p&gt;bool question(int x, long long v) : $A_x ≥ v$ 여부 반환&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Naive한 방법으로는, 각 수에 대해 이분탐색을 진행해 값을 알아내서 최댓값을 구하면 됩니다. question함수를 $O(N log 10^{18})$번 호출하게 됩니다.&lt;/p&gt;

&lt;p&gt;여기에서 약간의 커팅을 시도할 수 있습니다. 현재까지 봤던 수의 최댓값보다 작거나 같은 수에 대해서는 굳이 이분 탐색을 하지 않아도 됩니다.&lt;br /&gt;만약 배열이 내림차순으로 정렬된 경우에는 $N + O(log 10^{18})$번으로 커팅이 잘 되며, 오름차순으로 정렬된 경우에는 $O(N log 10^{18})$번으로 커팅이 되지 않습니다. 평균적인 경우에는 어떨지 알아봅시다.&lt;/p&gt;

&lt;p&gt;일단 최댓값을 찾은 경우, 오른쪽에 있는 수에 대해서는 이분 탐색을 하지 않아도 됩니다. 최댓값 위치의 기댓값은 배열의 중앙입니다. 최댓값보다 왼쪽에 있는 수를 봅시다.&lt;br /&gt;최댓값보다 왼쪽에 있는 수 중에서 가장 큰 값을 만나게 되면, 배열 전체의 최댓값을 만나기 전까지는 이분 탐색을 수행하지 않습니다. 이러한 값의 위치의 기댓값은 첫 번째 원소와 최댓값의 중앙입니다.&lt;/p&gt;

&lt;p&gt;이런식으로 탐색할 구간에서 최댓값 위치의 기댓값이 중앙이라는 것을 이용해 평균적으로 $O(log N)$번만 이분 탐색을 하면 된다는 것을 알 수 있고, 평균적으로 $N + O(log N log 10^{18})$번의 함수 호출로 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;위 2문제에서 했던 것처럼, 탐색 순서를 섞어주는 것으로 평균 복잡도에 동작하도록 만들 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;boj18192-보고-정렬&quot;&gt;BOJ18192 보고 정렬&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/18192&quot;&gt;문제 링크&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;이 문제는 랜덤이 모든 경우를 같은 확률로 발생시킨다는 성질을 이용해 해결합니다.&lt;/p&gt;

&lt;p&gt;앞쪽부터 하나씩 맞춰가는 방식을 이용하면 $O(N log N)$번의 함수 호출로 문제를 풀 수 있습니다.&lt;br /&gt;간략하게 알고리즘을 소개하자면 $A_i ≠ i$인 최소 $i$를 찾은 뒤, $A_j = i$를 찾아 $[i, j]$ 구간을 섞어서 $A_i = i$로 만드는 것을 반복하면 됩니다.&lt;/p&gt;

&lt;p&gt;$i$를 하나씩 맞춰가는 방식으로 진행이 되며, 총 셔플 횟수는 $\displaystyle \sum_{i=0}^{N-1} (i를\space 맞추는데\space  필요한\space  셔플\space  횟수)$ 가 됩니다.&lt;br /&gt;어떤 수를 왼쪽으로 n칸 옮기는데 필요한 셔플 횟수의 기댓값 $D_n$은 $\displaystyle D_n = \frac {1}{n+1}(\sum_{k=1}^{n}D_k)+1$이고, 식을 정리하면 약 $2 + ln\space n$정도입니다.&lt;/p&gt;

&lt;p&gt;그러므로 $O(N log N)$번의 셔플로 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &quot;bogoSort.h&quot;
#include &amp;lt;bits/stdc++.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;chk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sort_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;shuffle_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;boj10523-직선-찾기&quot;&gt;BOJ10523 직선 찾기&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/10523&quot;&gt;문제 링크&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;이 문제에서는 랜덤을 이용해 적당한 답을 구하고, 그것을 반복해 틀린 답을 구할 확률을 줄입니다.&lt;br /&gt;이 문제에서 주목해야할 점은 $p≥20$이라는 조건입니다.&lt;/p&gt;

&lt;p&gt;이 문제에서 p%, 즉 $\frac {Np}{100}$개 이상이 한 직선 위에 있는지 판단해야 합니다. 그런 직선이 존재하는 경우, 랜덤으로 두 점을 잡아 직선을 만들 때 정답이 되는 직선을 찾을 확률을 알아봅시다.&lt;/p&gt;

&lt;p&gt;N개의 점으로 만들 수 있는 직선의 개수는 최대 $\frac {N(N-1)}{2}$입니다. 그중에서 $\frac {Np}{100}$개의 점으로만 만들 수 있는 직선의 개수는 $\frac {\frac {Np}{100}(\frac {Np}{100}-1)}{2} = \frac {N^2p^2-100Np}{20000}$입니다.&lt;br /&gt;그러므로 정답이 되는 직선을 찾을 확률은 $\frac {Np^2-100p}{10000(N-1)}$입니다. 이때 $p≥20$이기 때문에 정답이 되는 직선을 찾을 확률은 $\frac {N-5}{25(N-1)}$ 이상이 되므로 찾지 못할 확률은 약 $\frac {24}{25}$가 됩니다.&lt;/p&gt;

&lt;p&gt;이렇게 우리는 랜덤으로 두 점을 잡아 $O(N)$만에 확인하는, 96%의 확률로 틀리는 알고리즘을 만들었습니다!&lt;br /&gt;쓸모없을 것 같지만, K번 반복해주면 틀릴 확률이 $(\frac {24}{25})^K$가 됩니다. K가 커질수록 맞을 확률이 올라가며, 시간 복잡도는 $O(KN)$입니다.&lt;/p&gt;

&lt;p&gt;K번 반복을 해서 $\frac {Np}{100}$개 이상의 점을 포함하는 직선을 발견했다면 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;possible&lt;/code&gt;을 출력하고, 발견하지 못했다면 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impossible&lt;/code&gt;를 출력해주면 됩니다.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
#define x first
#define y second
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chrono&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steady_clock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_since_epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;per&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;want&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ccw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ccw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ios_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sync_with_stdio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;per&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;per&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;want&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;per&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;want&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;per&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;possible&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;uniform_int_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;want&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;possible&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;impossible&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;boj2912-백설공주와-난쟁이&quot;&gt;BOJ2912 백설공주와 난쟁이&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2912&quot;&gt;문제 링크&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;이 문제도 랜덤을 이용해 틀릴 확률을 줄이는 방식을 이용합니다.&lt;/p&gt;

&lt;p&gt;쿼리로 들어온 구간의 과반수가 같은 수로 이루어진 경우, 랜덤으로 원소를 선택하면 $\frac {1}{2}$ 이상의 확률로 과반수를 차지하는 수를 선택하게 됩니다. 즉, 틀릴 확률은 $\frac {1}{2}$ 이하가 됩니다.&lt;/p&gt;

&lt;p&gt;틀릴 확률이 50%인 알고리즘이지만, K번 반복하면 틀릴 확률이 $\frac {1}{2^K}$인 아주 좋은 알고리즘이 됩니다.&lt;/p&gt;

&lt;p&gt;각 수마다 등장하는 인덱스를 정렬된 상태로 저장하면, 특정 구간에 해당 수가 몇 번 나오는지 $O(log N)$만에 알 수 있으므로, 전체 시간 복잡도는 $O(KQlogN)$입니다.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;303030&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10101&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chrono&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steady_clock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_since_epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ios_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sync_with_stdio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;uniform_int_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
			&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
			&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;no&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;yes &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;더-알아보기&quot;&gt;더 알아보기&lt;/h3&gt;

&lt;p&gt;Global Min Cut을 랜덤을 사용해 $O(V^2 log^3 V)$만에 $\frac {V-1}{V}$의 확률로 정확한 답을 구할 수 있는 방법이 있습니다. (&lt;a href=&quot;http://www.secmem.org/blog/2019/10/20/Kargers-Algorithm/&quot;&gt;링크&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Delaunay Triangulation을 랜덤을 사용해 평균 $O(N log log N)$에 구할 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;참고-문헌&quot;&gt;참고 문헌&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.secmem.org/blog/2019/04/08/Smallest-Enclosing-Circle/&quot;&gt;삼성 소프트웨어 멤버십 블로그 - 최소 외접원 찾기&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://codeforces.com/blog/entry/62602&quot;&gt;Blogewoosh #6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://youngyojun.github.io/contest/review/2020/02/15/iamcoder-2019-yearend-contest/&quot;&gt;나는코더다 2019 송년대회 풀이&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 23 Mar 2020 02:27:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/03/23/random/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/03/23/random/</guid>
        
        <category>random</category>
        
        
      </item>
    
      <item>
        <title>Dual</title>
        <description>&lt;p&gt;두 번째 주의 AlgoShitPo에서 &lt;strong&gt;dual of a planar graph&lt;/strong&gt;를 작성하게 된 ahgus89입니다.&lt;/p&gt;

&lt;h3 id=&quot;평면-그래프planar-graph&quot;&gt;평면 그래프(planar graph)&lt;/h3&gt;
&lt;p&gt;평면 그래프란, 이름처럼 평면 위에 놓여져 있는 그래프를 의미합니다. 정확히는, 적당한 평면이 존재하여 어떤 두 간선도 정점을 제외하고 교차하지 않는 그래프를 말합니다.&lt;/p&gt;

&lt;p&gt;유명한 평면 그래프와, 그렇지 않은 것들의 예시로는 다음과 같은 것들이 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/planar.png&quot; alt=&quot;&quot; /&gt; &lt;img src=&quot;https://algoshitpo.github.io/files/non_planar.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;$K_5$나 $K_{3, 3}$을 minor로 가지는 것과 평면 그래프가 아님이 동치임이 알려져 있습니다.(Wagner’s theorem)&lt;/p&gt;

&lt;p&gt;평면 그래프에서 성립하는 유명한 관계식이 몇 가지 있습니다.&lt;/p&gt;

&lt;p&gt;단순 연결 평면 그래프가 $v \geq 3$개의 정점, $e$개의 간선, $f$개의 면을 가질 때 다음과 같은 관계식이 성립합니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$e \leq 3v-6$&lt;/li&gt;
  &lt;li&gt;길이 3인 사이클(삼각형)이 없으면, $e \leq 2v-4$&lt;/li&gt;
  &lt;li&gt;$f \leq 2v-4$&lt;/li&gt;
  &lt;li&gt;$v-e+f=2$ (&lt;strong&gt;Euler characteristic&lt;/strong&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이처럼 평면 그래프의 특수성 때문에 일반적인 그래프와는 다르게 $e = O(v)$가 성립합니다.&lt;/p&gt;

&lt;p&gt;평면 그래프의 성질은 정말 다양한 것이 많지만, 이 글에서 다 다루지는 않겠습니다. 더 다양한 내용은 &lt;a href=&quot;https://en.wikipedia.org/wiki/Planar_graph&quot;&gt;영문 위키&lt;/a&gt;에 잘 서술되어 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;듀얼-그래프dual-graph&quot;&gt;듀얼 그래프(dual graph)&lt;/h3&gt;
&lt;p&gt;평면 그래프 $G$의 듀얼이란, $G$의 각 면을 정점으로 가지고, $G$의 간선으로 이웃한 두 면을 연결하는 간선을 가지는 그래프를 의미합니다. 앞서 설명한 평면 그래프의 듀얼은 다음과 같습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/dual.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;$G$의 듀얼 그래프는 $f$개의 정점, $e$개의 간선, $v$개의 면으로 이루어져 있으며 이 그래프의 듀얼은 다시 $G$가 됩니다.
듀얼 그래프는 위 그림처럼 multiple edge나 self-loop가 존재할 수 있지만, 역시 평면 그래프입니다. 또한, 모든 면이 간선을 통해 인접해 있으므로 항상 연결 그래프가 됩니다.&lt;/p&gt;

&lt;p&gt;PS에서 유명한 dual 관계는 &lt;a href=&quot;https://en.wikipedia.org/wiki/Voronoi_diagram&quot;&gt;Voronoi diagram&lt;/a&gt;과 &lt;a href=&quot;https://en.wikipedia.org/wiki/Delaunay_triangulation&quot;&gt;Delaunay triangulation&lt;/a&gt;이 있습니다.&lt;/p&gt;

&lt;p&gt;듀얼 그래프 역시 더 많은 정보를 원한다면 &lt;a href=&quot;https://en.wikipedia.org/wiki/Dual_graph&quot;&gt;영문 위키&lt;/a&gt;를 참조하시기 바랍니다. 문제풀이에 있어서는 듀얼의 개념만 알아도 충분합니다.&lt;/p&gt;

&lt;h3 id=&quot;문제&quot;&gt;문제&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15308&quot;&gt;이 문제&lt;/a&gt;를 풀어봅시다.&lt;/p&gt;

&lt;p&gt;간단하게 요약하면, 평면 그래프와 $Q$개의 점이 주어지면 이전 점에서 현재 점으로 가는 최단 시간을 구하는 문제인데, 이때 그래프의 같은 면 내에서 이동할 때는 시간이 0, 간선으로 인접한 면으로 이동할 때는 $h_i$만큼의 시간이 듭니다.&lt;/p&gt;

&lt;h2 id=&quot;생각&quot;&gt;생각&lt;/h2&gt;
&lt;p&gt;면에서 면으로의 이동은, 주어진 그래프의 듀얼 그래프에서 정점에서 정점으로의 이동으로 생각할 수 있습니다.&lt;/p&gt;

&lt;p&gt;그렇기 때문에 듀얼 그래프에서 정점 사이의 최단거리들을 모두 전처리하고, 각 점이 듀얼 그래프의 어떤 정점에 속하는지 모두 구한다면 쿼리당 $O(1)$에 쉽게 처리할 수 있을 것입니다.&lt;/p&gt;

&lt;p&gt;문제의 제한을 보면, $N-1 \leq M \leq N+100$이기 때문에, Euler characteristic을 생각해 보면 $f \leq 102$입니다. 따라서 듀얼 그래프만 구한다면 최단거리는 플로이드 워셜로 $O(f^3)$ 정도의 복잡도라도 충분함을 알 수 있습니다.&lt;/p&gt;

&lt;p&gt;풀이를 우선 정리하면 다음과 같습니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;주어진 평면 그래프의 듀얼 그래프를 만든다.&lt;/li&gt;
  &lt;li&gt;듀얼 그래프에서 플로이드 워셜로 최단거리 쌍을 모두 구한다.&lt;/li&gt;
  &lt;li&gt;쿼리로 주어진 점들이 듀얼 그래프에서 어떤 정점에 속하는지, 즉 원본 그래프의 어떤 면에 속하는지 구한다.&lt;/li&gt;
  &lt;li&gt;2와 3에서 구한 정보로 쿼리에 대한 답을 출력한다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2와 4는 아주 간단하게 할 수 있고, 우리는 1과 3을 $200,000$ 정도의 제한 안에서 잘 해낼 방법을 찾아야 합니다.&lt;/p&gt;

&lt;h3 id=&quot;듀얼-그래프-만들기&quot;&gt;듀얼 그래프 만들기&lt;/h3&gt;
&lt;p&gt;듀얼 그래프를 만드는 방법을 알아봅시다.&lt;/p&gt;

&lt;p&gt;듀얼 그래프의 간선은, 원본 그래프의 간선의 양쪽에 있는 (같을 수도 있는)두 면을 연결합니다. 따라서 일단 $2e$개의 정점을 만들고, 같은 정점을 합쳐주는 방식으로 듀얼 그래프를 만들 수 있습니다.&lt;/p&gt;

&lt;p&gt;예제를 예를 들어 설명하면, 우선 다음과 같은 그래프가 생깁니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/dual1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;또한 한 점에 연결된 간선들만을 본다면, 아래 그림과 같이 연결된 정점들을 각도 순으로 정렬하면, 인접한 두 정점 사이의 영역은 유일하므로, 두 정점과 연결된 간선으로 나눠진 부분을 듀얼에서의 같은 정점으로 합쳐줄 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/dual2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;정점을 합치는 과정은 Union-find로 구현할 수 있습니다.&lt;/p&gt;

&lt;p&gt;따라서 듀얼 그래프를 만드는 과정의 시간 복잡도는, $N$개의 정점에 대해 각각 인접한 점들과 각도 정렬을 하고, Union-find로 합쳐주는 것만이 필요하므로, 총 $O(NlogN)$ 시간 안에 구할 수 있습니다.&lt;/p&gt;

&lt;p&gt;한 점 기준으로의 각도 정렬, Union-find를 구현해야 하고, 그 외에도 합쳐줘야 할 점이 간선을 기준으로 어느 쪽인지 판별하기 쉽지 않아 구현이 까다로울 수 있습니다.&lt;/p&gt;

&lt;p&gt;이때 외부에 해당하는 정점을 미리 구해두면 편리할 수 있습니다. $x$좌표가 가장 작은 정점에 대해 각도가 가장 큰 간선의 아래쪽이 외부에 해당하게 됩니다.&lt;/p&gt;

&lt;h3 id=&quot;주어진-점이-듀얼-그래프의-어떤-정점에-속하는지-구하기&quot;&gt;주어진 점이 듀얼 그래프의 어떤 정점에 속하는지 구하기&lt;/h3&gt;
&lt;p&gt;정점 $P_i (x_i, y_i)$에 대해, 직선 $x=x_i$와 교차하는 모든 선분들을 그 지점에서의 $y$좌표 순으로 정렬된 상태로 저장하고 있으면, lower_bound로 $P_i$ 위의 가장 가까운 선분을 구할 수 있고, 따라서 $P_i$는 그 선분의 아래쪽에 해당하는 영역에 포함됩니다.&lt;/p&gt;

&lt;p&gt;$P_i$보다 위에 있는 선분이 없다면($x=x_i$와 교차하는 선분이 없는 경우 등), $P_i$는 외부 면에 해당하는 영역에 포함됩니다. 미리 구해둔 외부 영역을 알고 있기 때문에 쉽게 할 수 있습니다.&lt;/p&gt;

&lt;p&gt;하지만 이 방법 그대로 구현하면, $O(NMlogM)$ 시간이 걸리게 됩니다.&lt;/p&gt;

&lt;p&gt;시간복잡도를 줄이기 위해 주어진 점들을 모두 입력 받은 뒤, $x$축으로 sweeping 하면서 오프라인으로 처리해줄 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;주어진 그래프가 평면 그래프이기 때문에 교차하는 두 선분은 존재하지 않고, 그렇기 때문에 $x$좌표가 변화하더라도 $y$좌표의 크기 관계는 변화하지 않습니다.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;따라서 std::set을 이용해, 원본 그래프의 정점이 주어진다면 그 정점과 연결된 간선에 대해 이미 set에 들어있는 간선은 지우고, 그렇지 않은 간선은 삽입하여 현재 $x$좌표를 지나는 선분들을 모두 $y$좌표 순으로 정렬하여 저장해 둘 수 있습니다. 이때 $y$축과 평행한 선분은 무시해줍니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://algoshitpo.github.io/files/dual3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;이때 주의할 점이 있는데, 그 정점은 모든 선분이 교차하는 지점이므로, 선분이 $y$좌표 순으로 정렬이 되지 않습니다.&lt;/p&gt;

&lt;p&gt;이미 존재하는 선분을 삭제할 때는 선분이 삽입될 때 $x$좌표가 현재보다 작았고, 새로 선분을 추가할 때는 앞으로 더 큰 $x$좌표에서 보게 될 것이므로, $x$좌표를 $(현재 x좌표-0.5)$로 둔 상태로 선분을 먼저 삭제한 뒤, $x$좌표를 $(현재 x좌표+0.5)$로 둔 상태로 선분을 추가하면 $y$좌표 순으로 정렬할 수 있습니다.&lt;/p&gt;

&lt;p&gt;각 간선은 최대 한번씩만 넣고 빠지므로, 총 시간복잡도는 점 $x$좌표 순 정렬에 $O((Q+N)log(Q+N))$, 간선 삽입/삭제에 $O(MlogM)$, 쿼리 처리에 $O(QlogM)$의 시간에 해결할 수 있습니다. 따라서 전체 문제를 풀 수 있습니다.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;정답 코드&lt;/summary&gt;

  &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;bits/stdc++.h&amp;gt;
#define x first
#define y second
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202020&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202020&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202020&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202020&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;404040&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;uni&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ccw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ccw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;202020&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ios_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sync_with_stdio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;404040&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
		&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//각도 정렬&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;//간선의 어느쪽인지 판별&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;uni&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;uni&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//외부&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;//플로이드&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;ll&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//삭제 먼저&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;			
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;nx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//이후 삽입&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;vt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;			
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/details&gt;

&lt;p&gt;제 구현 외에도 &lt;a href=&quot;https://github.com/Namnamseo/iamcoder-goodbye-2017/blob/master/D%20%EB%B9%84%EB%B0%80%20%EC%9A%94%EC%9B%90/sol.cpp&quot;&gt;출제자의 코드&lt;/a&gt; 역시 한번 참고해 보시면 좋을 것 같습니다.&lt;/p&gt;

&lt;p&gt;###연습 문제
&lt;a href=&quot;https://www.acmicpc.net/problem/13145&quot;&gt;BOJ 13145 Masonry Bridge&lt;/a&gt;&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;풀이의 방향&lt;/summary&gt;

  &lt;p&gt;연결 시간의 최솟값과 최댓값을 구하는 문제인데, 최솟값의 경우 다익스트라로 쉽게 구할 수 있습니다.&lt;/p&gt;

  &lt;p&gt;최댓값이 문제인데, 듀얼 그래프를 생각해봅시다. 이때, 직선 $x=x_1$과 $x=x_N$을 추가로 그어 가장 바깥쪽 영역을 위와 아래로 나누어 줍니다. $1$번 점과 $N$번 점은 각각 $x$좌표가 최소, 최대이므로 이렇게 나눌 수 있습니다.&lt;/p&gt;

  &lt;p&gt;듀얼 그래프의 간선 중 원본 그래프에서 연결되지 않은 간선만을 포함하는 그래프를 생각하면, $1$번 점과 $N$번 점이 연결되기 직전에는, 위에서 나눈 ‘위’와 ‘아래’ 영역을 그 그래프의 간선만으로 이동할 수 있고, 연결되는 순간부터는 이동할 수 없습니다.&lt;/p&gt;

  &lt;p&gt;마지막으로 연결된 간선이 듀얼 그래프의 정점 $A$와 $B$를 연결한다면, 소요 시간은 $(모든 \space 간선 \space 가중치의 \space 합)-(“위”에서 \space A까지의 \space 최단 \space 거리)-(B에서 \space “아래”\space 까지의 \space 최단 \space 거리)$가 됩니다. $1$번 정점과 $N$번 정점에서 다익스트라 알고리즘으로 최단거리를 구해 이 값 또한 구해낼 수 있습니다.&lt;/p&gt;

  &lt;p&gt;$M \leq 10^6$이라는 제한이 빡세 보일 수 있지만, 위에서 언급한 평면 그래프의 성질 때문에 실제로 $M \leq 150000$ 정도입니다.&lt;/p&gt;

&lt;/details&gt;
</description>
        <pubDate>Mon, 23 Mar 2020 02:25:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/03/23/dual/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/03/23/dual/</guid>
        
        <category>graph</category>
        
        <category>geometry</category>
        
        
      </item>
    
      <item>
        <title>Potential Method</title>
        <description>\[\newcommand{\PhiInsert}{\Phi_{insert}}
\newcommand{\PhiDelete}{\Phi_{delete}}
\newcommand{\size}{\mbox{size}}
\newcommand{\capa}{\mbox{capacity}}
\newcommand{\di}{D_{i}}
\newcommand{\dim}{D_{i-1}}
\newcommand{\zig}{\mbox{Zig}}
\newcommand{\zzig}{\mbox{Zig-zig}}
\newcommand{\zzag}{\mbox{Zig-zag}}
\newcommand{\bf}{\mbox{Before}}
\newcommand{\aft}{\mbox{After}}
\newcommand{\subt}{\mbox{Subtrees}}\]

&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;

&lt;p&gt;Potential Method가 뭘까요? Potential Method는 우리가 흔히 사용하는 Amortized 분석과 큰 연관성을 가지고 있습니다. 어떠한 자료구조의 상태가 있을 때, 그 자료구조가 잘 작동하기 위한 ‘이상적인’ 상태가 존재합니다. 하지만 일반적으로 이 이상적인 상태를 유지하는 것은 그렇게 쉽지 않습니다. 항상 자료구조를 이상적인 상태로 유지하는 것은 굉장히 많은 비용을 요구합니다. 생각해 볼 수 있는 대안은, 자료구조를 매번 갱신하는 것이 아니라 적당히 비효율적인 연산을 하다가, 한 번에 효율적인 상태로 자료구조를 갱신해주는 것입니다. 이 둘이 균형을 이룰수록 전체적인 시간복잡도는 낮아지게 됩니다. 이 때 비효율적인 연산과 효율적인 상태로 자료구조를 갱신하는 것을 합쳐 평균적으로 특정 복잡도 이하가 됨을 보장할 수 있다면, 이를 일반적으로 Amortized 복잡도라고 합니다.&lt;/p&gt;

&lt;p&gt;대표적인 Amortized 복잡도의 예로는 Array Doubling 기법이 있습니다. Array Doubling 기법은 동적 배열을 할당할 때 현재 배열 크기의 두 배씩 할당하고, 줄일 때는 4분의 3 이상 비어 있어야 크기를 절반으로 줄이는 것입니다. 어떤 배열에 원소를 집어넣을 때 만약 배열이 꽉 찬다면 그 배열을 전부 새로운 연속된 메모리를 할당하여 이동해야 합니다. 하지만 이 때 메모리를 추가로 얼마정도 잡아줘야 효율적으로 배열을 확장할 수 있을까요? 앞에서 언급했듯이 자신의 크기만큼 잡아주는게 최적입니다. 배열의 크기가 $N$일 때 새로 추가되는 공간의 크기는 $N$이고, 최대 $N$개의 원소 추가 쿼리를 $O(1)$에 수용할 수 있으며, 한 번 공간을 추가할 때 시간은 $O(N)$만큼 듭니다. 따라서 결과적으로 한 개의 원소를 추가할 때, 평균적으로는 $O(1)$ 시간에 쿼리를 처리할 수 있습니다. 지울 때도 마찬가지입니다.&lt;/p&gt;

&lt;p&gt;이러한 연산을 진행할 때, 자료구조가 얼마나 ‘망가져’ 있는지를 나타내는 지표를 Potential이라고 합니다. 자료구조가 에너지를 가지고 있다고 생각하고 현실처럼 에너지가 낮을 때 안정하다고 합시다. 자료구조가 Potential이 높은 상태라면, 안정하지 못한 상태입니다. 우리는 여기에 특정한 연산을 적용해서 Potential을 낮출 수 있습니다. Potential이 높으면, 연산을 적용하는 데 일반적으로 시간이 더 많이 걸립니다. 이 Potential을 적당히 유지하면서 자료구조를 관리하기 위해서는 Potential이라는 개념을 정량적으로 관리할 수 있어야 합니다.&lt;/p&gt;

&lt;h2 id=&quot;potential의-수학적-정의&quot;&gt;Potential의 수학적 정의&lt;/h2&gt;

&lt;p&gt;함수 $ \Phi $ 를 자료구조의 상태, 즉 포텐셜을 나타내기 위한 함수로 정의할 수 있습니다. 이 함수는 자료구조의 상태를 음이 아닌 정수로 변환해줍니다. $D$가 자료구조의 상태라고 하면, $ \Phi (D) $는 이 자료구조의 현재 Potential Energy를 나타냅니다. 다른 말로는, 자료구조가 얼마나 ‘이상적인 상태’에서 먼지를 나타내기도 합니다. $\Phi$는 자유롭게 잡을 수 있지만, $\Phi(D_0) = 0$이여야 하며 $\Phi (\di) \ge 0$ 이어야 합니다. 이 조건을 만족하는 선에서 후술할 amortized time이 최대한 균일하게 유지될 수 있도록 함수를 적당히 설정해 주면 됩니다.&lt;/p&gt;

&lt;p&gt;$o$를 자료구조에서 수행하는 연산 중 하나라고 합시다. $\space _{before}$을 연산 $o$를 수행하기 전 자료구조의 상태, $\space  _{after}$을 연산 $o$를 수행한 이후의 자료구조의 상태라고 할 때, $ \Phi $에 대해 연산 $o$의 amortized time은 다음과 같습니다.
\(T_{amortized}(o) = T_{actual}(o)+C \cdot (\Phi(D_{after})-\Phi(D_{before}))\)
다시 말해, 연산 $o$의 amortized time은 단순하게 연산 $o$를 수행하는 시간 뿐만 아니라, 이 연산을 통해서 앞으로의 자료구조가 얼마나 더 효율적으로/비효율적으로 변하는지까지 반영하고 있습니다. 상수 $C$는 음이 아닌 정수로 상태 간의 potential과 실제로 수행해야 하는 연산에 대한 비례 상수를 나타냅니다만, 일반적으로 Big O Notation에서는 상수를 생략하므로 보통 $C$ 또한 생략됩니다.&lt;/p&gt;

&lt;p&gt;Potential을 사용해서 amortized time을 다음과 같이 적용할 수 있다는 것은 알았습니다. 그렇다면 이 amortized time을 실제 문제에는 어떻게 적용할 수 있을까요?  다음과 같이 생각해 봅시다. 자료구조에 대해 연산 순열 $O = o_1, o_2, \cdots ,o_n$을 적용한다고 할 때 총 amortized time은 $ \displaystyle T_{amortized}(O) = \sum_{i=0}^{n} T_{amortized}(o_i)$ 입니다. 그리고 실제 수행 시간은 $ \displaystyle T_{actual}(O) = \sum_{i=0}^{n} T_{actual}(o_i)$ 가 됩니다.여기서 $T_{amortized}(o)$를 위에 서술한 정의대로 풀어 보면 다음과 같게 됩니다.
\(\displaystyle T_{amortized}(O) = \sum _{i=1} ^{n} (T_{actual}(o_i) + C \cdot (\Phi (S_i) - \Phi(S_{i-1}))) = T_{actual}(O) + C \cdot (\Phi (S_n) - \Phi(S_0))\)
$D_i$를 $i$번 연산을 수행하고 나서의 자료구조의 상태라고 하면, 식에서 $D_i$가 모두 상쇄되어 사라지면서 처음 항과 마지막 항만이 남게 됩니다. 이때 $\Phi (D_0) = 0$ 이고 $\Phi (D_n) \ge 0$이어서  언제나 $T_{actual}(O) \le T_{amortized}(O)$ 이 됩니다. 따라서 $T_{amortized}(O)$는 $T_{actual}(O)$보다 $C \cdot \Phi (D_n)$ 만큼만 큰, 상당히 정확한 상계로 활용할 수 있습니다. 시간복잡도를 측정하는 유용한 지표가 되는 것이죠.&lt;/p&gt;

&lt;h2 id=&quot;dynamic-array-problem&quot;&gt;Dynamic Array Problem&lt;/h2&gt;

&lt;p&gt;위에 설명했던 동적 배열의 할당을 예시로 들어서 이 개념을 이해해봅시다. Potential 함수를 정의하기 전에 먼저 Dynamic Array가 어떻게 동작하는 지 다시 한 번 정확하게 짚고 넘어가는 것이 좋겠군요.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;만약 배열이 모두 찼다면 현재 배열 크기의 두 배의 크기를 가진 새로운 배열에 복사해 넣습니다.&lt;/li&gt;
  &lt;li&gt;만약 배열이 4분의 1 이하로 차 있다면, 현재 배열 크기 절반의 크기를 가진 새로운 배열에 복사해 넣습니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;우리가 증명하고자 하는 것은 다음과 같습니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;비어 있는 Dynamic Array에 임의의 순서로 $n$번의 삽입과 삭제 연산을 수행했을 때, $O(n)$ 시간만이 걸립니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;incremental-dynamic-array&quot;&gt;Incremental Dynamic Array&lt;/h4&gt;

&lt;p&gt;먼저 삽입 연산만이 존재한다고 가정합시다. 그러면 우리가 증명하고자 하는 명제는&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;비어 있는 Dynamic Array에 $n$번의 삽입 연산을 수행했을 때, $O(n)$ 시간만이 걸립니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;가 됩니다. 이를 증명하기 위해서 적절한 Potential Function을 정의합시다.
\(\Phi _{insert} (D_i) = 2 \cdot  \mbox{size} (D_i) - \mbox {capacity}(D_i)\)
이때 $\PhiInsert(D_0) = 0$이고, 원소의 개수의 2배보다 배열의 크기가 클 수 없기 때문에 모든 $i$에 대해서 $\PhiInsert(D_i) \ge 0$ 입니다. 이제 원소가 추가될 때 배열의 Potential이 실제로 어떻게 변하는지 확인해봅시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;배열이 확장되지 않는 경우&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이 경우 $\capa(D_i)$ = $\capa (D_{i-1})$입니다. 연산을 수행하는 데 요구되는 실제 비용 $T_{actual}(o_i)=1$이고, 
\(\PhiInsert(D_i) - \PhiInsert(D_{i-1}) = (2 \cdot \size (D_i) - \capa (D_i)) - (2 \cdot \size(D_{i-1}) - \capa (D_{i-1})) = 2\)
입니다.  따라서 amortized time은 $T_{actual}(o_i) + (\PhiInsert(D_i)-\PhiInsert(D_{i-1})) = 3$ 입니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;배열이 확장되는 경우&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이 경우 $\capa (D_i) = 2 \cdot \capa (D_{i-1})$ 입니다. 현재 배열에 있는 원소들을 조금 더 넓은 여유 공간이 있는 곳으로 옮겨줘야 하고 새로운 원소 하나를 추가해야 하므로, 연산을 수행하는 데 요구하는 실제 비용인 $T_{actual} (o_i) = 1 + \capa(D_i)$ 입니다. 하지만 이렇게 한 연산의 비용이 늘어나는 만큼 요구되는 Potential의 양은 줄어듭니다. 보다 구체적으로,
\(\begin{eqnarray*} \PhiInsert (D_i) - \PhiInsert(D_i-1) &amp;amp;=&amp;amp; (2 \cdot \size (D_i) - \capa (D_{i})) - (2 \cdot \size(\dim) - \capa (\dim)) \\ &amp;amp;=&amp;amp; 2-\capa (D_i) + \capa (D_{i-1}) \\
&amp;amp;=&amp;amp; 2-\capa (D_{i-1}) \end{eqnarray*}\)
가 됩니다. 이 경우에도 amortized time은 위 둘의 합으로
\(\begin{eqnarray*} T_{actual}(o_i) + (\PhiInsert(D_i) - \PhiInsert(D_{i-1})) &amp;amp;=&amp;amp; 1 + \capa(D_{i-1}) + (2-\capa(D_{i-1})) \\ &amp;amp;=&amp;amp; 3 \end{eqnarray*}\)
이 됩니다.&lt;/p&gt;

&lt;p&gt;각 연산의 amortized time이 $3$으로 동일하므로, $n$번의 연산 이후에도 amortized time이 실제 수행 시간보다 항상 크다는 증명에 따라 실제 연산의 수행 횟수가 $3n$번을 초과하지 않습니다. 따라서 Big O notation으로, 각 연산의 수행 시간은 $O(1)$이 됩니다.&lt;/p&gt;

&lt;h4 id=&quot;fully-dynamic-array&quot;&gt;Fully Dynamic Array&lt;/h4&gt;

&lt;p&gt;이제 $\PhiInsert$가 아닌 삽입, 삭제가 모두 가능하다는 가정 하에서의 Potential Function $\Phi$를 정의해봅시다. 증명해야 하는 Lenma는 다음과 같고, 이를 위해 Potential Function을 다음과 같이 정의할 수 있습니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;비어 있는 Dynamic Array에 $n$번의 삽입 또는 삭제 연산을 임의의 순서로 수행했을 때, $O(n)$ 시간만이 걸립니다.&lt;/li&gt;
&lt;/ul&gt;

\[\Phi(D_i) = \left\{ \begin{array}{ll}
         2 \cdot \size(\di) - \capa(\di) &amp;amp;
         \mbox{if} \ \ 2 \cdot \size (\di) \ge \capa(\di) \\
        \capa(\di)/2 -\size(\di) &amp;amp;
        \mbox{if} \ \  \capa(\di)  &amp;gt; 2 \cdot \size(\di)
        \end{array} \right.\]

&lt;p&gt;이 함수는 Potential Function의 성질인 $\Phi (D_0)=0$, 그리고 $\Phi(\di) \ge 0$을 자명하게 만족합니다. 이제 실제 연산에서의 amortized time을 분석해 봅시다.&lt;/p&gt;

&lt;p&gt;먼저 원소를 삽입할 때는 위에 서술한 증명과 같이 potential time이 $3$이라는 것을 알 수 있습니다. 그렇다면 원소를 삭제할 때는 어떨까요? 이 경우 배열의 크기가 절반으로 줄어들게 되므로 $ \displaystyle \capa(\di) = \frac{1}{2} \capa(\dim)$입니다. 연산을 수행하는 데 필요한 비용은 배열 전체를 옮겨야 하므로 $\displaystyle T_{actual}(o) = \size (\di) = \frac{1}{4} \capa(\di)$입니다. 역시 이 때도 퍼텐셜이 달라집니다. 보다 구체적으로,
\(\begin{eqnarray*} \Phi (D_i) - \Phi(D_i-1) &amp;amp;=&amp;amp; (\capa(\di)/2 - \size (\di)) - (\capa(\dim)/2 - \size (\dim)) \\ &amp;amp;=&amp;amp; 1 + \capa(\di)/2 -\capa(\dim)/2 \\ &amp;amp;=&amp;amp; 1-\capa (D_{i}) / 4 \end{eqnarray*}\)
와 같습니다. 이 경우에 potential time을 분석해 보면 
\(\begin{eqnarray*}
T_{actual}(o_i) + (\Phi(D_i) - \Phi(D_{i-1})) &amp;amp;=&amp;amp; \capa(D_{i}) / 4 + (1-\capa(D_{i})/4) \\ &amp;amp;=&amp;amp; 1 \end{eqnarray*}\)
입니다.  결과적으로 삽입과 삭제에서 모두 amortized time이 $O(1)$이므로, Dynamic Array에서의 삽입과 삭제는 실제로도 amortized $O(1)$에 수행된다는 결론을 낼 수 있습니다.&lt;/p&gt;

&lt;h2 id=&quot;splay-tree&quot;&gt;Splay Tree&lt;/h2&gt;

&lt;p&gt;Splay Tree는 많은 문제에서 활용되는 이진 탐색 트리입니다. Splay Tree의 재밌는 점은 여타 다른 자가 균형 이진 탐색 트리와는 달리 항상 균형잡힌 꼴을 유지하고 있지 않다는 것입니다. 그럼에도 불구하고 Splay Tree가 쓰일 수 있는 이유는 Splay Tree는 고유의 Splay 연산을 통해서 기초 연산을 트리의 크기 $N$에 대해 amortized $O(\lg N)$ 시간에 수행할 수 있기 때문입니다. 이를 직관적으로 이해하기는 매우 쉽지 않습니다만, Potential Method를 통해서 이를 증명할 수 있습니다. 이 문단에서 보이고자 하는 바는, 다음 두 정리가 Splay Tree에서 성립하는 것을 보이는 것입니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Splay Tree에서의 Splay 연산은 amortized $O(\lg N)$ 시간에 수행 가능하다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Splay Tree에서의 삽입, 삭제, 검색 연산은 amortized $O(\lg N)$ 시간에 수행 가능하다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이 문단은 독자가 Splay Tree의 기본적인 작동 원리를 모두 안다고 가정하고 설명을 진행합니다. 일단 설명을 시작하기 전에 Potential Function을 정의하기 위한 몇 가지 표기법들을 정합니다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;이진 탐색 트리 $T$에 대하여 $S(x)$는 $x$를 루트로 하는 서브트리에 있는 정점 개수입니다.&lt;/li&gt;
  &lt;li&gt;이진 탐색 트리 $T$에 대하여 $x$의 rank인 $r(x)$는 $ \lfloor \lg S(x) \rfloor $로 정의됩니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;그 이후 Splay Tree $T$에 대한 Potential Function $\Phi (T)$를 정의합시다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Splay Tree $T$에 대하여 Potential Function $\displaystyle \Phi(T) = \sum_{x \in T} r(x) $ 입니다.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;splay-연산&quot;&gt;Splay 연산&lt;/h3&gt;

&lt;p&gt;Splay Tree의 시간복잡도를 보장해 주는 일등공신, Splay 연산의 시간복잡도부터 분석해야 나머지 작업이 쉬워집니다. 시간복잡도를 분석하기 전에 자료구조의 연산에 Dynamic Array에서 써먹었던 개념인 amortized time을 적용하는 것을, 앞으로는 amortized cost의 약자인 $AC(o)$로 줄여 쓰도록 하겠습니다. 우리가 처음 참임을 보일 정리는,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$AC(Splay(x)) \le 3(r(root)-r(x))+1$ 이다.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;입니다.&lt;/p&gt;

&lt;p&gt;이 정리를 증명하기 위해서 간단한 보조정리들부터 시작합시다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lemma 1.&lt;/strong&gt; 정점 $x$가 두 자식 정점 $a$와 $b$를 가지고 $r(a)=r(b)=r$이라면, $r(x) &amp;gt; r$ 이다.&lt;/p&gt;

&lt;p&gt;Proof.  $S(x) = S(a)+S(b) + 1&amp;gt; S(a) + S(b) \ge 2^{r(a)} + 2^{r(b)} = 2^r + 2^r =2^{r+1}$이므로, $r(x) \ge r+1$이다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Corollary 2.&lt;/strong&gt; 정점 $x$가 두 자식 정점 $a$와 $b$를 가지고 $r(x) = r(a)$라면, $r(x) &amp;gt; r(b)$이다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Corollary 3.&lt;/strong&gt; 정점 $x$가 두 자식 정점 $a$와 $b$를 가지고 $r(x) = r(a)$라면, $2r(x) &amp;gt; r(a) +r(b)$ 이다.&lt;/p&gt;

&lt;p&gt;이제 이 당연한 사실들만 가지고 처음 정리를 증명하려고 시도해봅시다. Splay 연산은 Zig 스텝과 Zig-zig, Zig-zag 스텝으로 구성되어 있으므로, 이 스텝들을 각각 증명하면 도움이 크게 될 것 같습니다. $C$를 자식 정점, $P$를 부모 정점, $GP$를 부모 정점의 부모 정점이라고 할 때 Zig 스텝과 Zig-zig, Zig-zag 스텝에 대해서&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lemma 4.&lt;/strong&gt; $AC(\zig) \le 3(r(root)-r(C)) +1 $ 이다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lemma 5.&lt;/strong&gt; $AC(\zzig), AC(\zzag) \le 3(r(GP)-r(C))$ 이다.&lt;/p&gt;

&lt;p&gt;를 따로따로 증명해봅시다. Splay 연산에서 하나의 step을 진행할 때는 부모 정점이 루트이면 Zig, 그렇지 않다면 Zig-zig 또는 Zig-zag 연산을 반복적으로 수행하게 되므로, 이 연산 각각에 대한 amortized cost를 계산한 후 이 연산들을 연속해서 수행했을 때의 시간복잡도를 증명하면 됩니다.&lt;/p&gt;

&lt;p&gt;[이미지 1], [이미지 2]&lt;/p&gt;

&lt;p&gt;먼저 부모가 루트 정점일 때 수행되는 시간복잡도를 나타내는 &lt;strong&gt;Lemma 4&lt;/strong&gt;입니다. Zig 스텝에 의해서 왼쪽 트리가 오른쪽과 같이 바뀐다고 하면 서브트리 $A$, $B$, $C$는 그대로이고 $x$와 $p$의 위치만 바뀌게 됩니다. 연산을 수행하고 나서의 $x$와 $p$를 각각 $x’$와 $p’$이라고 하면, 트리 $T$와 연산 수행 후 바뀐 트리 $T’$의 포텐셜은 다음과 같이 변화하게 됩니다.
\(\begin{eqnarray*}
\Phi(T) &amp;amp;=&amp;amp; r(x) + r(p) + \Phi(A) + \Phi(B) + \Phi(C) \\ \Phi(T') &amp;amp;=&amp;amp; r(x') + r(p') + \Phi(A) + \Phi(B) + \Phi(C) \\
\end{eqnarray*}\)
$r(p) = r(x’)$ 이므로, $ \Delta \Phi = \Phi(T’) - \Phi(T) = r(p’) - r(x)$ 입니다. $AC(\zig)$는 정의에 따라 Potential의 변화에 실제로 Rotation을 수행하는 데 드는 단위 연산량 $1$을 더해 $1+\Delta \Phi = 1 + r(p’) - r(x)$일 텐데, 
\(\begin{eqnarray*}
&amp;amp;AC(\zig)&amp;amp; &amp;amp;=&amp;amp;1+r(p')-r(x) \\
&amp;amp;&amp;amp;&amp;amp;\le&amp;amp; 1 + r(p)- r(x) &amp;amp; \mbox{since} \ \ r(p) \ge r(p') \\
&amp;amp;&amp;amp;&amp;amp;\le&amp;amp; 3(r(p)-r(x))+1
\end{eqnarray*}\)
입니다. 여기서 우리가 수행한 $x$의 부모 정점은 루트이니 $p=root$이고, $x=C$가 되므로 &lt;strong&gt;Lemma 4&lt;/strong&gt;의 증명이 끝났습니다. 왜 굳이 3을 곱해서 부등식을 비효율적으로 만드냐고 생각할 수도 있지만, 이는 &lt;strong&gt;Lemma 5&lt;/strong&gt;를 증명하면 알 수 있습니다.&lt;/p&gt;

&lt;p&gt;부모가 루트 정점이 아닐 때 수행되는 시간복잡도를 나타내는 &lt;strong&gt;Lemma 5&lt;/strong&gt;입니다. 이 경우에는 Zig-zig나 Zig-zag 연산을 수행하면서 $GP$까지 동원하게 되는데요, 편의를 위해 연산 전 정점들의 rank를 $r=r(GP)$, $b=r(P)$, $a=r(C)$로 표현하도록 하겠습니다. $r\ge a$인 것은 자명하니 각각을 $r=a$인 경우와 $r&amp;gt;a$인 경우로 케이스를 나누어 생각하겠습니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$r&amp;gt;a$인 경우&lt;/li&gt;
&lt;/ol&gt;

\[\Phi(\mbox{Before}) = r+b+a+\Phi(\mbox{Subtrees}) \ge r+2a+\Phi(\mbox{Subtrees}) \\
\Phi(\mbox{After}) \le 3r+ \Phi(\mbox{Subtrees})\]

&lt;p&gt;임이 $r \ge b \ge a$임에 따라 자명합니다. 따라서
\(\Delta \le 3r-(r+2a)=2(r-a)\)
이고, 실제 수행 시간 $1$을 더해서
\(AC=1+\Delta \Phi \le 1+2(r-a) \le 3(r-a) = 3(r(GP)-r(c))\)
입니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$r=a$인 경우&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;$r \le b \le a$이면서 $r=a$이므로, $r=a=b$ 입니다. 따라서
\(\Phi (\bf) = r+b+a+\Phi(\subt) = 3r+\Phi(\subt)\)
입니다. 이제 Lemma 5를 증명하려면 $\Phi (\aft) \le 3r-1+\Phi(\subt)$인 것만 보이면 됩니다.&lt;/p&gt;

&lt;p&gt;만약 Zig-zag 연산을 수행한다면, 트리는 $C$를 루트로 하는 다음과 같은 구조로 변화하게 됩니다.&lt;/p&gt;

&lt;p&gt;[이미지 3]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Corollary 3&lt;/strong&gt;에 의해 $r(GP) + r(P) &amp;lt; 2(C)$임을 알 수 있습니다. 따라서
\(\Phi(\aft) = r(C)+r(GP)+r(P) + \Phi(\subt) \le 3r(C)-1+\Phi(\subt) = 3r-1+\Phi(\subt)\)
입니다. (rank는 모두 양의 정수이어서 성립합니다.)&lt;/p&gt;

&lt;p&gt;만약 Zig-zig 연산을 수행한다면, 트리는 이렇게 두 단계에 걸쳐 변화하게 됩니다.&lt;/p&gt;

&lt;p&gt;[이미지 4]&lt;/p&gt;

&lt;p&gt;일단 $C$를 루트로 하는 서브트리들에는 전혀 변함이 없습니다. 따라서 모든 상황에 대해 $r(C)=r$임을 알 수 있습니다. 가운데 상황에서 $r(P)$또한 첫 번째 상황에서의 $GP$와 같은 상황에 놓여 있으므로 $r(P)=r$입니다. 역시 가운데 상황에서, $GP$는 &lt;strong&gt;Corollary 2&lt;/strong&gt;에 의해 $r(P)=r(C)$이고 $P$의 두 자식 정점이 $C$와 $GP$ 이므로 $r(GP)&amp;lt;r$이 됩니다. 마지막 상황에서도 $GP$를 루트로 하는 서브트리들에는 변함이 없으므로, $r(GP)&amp;lt;r$이  유지됩니다. 이 사실들을 종합해 볼 때,
\(\begin{eqnarray*}
\Phi(\aft) &amp;amp;=&amp;amp; r(C) + r(P) + r(GP) + \Phi(\subt) \\ &amp;amp;\le&amp;amp; r+r+(r-1) + \Phi(\subt) \\ 
&amp;amp;=&amp;amp; 3r-1+\Phi(\subt)
\end{eqnarray*}\)
임을 알 수 있습니다.&lt;/p&gt;

&lt;p&gt;결과적으로  $\Delta \Phi = \Phi(\aft) - \Phi(\bf) \le -1$이니,
\(AC=1+\Delta \Phi \le 1+(-1) = 0 \le 3(r(GP)-r(C))\)
이 됩니다.&lt;/p&gt;

&lt;p&gt;이로서 &lt;strong&gt;Lemma 4&lt;/strong&gt;와 &lt;strong&gt;Lemma 5&lt;/strong&gt;가 모두 참이라는 것을 알았습니다. 그렇다면 이를 이제 Splay 연산에 어떻게 적용시킬 수 있을까를 알아야 하는데, Splay 연산은 이 연산들을 별개의 정점에 따로따로 적용하는 것이 아닌 하나의 정점에 반복적으로 적용한다는 사실을 알아야 합니다. 한 번의 Splay 연산이 $n$개의 스텝으로 구성될 경우 $n-1$번의 Zig-zig 스텝이나 Zig-zag 스텝을 거치게 되므로,
\(\displaystyle
\begin{eqnarray*}
AC(\mbox{Splay}(x)) &amp;amp;=&amp;amp; \sum_{i=1}^{n-1} ( AC(\zzig) ∨ AC(\zzag)) +AC(\zig)\\
&amp;amp;=&amp;amp; 3(r_1 -r_0) + 3(r_2 -r_1) +\cdots + 3(r_n -r_{n-1}) + 1 \\
&amp;amp;=&amp;amp; 3(r_n - r_0) +1 \\
&amp;amp;=&amp;amp; 3(r(root)-r(x))+1
\end{eqnarray*}\)
이 됩니다. 비로소 원래 목표로 하던 정리를 증명했습니다! 또한 이 정리로부터 다음 명제도 자연스레 이끌어낼 수 있습니다. (rank는 항상 트리의 정점 개수에 $\lg$를 씌운 것보다 클 수 없으니까요.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Corollary 6.&lt;/strong&gt; $AC(\mbox{Splay}(x)) \le 3 \lg  n +1$ 이다.&lt;/p&gt;

&lt;p&gt;따라서 Splay 연산은 amortized $O(\lg n)$ 이라는 결론을 이끌어 낼 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;기타-다른-연산&quot;&gt;기타 다른 연산&lt;/h3&gt;

&lt;p&gt;Splay의 시간복잡도가 $O(\lg n)$이라는 것을 깨달으면, 나머지 연산은 간단하게 해결됩니다. Splay Tree는 삽입과 삭제, 검색한 노드에 항상 Splay 연산을 진행해 줍니다. 이 세 연산의 시간복잡도는 항상 Splay 연산에 의해 Bound되게 됩니다. 그 이유는 매우 간단한데, 이 세 연산을 수행하는 동안 사용해야 하는 연산량이 반드시 그 정점에 대한 Splay 연산보다 시간복잡도가 같거나 더 작기 때문입니다. 모든 연산이 연산에서 Splay한 정점의 높이를 초과하는 시간복잡도를 가질 수 없다는 말과 같게 생각해도 좋습니다. 이 세 연산 외에도 Splay를 통해 Bound 될 수 있는 연산이라면, 항상 $O(\lg n)$ 복잡도로 수행할 수 있다는 것을 간단하게 증명할 수 있습니다.&lt;/p&gt;

&lt;h2 id=&quot;segment-tree-beats&quot;&gt;Segment Tree Beats&lt;/h2&gt;

&lt;p&gt;Segment Tree Beats는 일반적인 세그먼트 트리 Lazy Propagation에서 좀 더 나아가, 노드에 정보를 추가적으로 저장하고 이를 바탕으로 보다 복잡한 연산들의 시간복잡도를 bound 하는 기법입니다. 이 문단에서도 역시 독자가 세그먼트 트리 비츠의 기본적인 내용을 알고 있다고 간주하고 시간복잡도를 설명합니다. 세그먼트 트리 비츠를 대표할 만 한 2개의 문제의 시간복잡도만 분석하고 설명할 것입니다만, 다른 문제에도 비슷한 방식으로 접근하여 복잡도를 증명할 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;range-min-query&quot;&gt;Range Min Query&lt;/h3&gt;

&lt;p&gt;배열에서 다음과 같은 쿼리 두 개를 처리해야 합니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$i \in [l,r]$에 대해서 $A_i$를 $\min (A_i,x)$로 바꾼다.&lt;/li&gt;
  &lt;li&gt;$\displaystyle \sum_{i=L}^{R} A_i$를 구한다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이 두 쿼리에 대한 amortized time complexity는 $O(\lg n)$ 입니다. 이를 어떻게 증명할 수 있을까요? 이는 세그먼트 트리 비츠 특유의 tag와 break로 증명할 수 있습니다. 세그먼트 트리 비츠에서의 tag는 Lazy Propagation에서 사용하는 Lazy 값과 유사한 기능을 하는데요, 어떤 노드에 tag가 달려있다는 것은 그 노드가 담당하는 범위의 값을 그 tag로 대체할 수 있다는 뜻입니다.&lt;/p&gt;

&lt;p&gt;이 문제에서의 tag를 설정합시다. 맨 처음에는 자신이 담당하는 범위에서 가장 큰 값을 tag로 하고, 만약 자신의 부모 정점과 tag 값이 같다면 자신의 tag 값을 비운 채로 남깁니다. 이 글을 읽는 독자라면 이 문제를 세그먼트 트리 비츠로 해결할 때는 maximum 값과 second maximum 값을 같이 저장해서 문제를 해결한다는 것을 알고 계실겁니다. 방금 전에 설정한 것처럼 tag를 나타내면, 어떤 정점이 나타내는 구간에서의 second maximum 값은 서브트리에 있는 tag들 중 maximum인 것 또한 알 수 있습니다. 이 tag가 중요한 이유는, 이 tag가 서브트리에 존재하는 모든 tag보다 strict하게 크다는 성질이 1번 쿼리 이후에도 성립한다는 점입니다. 왜냐하면 $x$가 그 정점의 maximum 값 또는 second maximum 값보다 크다면 그 정점에 아무 일도 일어나지 않거나 그 정점의 tag만 변경될 것이고, 반대로 만약 어떤 정점의 second maximum보다 $x$가 작다면 직접 아래로 내려가서 조건을 만족하도록 정점에 대한 값들을 새로 갱신해 줄 것이기 때문입니다. 요약하면 이 성질이 만족되지 않을 때는 직접 하위 정점들을 방문해서 성질을 만족시키도록 값을 유지해 주니까, 어떤 정점에 tag를 다는 것은 서브트리에 있는 모든 정점들이 자신보다 작은 tag를 가지고 있는 경우 뿐입니다.&lt;/p&gt;

&lt;p&gt;그렇다면 이 tag가 언제 바뀌는지 알아봅시다. 첫 번째로 방금 전에 설명했듯이 1번 쿼리를 수행하면 조건을 만족시키지 않는 몇 가지 태그를 제거하고, 또 몇 개의 새로운 태그를 추가하게 될 것입니다. 그리고 Lazy Propagation처럼 위에 있는 tag가 아래에 영향을 미치게 된다면, 그 때도 아래에 있는 정점들을 향해 tag가 갱신이 되겠죠. (이를 앞으로 pushdown이라고 칭하겠습니다.)&lt;/p&gt;

&lt;p&gt;조금 더 분석을 원할하게 하기 위해, tag의 클래스를 다음과 같이 정의합시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;한 번의 구간 최솟값 쿼리를 통해서 붙여진 모든 태그는 같은 클래스에 속합니다.&lt;/li&gt;
  &lt;li&gt;pushdown 후에 내려간 새 tag는 기존에 있던 tag와 같은 클래스에 속합니다.&lt;/li&gt;
  &lt;li&gt;위 두 조건을 둘 다 만족시키지 못하는 두 tag는 다른 클래스에 속합니다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;또 1번 쿼리를 수행할 때 우리가 실제로 방문하는 정점들을 일반 정점, 조건에 걸려서(쿼리의 범위에 속하지 않아서) 방문하지 않는 정점들을 잉여 정점들이라고 칭하겠습니다.&lt;/p&gt;

&lt;p&gt;이러한 복잡한 과정을 걸쳐서 드디어 이 세그먼트 트리에 대한 Potential Function을 정의할 수 있습니다. tag 클래스 $T$에 대해 $w(T)$를 서브트리 안에 적어도 하나의 tag 클래스가 $T$인 정점을 가지고 있는 정점의 수라고 합시다.  또 Potential Function $\Phi (x)$를 존재하는 모든 tag 클래스에 대해 $w(T)$의 합으로 정의합시다.&lt;/p&gt;

&lt;p&gt;세그먼트 트리에 $n$개 초과의 tag가 존재할 수 없음은 너무 자명합니다. 따라서 $\Phi (x)$는 처음에 $O(n \lg n)$의 값을 가집니다. 이제 각 연산이 Potential Function을 어떻게 변화시키는지 살펴봅시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;pushdown 연산을 통해 tag가 자식 정점으로 내려가게 된다면, $\Phi(x)$는 $2$만큼 증가하게 됩니다.&lt;/li&gt;
  &lt;li&gt;tag는 항상 일반 정점에서만 갱신됩니다. 그래서 새롭게 나타나는 tag 클래스 $T$에 대해, $w(T)$는 $O(\lg n)$의 값을 가지게 됩니다. (일반 정점의 개수가 세그먼트 트리의 성질에 의거해 많아봐야 $O(\lg n)$이기 때문입니다.)&lt;/li&gt;
  &lt;li&gt;쿼리를 처리할 때 잉여 정점들을 방문하는 이유는 그 정점들의 서브트리에서 조건을 만족하지 못하는 tag 몇 개를 지워야 하기 때문입니다. 그리고 만약에 그런 서브트리에서 tag 클래스 $T$에 속하는 tag가 한 개라도 삭제된다면, 같은 tag 클래스에 속하는 tag들도 그 서브트리에서 모두 제거됨이 자명합니다. 따라서 잉여 정점을 하나 방문할 때마다, $\Phi(x)$는 적어도 $1$만큼 감소합니다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;$\Phi(x)$는 처음에 $O(n \lg n)$의 값을 가지고 $\Phi(x)$가 $O(n \lg n)$을 넘게 증가할 수 없습니다. 또 각 잉여 정점마다 $\Phi(x)$를 적어도 하나 감소시키니, 총 시간복잡도가 amortized $O(n \lg n)$이 된다는 것을 알 수 있습니다.&lt;/p&gt;

&lt;h3 id=&quot;more-complex-queries&quot;&gt;More Complex Queries&lt;/h3&gt;

&lt;p&gt;배열에서 다음과 같은 4가지 쿼리를 처리해야 합니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;$i \in [l,r]$에 대해서 $A_i$를 $\min (A_i,x)$로 바꾼다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;$i \in [l,r]$에 대해서 $A_i$를 $ \max (A_i,x)$로 바꾼다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;$i \in [l,r]$에 대해서 $A_i$를 $ A_i+x$로 바꾼다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;$\displaystyle \sum_{i=L}^{R} A_i$를 구한다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;이 문제에 대한 시간복잡도를 계산하기 위해서는 조금 더 tricky한 Potential Function을 잡아 줄 필요가 있습니다. tag $t$에 대해 $d(t)$를 $t$의 세그먼트 트리 내 깊이라고 하고, $\Phi(x)$를 모든 $d(t)$의 합이라고 합시다. 당연히 $d(t)$는 $O(\lg n)$이고, tag의 개수는 $n$을 초과하지 않습니다. 따라서 $\Phi(x)$의 초깃값은 $O(n\lg n)$입니다.&lt;/p&gt;

&lt;p&gt;이제 각 연산들이 Potential Function을 어떻게 변화시키는지 알아봅시다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;1번 쿼리를 수행하고 나서, 잉여 정점들에는 태그가 붙어있지 않을 것 입니다. 따라서 그냥 일반 정점들에 대해서 pushdown을 수행해 주는 것만 고려해주면 됩니다.  pushdown 이후에, 기존의 tag는 자식 정점 2개로 내려가게 되는데 이 때 $\Phi(x)$는 $O(\lg n)$ 만큼 증가할 것입니다. 세그먼트 트리의 구조에 의해 일반 정점은 최대 $O(\lg n)$개 있고, 따라서 pushdown은 매 쿼리마다 $O(\lg^2 n)$만큼 $\Phi(x)$를 증가시킬 것입니다. 이는 1번 쿼리가 아니라, 2번과 3번 쿼리에도 동일하게 적용할 수 있는 부분입니다.&lt;/li&gt;
  &lt;li&gt;새로운 tag는 항상 일반 정점에서만 나타나므로 역시 $\Phi(x)$를 $O(\lg^2 n)$만큼만 증가시킵니다.&lt;/li&gt;
  &lt;li&gt;각 잉여 정점은 $\Phi(x)$를 $1$만큼 감소시키는데, 잉여 정점에서 tag가 지워져도 그 정점보다 깊은 정점에 tag가 남아 있기 때문입니다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;$\Phi(x)$의 총 증가량이 $O(n \lg ^2 n)$이고, 각 잉여 정점이 $\Phi(x)$를 $1$만큼 감소시키니 총 시간복잡도는 amortized $O(n\lg^2n)$ 입니다. 가장 재밌는 점은 Potential Function의 변화량을 계산할 때 min/max, 또는 interval add/subtract 함수의 성질을 이용한 것이 아닌 오직 tag의 성질만을 이용했다는 점입니다. 그래서 꼭 이런 특수한 함수들이 아니더라도 tag에 동일한 영향을 끼치는 연산이라면 별도의 증명 없이 그대로 적용 가능합니다.&lt;/p&gt;

&lt;h2 id=&quot;글을-마치며&quot;&gt;글을 마치며&lt;/h2&gt;

&lt;p&gt;Potential Method는 amortized anaylsis를 진행할 때 사용할 수 있는 강력한 도구입니다. 물론 Potential Function을 설정하고 이를 통해 연산의 Potential Function 변화량을 측정하는 것이 상당히 까다로운 일이기는 하지만, 자명해보이지만 증명하기는 힘든 정리들을 증명할 때 복잡도를 bound해야 하는 연산들이 많아질수록Potential Function을 이용하여 증명하는 것은 개개의 연산은 Ad-hoc하게 증명하지 않아도 된다는 점에서 빛을 발합니다(개인적인 의견입니다만). 비록 Problem Solving에서는 amortized analysis를 사용해야 하는 문제가 그렇게 많지 않지만,  segment tree beats와 같은 자료구조를 구현할 때 Proof by AC로만 문제를 해결하려고 하지 말고 한 번쯤은 Potential Method와 같은 도구들을 활용해 복잡도를 확실하게 계산하고 문제를 풀어보기를 바랍니다. 이상 마칩니다.&lt;/p&gt;

</description>
        <pubDate>Mon, 23 Mar 2020 02:00:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/03/23/PotentialMethod/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/03/23/PotentialMethod/</guid>
        
        <category>Math</category>
        
        
      </item>
    
      <item>
        <title>eer tree</title>
        <description>&lt;p&gt;https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b 와 Rubinchik, M., &amp;amp; Shur, A. M. (2016). EERTREE: An Efficient Data Structure for Processing Palindromes in Strings. 를 참고하여 작성한 글임을 밝힙니다.&lt;/p&gt;

&lt;h4 id=&quot;intro&quot;&gt;Intro&lt;/h4&gt;

&lt;p&gt;ICPC 등의 알고리즘 경진 대회에는 문자열을 다루는 종류의 문제가 자주 출제됩니다. 이런 문자열 문제들을 풀다 보면, 특수한 문자열에 대한 깊은 고찰을 요구하는 상황들이 많이 등장합니다. 이런 특수한 문자열 중 팰린드롬은 오래 전부터 깊은 연구가 진행되어 왔고, 팰린드롬의 성질을 이용하여 관련 문제를 효율적으로 해결하는 다양한 알고리즘이 개발되었습니다. 대표적인 예시로 길이 $ n $인 문자열 $ S $의 최장 길이 팰린드롬을 $ O(n) $ 시간만에 구하는 Manacher’s Algorithm 이 있습니다.&lt;/p&gt;

&lt;p&gt;본 글에서 설명할 &lt;strong&gt;eertree (Palindromic Tree)&lt;/strong&gt;는 팰린드롬을 효율적으로 저장하는 자료구조입니다.&lt;/p&gt;

&lt;h4 id=&quot;terms&quot;&gt;Terms&lt;/h4&gt;

&lt;p&gt;본 글에서 문자열은 $ S[1…n] $ 로 나타내기로 합니다. 별다른 언급이 없으면 $ S $와 같은 표기는 $ S[1…n] $과 동일한 의미를 가집니다. $ S[i] $는 문자열 $ S $의 $ i $번째 문자를 나타냅니다.&lt;/p&gt;

&lt;p&gt;문자열 $ S $가 팰린드롬 (Palindrome) 인 것은 모든 $ 1 \leq i \leq n $ 에 대해 $ S[i] = S[n+1-i] $ 가 성립한다는 것과 동치입니다. 이때 길이가 0인 문자열 또한 팰린드롬으로 봅니다.&lt;/p&gt;

&lt;p&gt;$ 1 \leq i \leq j \leq n $ 에 대해 $ T = S[i…j] $ 로 나타내어지는 문자열 $ T $를 $ S $의 부분 문자열 (Substring) 이라고 합니다. 여기서 $ j = n $ 인 경우 $ T $를 $ S $의 접미사 (Suffix) 라고 부릅니다. 일반적으로 $ S $는 $ S $의 suffix이지만, 본 글에서 “$ S $의 가장 긴 팰린드롬 suffix” 를 칭할 때에는 $ S $ 본인이 팰린드롬인 경우는 제외하고 생각합니다.&lt;/p&gt;

&lt;h4 id=&quot;motivation&quot;&gt;Motivation&lt;/h4&gt;

&lt;p&gt;어떤 문자열 $ S $ 의 서로 다른 팰린드롬의 개수를 구하는 문제를 생각해봅시다.&lt;/p&gt;

&lt;p&gt;$ S $의 가능한 substring의 개수가 $ \Theta(n^2) $ 개이기 때문에 서로 다른 팰린드롬도 $ \Theta(n^2) $ 개 존재할 것이라고 추측하고, 실제로 서로 다른 팰린드롬이 $ \Theta(n^2) $ 개인 문자열을 잡아보려고 하면 생각보다 쉽게 되지 않음을 알 수 있습니다. “aaaaaaaa”와 같이 모든 substring이 팰린드롬인 문자열을 생각해도 서로 겹치는 것이 많아 최종적으로 서로 다른 팰린드롬의 수는 $ O(n) $이 됩니다. “abababa”, “abcbabcba” 와 같은 문자열을 잡아도 이는 마찬가지입니다.&lt;/p&gt;

&lt;p&gt;실제로, $ S $에 존재하는 서로 다른 팰린드롬은 많아야 $ n $개라는 사실을 증명할 수 있습니다. 증명은 다음과 같은 귀납적인 과정을 통해서 이루어집니다.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;길이가 1인 문자열의 경우, 전체 문자열이 유일하게 존재하는 팰린드롬이므로 위의 성질이 성립합니다.&lt;/li&gt;
  &lt;li&gt;길이가 $ n-1 $인 문자열 $ S[1…n-1] $ 의 서로 다른 팰린드롬의 수가 $ n-1 $개 이하라고 가정합시다. 이 문자열에 $ n $번째 문자를 추가했을 때, $ S[n] $ 을 끝으로 하면서, $ S[1…n-1] $에 등장하지 않은 팰린드롬이 많아야 1개 존재하면 귀납법으로 문제를 증명할 수 있습니다.&lt;/li&gt;
  &lt;li&gt;만약 그러한 팰린드롬이 두 개 존재한다고 하고, $ i&amp;lt;j $ 에 대해 $ S[i…n] $ 과 $ S[j…n] $ 이라고 합시다. 이때 $ S[j…n] $ 은 $ S[i…n] $ 의 부분 문자열이 됩니다. $ S[i…n] $ 이 팰린드롬이므로, $ S[j…n] $ 을 $ S[i…n] $ 상에서 “뒤집은” 문자열, 즉 $ S[i…i+n-j+1] $ 은 $ S[j…n] $ 과 정확히 같은 문자열이 됩니다. 즉 $ S[j…n] $ 과 같은 팰린드롬이 $ S[n] $ 이 문자열에 추가되기 전에 등장했으므로 모순이 발생합니다.&lt;/li&gt;
  &lt;li&gt;따라서 문자열에 문자 하나를 추가하는 과정에서 새로 생기는 팰린드롬은 최대 1개이므로 $ S[1…n] $ 에 속하는 서로 다른 팰린드롬은 많아야 $ n $개입니다.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;위 증명이 시사하는 바는, 문자열 $ S $에 새로운 문자 $ c $ 를 끝에 추가할 때 새로 추가되는 팰린드롬의 수는 최대 1개이며, 그것의 유일한 후보는 $ Sc$ 의 가장 긴 팰린드롬 suffix라는 것입니다. 그런 문자열은 $ S $의 팰린드롬 suffix $ T $에 대 $ cTc$ 꼴로 나타내어질 것이므로, 우리가 모든 $ T $에 대한 정보를 가지고 있다면 모든 $ T $를 보면서 $ Sc $ 의 팰린드롬 suffix에 대한 정보를 구할 수 있겠네요. 즉, 문자를 하나씩 추가하면서 팰린드롬 suffix에 대한 정보를 관리한다면 이 문제를 해결할 수 있습니다.&lt;/p&gt;

&lt;p&gt;eertree는 이 과정을 문자열의 길이에 비례하는 시간에 관리하는 자료구조입니다.&lt;/p&gt;

&lt;h4 id=&quot;edge와-suffix-link&quot;&gt;edge와 suffix link&lt;/h4&gt;

&lt;p&gt;길이가 $ n (\geq 2) $ 인 팰린드롬의 처음과 마지막 문자를 제거하면 길이가 $ n-2 $ 인 팰린드롬이 됩니다. 즉 한 문자열에 등장하는 여러 팰린드롬 사이에는 서로 포함하는 관계가 존재하고, 이런 관계는 트리 구조로 나타낼 수 있습니다. 구체적으로, 각 팰린드롬에 대응하는 정점을 가진 그래프에서 두 팰린드롬 $ S $와 $ T $가 $ S $ = $ cTc $ 인 관계를 가질 때 $ c $로 라벨링된 $ T \rightarrow  S $ 간선을 준다면, 이 그래프는 여러 독립적인 트리의 집합인 포레스트를 이루게 됩니다.   eertree는 이 구조를 기반으로 설계된 자료구조이며, 여기서 정의된 간선을 &lt;strong&gt;edge&lt;/strong&gt; 라고 부르기로 합니다.&lt;/p&gt;

&lt;p&gt;보통은 구현 및 설명의 편의성을 위해, 이 구조에 길이 0인 팰린드롬과 길이 -1인 팰린드롬을 나타내는 가상의 정점 $ O $와 $ I $를 추가적으로 정의합니다. $ O $에서 모든 길이 2인 팰린드롬으로 가는 edge를 주고, $ I $에서 모든 길이 1인 팰린드롬으로 가는 edge를 주고, $ I $에서 $ O $로 가는 edge를 주면 전체 그래프가 $ I $를 루트로 하는 하나의 트리로 묶이게 됩니다.&lt;/p&gt;

&lt;p&gt;Motivation에서 살펴보았듯이, 이런 구조를 유지하기 위해서는 문자열의 팰린드롬 suffix에 대한 정보를 관리하는 것이 중요합니다. 따라서 eertree에는 팰린드롬 사이의 포함 관계를 나타내는 edge 외에도 &lt;strong&gt;suffix link&lt;/strong&gt; 라고 부르는 방향 간선이 존재합니다. 모든 정점에 대해, 해당 정점을 나타내는 팰린드롬 $ S $ 의 suffix중 가장 긴 팰린드롬을 $ T $라고 할 때 $ S \rightarrow T $ 인 suffix link가 존재하고, $ suf(S) = T $ 라는 표기를 사용합니다. $ suf(O) = suf(I) = I $ 로 정의합니다.&lt;/p&gt;

&lt;p&gt;이런 식으로 suffix link를 정의했을 경우, 팰린드롬 $ S $의 모든 팰린드롬 suffix를 보는 과정은 $ S $에 해당하는 정점의 suffix link를 타고 $ I $를 만날 때까지 올라가면서 각 정점을 보는 과정으로 변환할 수 있습니다.&lt;/p&gt;

&lt;h4 id=&quot;construction&quot;&gt;Construction&lt;/h4&gt;

&lt;p&gt;어떤 문자열 $ S $의 eertree를 만들기 위해서는 빈 문자열의 eertree에서 시작해서 $ S $의 각 문자를 앞에서부터 하나씩 eertree에 추가하면 됩니다. Motivation에서 증명한 바에 따르면 문자 하나를 추가할 때마다 eertree의 정점은 최대 하나씩 증가하고, 새로 추가된 정점 $ P $에 대해서 $ P $로 들어오는 edge 하나와 $ P $에서 나가는 suffix link가 하나씩 추가됩니다.&lt;/p&gt;

&lt;p&gt;문자 $ c $를 $ S $에 추가하는 함수는 다음 단계를 거쳐 작동합니다:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;$ S $의 가장 긴 팰린드롬 suffix를 $ X $라고 합시다.
    &lt;ul&gt;
      &lt;li&gt;$ cX $가 $ S $의 suffix인 경우 [$ X $가 $ I $인 경우] , 만약 $ cXc $ [ $ c $ ] 에 해당하는 정점이 이미 존재한다면 함수 실행을 종료합니다. 존재하지 않는다면, $ cXc $ [ $ c $ ]에 해당하는 정점 $ Y $를 만들고 $ X $에서 $ Y $로 가는 edge를 추가합니다.&lt;/li&gt;
      &lt;li&gt;$ cX $가 $ S $의 suffix가 아닌 경우, $ X $를 $ suf(X) $ 로 바꾸고 $ cX $가 $ S $의 suffix가 될 때까지 이 과정을 반복합니다.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;$ X $를 $ suf(X) $로 바꿉니다.
    &lt;ul&gt;
      &lt;li&gt;$ cX $가 $ S $의 suffix인 경우 [$ X $가 $ I $인 경우], $ Y $에서 $ cXc $ [ $ c $ ] 로 가는 suffix link를 만들고, 함수 실행을 종료합니다.&lt;/li&gt;
      &lt;li&gt;$ cX $가 $ S $의 suffix가 아닌 경우, $ X $를 $ suf(X) $ 로 바꾸고 $ cX $가 $ S $의 suffix가 될 때까지 이 과정을 반복합니다.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;요약하면 새로운 문자를 추가할 때마다 전체 문자열의 가장 긴 팰린드롬 suffix를 찾고, 두 번째로 긴 팰린드롬 suffix를 찾아 suffix link를 만들어주는데, 이를 위해서 기존 문자열의 팰린드롬 suffix를 긴 것부터 차례대로 보면서 조건에 맞는지 확인하는 단순한 과정입니다.&lt;/p&gt;

&lt;h4 id=&quot;analyzation&quot;&gt;Analyzation&lt;/h4&gt;

&lt;p&gt;eertree에 문자를 하나 추가할 때마다 $ O(1) $ 만큼의 메모리를 사용하므로 eertree가 $ O(n) $의 공간복잡도를 차지한다는 것은 쉽게 알 수 있습니다. 그럼 시간 복잡도는 어떨까요?&lt;/p&gt;

&lt;p&gt;문자를 추가하는 함수가 호출될 때마다 $ k_1 $번 suffix link를 타고 올라가면서 검사를 하고, 새로운 정점과 edge를 만든 다음에 다시 $ k_2 $번 suffix link를 타고 올라가면서 검사를 하고, 새로운 suffix link를 만듭니다. 따라서 eertree를 만드는 전체 비용은 각 함수에 대한 $ k_1 $과 $ k_2 $값의 합에 따라 결정이 되겠네요.&lt;/p&gt;

&lt;p&gt;이를 분석하기 위해서 함수 호출 전후의 &lt;u&gt;최장 팰린드롬 suffix의 첫 문자의 위치&lt;/u&gt;를 관찰해봅시다. edge를 만들기 위해 suffix link를 타고 올라가면서 비교한 횟수가 한 번 많아질 때마다 최장 팰린드롬 suffix의 첫 문자는 적어도 오른쪽으로 한 칸 이동하는데, suffix link를 타고 올라갈수록 팰린드롬의 길이가 적어도 한 칸 줄어들기 때문입니다. 마지막에 edge를 만들어 주면서 첫 문자가 왼쪽으로 한 칸 이동하므로 $ k_1 $번의 비교를 수행한 경우 첫 문자는 적어도 오른쪽으로 $ k_1 - 1 $칸 만큼 이동하게 됩니다.&lt;/p&gt;

&lt;p&gt;그런데 문자열의 길이가 $ n $이므로 최장 팰린드롬 suffix의 첫 문자는 $ n $칸 초과 오른쪽으로 이동할 수 없습니다. 즉 $ \sum (k_1-1) \leq n$ 이라는 부등식을 얻습니다. $ n $번의 함수 호출에 대한 $ k_1 $의 합이 $ O(n) $ 이라는 결론을 얻었네요. $ k_2 $의 합이 $ O(n) $ 이라는 것도 비슷한 부등식을 세워서 얻을 수 있습니다. 따라서 suffix link를 타고 올라가는 과정의 시간 복잡도는 $ O(n) $이 됩니다.&lt;/p&gt;

&lt;p&gt;마지막으로 새로운 정점을 삽입할 때 해당 정점이 이미 존재하는 지 체크하는 과정의 분석을 마치면 최종 시간 복잡도를 결정지을 수 있습니다. $ X $에서 $ Y $로 가는 edge를 추가해야 한다고 할 때, $ Y $가 이미 eertree 상에 존재했다면 $ X $에서 $ Y $로 가는 edge가 이미 존재했었어야 합니다. 이는 각 정점마다 균형 이진 트리를 관리하여 가능한 문자의 개수 $ \sigma $에 대해 $ O(log\sigma) $ 시간에 판별이 가능합니다. 또는, $ \sigma $의 값이 크지 않은 경우 길이 $ \sigma $인 배열을 정점마다 관리하여 $ O(1) $ 시간에 판별하는 방법도 가능합니다.&lt;/p&gt;

&lt;p&gt;결론적으로, 최종 시간 복잡도는 $ O(nlog\sigma) $ 가 되며, 실제로 $\sigma$가 크지 않은 대부분의 상황 (대회 등) 에서는 공간 복잡도 $ O(n\sigma) $, 시간 복잡도 $ O(n) $이 요구되는 방법을 주로 사용합니다.&lt;/p&gt;

&lt;h4 id=&quot;practice-problems&quot;&gt;Practice problems&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.spoj.com/problems/NUMOFPAL/&quot;&gt;SPOJ : Number of Palindromes&lt;/a&gt; (eertree를 이용해서 $ O(n) $ 에 풀어봅시다!)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://acm.timus.ru/problem.aspx?space=1&amp;amp;num=1960&quot;&gt;Timus OJ : Palindromes and Super Abilities&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://oj.uz/problem/view/APIO14_palindrome&quot;&gt;APIO 2014 : Palindrome&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15893&quot;&gt;UCPC 2018 예선 : 가장 긴 공통부분 팰린드롬&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://codeforces.com/contest/17/problem/E&quot;&gt;Codeforces Beta Round #17 : Palisection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15893&quot;&gt;MIPT Training Camp 2014, Palindromic Contest : Pairs}&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/10508&quot;&gt;CERC 2014 : Virus synthesis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Mon, 23 Mar 2020 00:02:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/03/23/eertree/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/03/23/eertree/</guid>
        
        
      </item>
    
      <item>
        <title>AlgoShitPo 규칙 (2020.02.17)</title>
        <description>&lt;h3 id=&quot;주제-pool&quot;&gt;주제 Pool&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;글 안 쓰는 사람은 1개 이상의 주제 추가&lt;/li&gt;
  &lt;li&gt;외부에서도 주제 제안 받음&lt;/li&gt;
  &lt;li&gt;주제 추가는 깃허브 이슈로 관리&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;포스팅-주제-선정&quot;&gt;포스팅 주제 선정&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;주제 pool에서 원하는 주제 선택해서 작성&lt;/li&gt;
  &lt;li&gt;주제 겹치면 먼저 선점한 사람이 가져감&lt;/li&gt;
  &lt;li&gt;자신이 제안한 주제는 선택하지 않는 것이 &lt;strong&gt;강력히 권장됨&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;인원부족시&quot;&gt;인원 부족시&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;인원이 모자르면 명시적으로 휴식기 선언&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;인원-pool&quot;&gt;인원 Pool&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;인원 풀 고정&lt;/li&gt;
  &lt;li&gt;4명 작성, 나머지 검수&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;글-작성할-사람&quot;&gt;글 작성할 사람&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;4명 선발&lt;/li&gt;
  &lt;li&gt;하고 싶은 사람 + 가장 예전에 글 올린 사람
    &lt;ul&gt;
      &lt;li&gt;5명 이상 넘어가면 가장 최근에 작성한 사람이 빠짐&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;글의-제한&quot;&gt;글의 제한&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;블로그에 올라가는 글에는 오류가 없어야한다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;단순 복붙/번역 금지&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;필요한 사전지식이 다 깔려있는 경우에 이해를 할 수 있도록 글 쓰기&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;검증은 서로 읽어보면서 이해 가능한지 확인&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;결원-발생&quot;&gt;결원 발생&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;급하게 못하는 경우가 생기면 대타를 찾는다&lt;/li&gt;
  &lt;li&gt;공백이 예상되면 최소 1주 전에 공지&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;일정&quot;&gt;일정&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;주제 선정 시 글 작성할 시간 X주 정하기 (4명 공통)&lt;/li&gt;
  &lt;li&gt;작성 완료 후 견적 보고 검수할 시간 Y주 정하기&lt;/li&gt;
  &lt;li&gt;검수를 통과한 글들은 일요일에 한 번에 업로드&lt;/li&gt;
  &lt;li&gt;Y주 내에 검수를 통과하지 못한 글들은 보류하고 나중에 생각&lt;/li&gt;
  &lt;li&gt;가끔씩 한 주 잡아서 글 작성 없이 검수&amp;amp;보완만 하는 주 운영&lt;/li&gt;
  &lt;li&gt;각 사람은 3주기에 적어도 1개 이상의 글을 써야함
    &lt;ul&gt;
      &lt;li&gt;1주기 = XY 사이클&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 17 Feb 2020 03:21:00 +0900</pubDate>
        <link>http://algoshitpo.github.io/2020/02/17/rule/</link>
        <guid isPermaLink="true">http://algoshitpo.github.io/2020/02/17/rule/</guid>
        
        <category>blog</category>
        
        
      </item>
    
  </channel>
</rss>
