当前位置: 首页 > news >正文

洛谷P5854 【模板】笛卡尔树 题解 笛卡尔树模板题

题目链接:https://www.luogu.com.cn/problem/P5854

笛卡尔树 模板题。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 5;int n;struct Node {int s[2], p, pri;
} tr[maxn];void f_s(int p, int u, int k) {tr[p].s[k] = u;tr[u].p = p;
}void build_tree() {stack<int> stk;stk.push(0);for (int i = 1; i <= n; i++) {int pos = stk.top();while (!stk.empty() && tr[stk.top()].pri > tr[i].pri) {pos = tr[stk.top()].p;stk.pop();}f_s(i, tr[pos].s[1], 0);f_s(pos, i, 1);stk.push(i);}
}int main() {scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &tr[i].pri);build_tree();long long res1 = 0, res2 = 0;for (int i = 1; i <= n; i++) {res1 ^= 1ll * i * (tr[i].s[0] + 1);res2 ^= 1ll * i * (tr[i].s[1] + 1);}printf("%lld %lld\n", res1, res2);return 0;
}
http://www.rkmt.cn/news/1311.html

相关文章:

  • 都江堰操作系统
  • [OLAP/Doris] Doris 之表设计
  • 20250909 之所思 - 人生如梦
  • 认识人工智能-基础认知
  • 苹果im虚拟机协议群发系统,苹果imessage推信软件,苹果iMessage自动群发协议–持续更新中...
  • 课前问题思考1
  • huggingface
  • 从0到1实现Transformer模型-CS336作业1
  • Morpheus 审计报告分享:AAVE 项目 Pool 合约地址更新导致的组合性风险
  • U3D动作游戏开发读书笔记--2.1一些通用的预备知识
  • Word中VBA提取人名所在的页码
  • ARC
  • Ubuntu 安装 Git
  • systemctl命令
  • 知识蒸馏
  • 第一次学dij qwq(p4779
  • 2025—2026 赛季记录
  • Ubuntu 安装搜狗输入法
  • 今日随笔
  • 摆放类状压DP基础题
  • DVectorT虐哭ListT
  • manim如何按绝对时间管理动画
  • Snapshot-based State Replication 基于快照的状态复制网络框架,快照同步
  • Transformer通俗讲解
  • Ubuntu 安装截图软件 flameshot
  • Kali连接postgreSQL失败(已解决)
  • 英语_阅读_water protection team_待读
  • Codeforces Round 1048 (Div 2)
  • 9.9日总结
  • GitHub Copilot代码审查大升级!路径级指令+组织级规范,开发者效率再提升!