HDU - 5338 ZZX and Permutations 线段树 + set
感觉分情况讨论清楚就好啦, 不是很难写。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}int n, a[N], pos[N], from[N], ans[N]; bool ban[N]; set<int> segEnd;#define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1struct segmentTree {PII mx[N << 2];void build(int *a, int l, int r, int rt) {if(l == r) {mx[rt].fi = a[l];mx[rt].se = l;return;}int mid = l + r >> 1;build(a, lson); build(a, rson);mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);}void update(int p, int l, int r, int rt) {if(l == r) {mx[rt].fi = 0;return;}int mid = l + r >> 1;if(p <= mid) update(p, lson);else update(p, rson);mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);}PII query(int L, int R, int l, int r, int rt) {if(R < l || r < L || R < L) return mk(0, 0);if(L <= l && r <= R) return mx[rt];int mid = l + r >> 1;return max(query(L, R, lson), query(L, R, rson));} } Tree;int main() {int T; scanf("%d", &T);while(T--) {segEnd.clear();scanf("%d", &n);segEnd.insert(n + 1);segEnd.insert(0);for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);pos[a[i]] = i;ban[i] = false;from[i] = i;}Tree.build(a, 1, n, 1);for(int i = 1; i <= n; i++) {int p = pos[i];if(ban[p]) continue;int R = from[p];PII tmp1 = mk(0, 0), tmp2 = mk(0, 0);auto it = segEnd.lower_bound(p);if(*it != p + 1) tmp1 = mk(a[p + 1], p + 1);--it;int L = *it + 1;tmp2 = Tree.query(L, R, 1, n, 1);if(tmp1 > tmp2) {ans[i] = tmp1.fi;from[tmp1.se] = from[p];Tree.update(tmp1.se, 1, n, 1);} else {ans[i] = tmp2.fi;L = tmp2.se, R = p;for(int j = L; j <= R; j++) {Tree.update(j, 1, n, 1);ban[j] = true;if(j < R) ans[a[j]] = a[j + 1];}segEnd.insert(p);segEnd.insert(L);}}for(int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]);}return 0; }/* */
转载于:https://www.cnblogs.com/CJLHY/p/10927379.html
HDU - 5338 ZZX and Permutations 线段树 + set相关推荐
- Hdu 5338 ZZX and Permutations(线段树+贪心)
解析: 首先用set维护被Cirlcle Notation切开的不连续的各个段. 然后从i开始,(p[i]为i的位置) 找到其在set中的左右端点,左边的最大值为value_l,右边p[i]+1的值为 ...
- HDU 1166 敌兵布阵(线段树:点更新,区间求和)
HDU 1166 敌兵布阵(线段树:点更新,区间求和) http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意: 给你n个整数,然后给你多条命令,每条命令如 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 2019CCPC网络赛 1002 HDU 6703(权值线段树)
2019CCPC网络赛 1002 HDU 6703(权值线段树) 思路:用权值线段树存题目给的数据后,2操作就是求权值线段树中大于等于k的部分中,靠近左端点的第一个大于r的值(这个求出来的只是原序列中 ...
- HDU 6070 Dirt Ratio(线段树、二分)
http://acm.hdu.edu.cn/showproblem.php?pid=6070 题解 首先不难看出错误率是单调的,那么我们可以直接二分答案x,某个区间的错误率=区间数的种类cnt/区间长 ...
- hdu 5692 Snacks(dfs序+线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...
- HDU 6089 Rikka with Terrorist (线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...
- hdu 4391 Paint The Wall 线段树 +优化 2012 Multi-University Training Contest 10 )
http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意: 刷墙, 以开始 有 n个节点,每个节点有一种颜色 ,m 次询问 m次 输入 a,l,r,z 如果 ...
- HDU 3397 Sequence operation(线段树)
HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...
最新文章
- 自己写的哈希表以及解决哈希冲突
- Java IO 字节流与字符流 (三)
- linux网络工程师证书,网络工程师应该考什么证
- Java一行一行的读文件和简单的写文件
- linux设置IP,网关,DNS和MAC地址
- 深度学习-Tensorflow2.2-tf.data输入模块{2}-tf.data输入实例-10
- SAP UI5 ComponentBase createMetaData signature - why is MD hard coded
- 带有Gluon Ignite和Dagger的JavaFX中的依赖注入
- 没有返回值的方法mock怎么写_【方法】小学生怎么写读书笔记?
- 信息学奥赛一本通 1005:地球人口承载力估计| OpenJudge NOI 小学奥数 7653
- 信息学奥赛一本通(1088:分离整数的各个数)
- JHChart 1.1.0 iOS图表工具库中文ReadMe
- 编写在浏览器中不弹出警告的ActiveX控件
- [3-6] xp系统主题下载最新xp系统软件更新 [yeskm整理]
- HTML+JS实现页面跳转
- web前端开发课设 html5前端网页设计 中国传统文化介绍
- Flutter随记四:null safe之 ??= 和??和 ?使用区别
- cannot connect to 192.168.137.137:5555: 由于目标计算机积极拒绝,无法连接。
- pythonk库函数minidom,wx
- 【图像处理】小波编码图像中伪影和纹理的检测附Matlab代码和报告
热门文章
- git 怎么给自己的分支命名_Git分支管理及命名规范
- SQL Server 2017安装
- js 控制表单input,textarea select为只读模式
- List 列表迭代器
- java项目符号转译问题_如何更改除项目符号列表或非项目符号列表中第一行以外的所有行的缩进?...
- Python实现图片转化为字符图(附完整代码)
- 字符串笔记(C Primer Plus)
- Error-tf.function-decorated function tried to create variables on non-first call
- 由近及远计算机网络故障判断,利用网络视频软件实现远程故障诊断
- dubbo3消费端bean字段名称引发的问题-dubbo2.7升级到dubbo3系列