码迷,mamicode.com
首页 > 其他好文 > 详细

[SGU 155] Cartesian Tree

时间:2019-08-18 12:00:41      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:ace   max   sort   names   push   模板   传送门   line   main   

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

原题传送门

#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

标签:ace   max   sort   names   push   模板   传送门   line   main   

原文地址:https://www.cnblogs.com/wyctstf/p/11371570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!