[SGU 155] Cartesian Tree

Posted wyctstf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SGU 155] Cartesian Tree相关的知识,希望对你有一定的参考价值。

笛卡尔树模板题,但是蒟蒻调这道题比调它的变式题目调的更久,我果然是菜过头了

原题传送门

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int maxn = 50000 + 10;

int n, top;
int fa[maxn], l[maxn], r[maxn];
int conv[maxn];

struct Node

    int id, va, vb;
    bool operator<(const Node &a) const
    
        return va < a.va;
    
 a[maxn];

inline int read()

    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
        f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch))
        x = x * 10 + (ch - '0'), ch = getchar();
    return x * f;


int main()

    n = read();
    for (int i = 1; i <= n; i++)
        a[i].va = read(), a[i].vb = read(), a[i].id = i;
    sort(a + 1, a + n + 1);

    conv[a[1].id] = 1;

    stack<int> S;
    S.push(1);
    for (int i = 2; i <= n; ++i)
    
        conv[a[i].id] = i;
        int fp = 0, sp = 0;
        while (!S.empty())
        
            int tmp = S.top();
            if (a[tmp].vb < a[i].vb)
            
                fp = tmp;
                break;
            
            else
            
                sp = tmp;
                S.pop();
            
        
        fa[i] = fp;
        r[fp] = i;
        l[i] = sp;
        fa[sp] = i;
        S.push(i);
    

    printf("YES\n");
    for (int i = 1; i <= n; ++i)
    
        int p = conv[i];
        printf("%d %d %d\n", a[fa[p]].id, a[l[p]].id, a[r[p]].id);
    
    return 0;

以上是关于[SGU 155] Cartesian Tree的主要内容,如果未能解决你的问题,请参考以下文章

将 RDD.cartesian 与 Spark Streaming 结合使用是不是存在错误?

POJ-2201-Cartesian Tree(笛卡尔树)

POJ 2201 Cartesian Tree

CF1290E Cartesian Tree

Detected cartesian product for LEFT OUTER join

今日SGU 5.14