ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 2201 Cartesian Tree

2019-08-13 12:02:19  阅读:262  来源: 互联网

标签:Node 2201 Cartesian cur int st rf POJ ans


笛卡尔树模版

按第一关键字排序,然后按第二关键字构建笛卡尔树即可。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define __fastIn ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
using namespace std;
typedef long long LL;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        ret = (ret << 3) + (ret << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -ret : ret;
}
template <typename A>
inline A __lcm(A a, A b){ return a / __gcd(a, b) * b; }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 60000;
int n, x, y;
vector< pair<pair<int, int>, int> > v;
struct Node{
    int i, val;
    Node *fa, *lf, *rf;
    Node(){}
    Node(int i, int val, Node *fa, Node *lf, Node *rf): i(i), val(val), fa(fa), lf(fa), rf(rf){}
}*null, *root;

bool cmp(Node *a, Node *b){ return a->i < b->i; }

Node *buildTree(){

    stack<Node *> st;
    Node *last = null;
    for(int i = 0; i < v.size(); i ++){
        Node *cur = new Node(v[i].second, v[i].first.second, null, null, null);
        while(!st.empty()){
            if(st.top()->val < cur->val){
                Node *up = st.top();
                if(up->rf != null) cur->lf = up->rf, up->rf->fa = cur;
                up->rf = cur, cur->fa = up;
                break;
            }
            last = st.top(), st.pop();
        }
        if(st.empty() && last) cur->lf = last, last->fa = cur;
        st.push(cur);
    }
    Node *root = null;
    while(!st.empty()) root = st.top(), st.pop();
    return root;
}
vector<Node *> ans;

void dfs(Node *cur){
    if(cur == null) return;
    ans.pb(cur);
    dfs(cur->lf), dfs(cur->rf);
}

int main(){

    n = read();
    for(int i = 0; i < n; i ++){
        x = read(), y = read();
        v.pb(make_pair(make_pair(x, y), i + 1));
    }
    sort(v.begin(), v.end());
    null = new Node(0, 0, NULL, NULL, NULL);
    root = buildTree();
    dfs(root);
    sort(ans.begin(), ans.end(), cmp);
    puts("YES");
    for(int i = 0; i < ans.size(); i ++){
        printf("%d %d %d\n", ans[i]->fa->i, ans[i]->lf->i, ans[i]->rf->i);
    }
    return 0;
}

标签:Node,2201,Cartesian,cur,int,st,rf,POJ,ans
来源: https://www.cnblogs.com/onionQAQ/p/11345058.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有