[TJOI2016&HEOI2016] 排序
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4552
[算法]
首先 , 二分答案x , 将比x小的数看作1,比x大的数看作0
然后用线段树检验即可
时间复杂度 : O(MlogN^2)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ; int n , m , q;
int a[MAXN],b[MAXN]; struct Que
{
int op , l , r;
} que[MAXN];
struct Node
{
int l , r;
int tag , cnt;
} Tree[MAXN << ]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void update(int index)
{
Tree[index].cnt = Tree[index << ].cnt + Tree[index << | ].cnt;
}
inline void build(int index,int l,int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].tag = -;
if (l == r)
{
Tree[index].cnt = b[l];
return;
}
int mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
update(index);
}
inline void pushdown(int index)
{
int l = Tree[index].l , r = Tree[index].r ,
mid = (l + r) >> ;
if (Tree[index].tag != -)
{
if (Tree[index].tag)
{
Tree[index << ].cnt = mid - l + ;
Tree[index << | ].cnt = r - mid;
Tree[index << ].tag = Tree[index << | ].tag = ;
} else
{
Tree[index << ].cnt = Tree[index << | ].cnt = ;
Tree[index << ].tag = Tree[index << | ].tag = ;
}
Tree[index].tag = -;
}
}
inline void modify(int index,int l,int r,int value)
{
if (l > r) return;
if (Tree[index].l == l && Tree[index].r == r)
{
if (value == ) Tree[index].cnt = Tree[index].r - Tree[index].l + ;
else Tree[index].cnt = ;
Tree[index].tag = value;
return;
}
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << ,l,r,value);
else if (mid + <= l) modify(index << | ,l,r,value);
else
{
modify(index << ,l,mid,value);
modify(index << | ,mid + ,r,value);
}
update(index);
}
inline int query(int index,int l,int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].cnt;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index << ,l,r);
else if (mid + <= l) return query(index << | ,l,r);
else return query(index << ,l,mid) + query(index << | ,mid + ,r);
}
inline bool check(int x)
{
for (int i = ; i <= n; i++) b[i] = (a[i] <= x);
build(,,n);
for (int i = ; i <= m; i++)
{
if (que[i].op == )
{
int cnt = query(,que[i].l,que[i].r);
modify(,que[i].l,que[i].l + cnt - ,);
modify(,que[i].l + cnt,que[i].r,);
} else
{
int cnt = query(,que[i].l,que[i].r);
modify(,que[i].l,que[i].r - cnt,);
modify(,que[i].r - cnt + ,que[i].r,);
}
}
return query(,q,q) == ;
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++) read(a[i]);
for (int i = ; i <= m; i++)
{
read(que[i].op);
read(que[i].l);
read(que[i].r);
}
read(q);
int l = , r = n , ans;
while (l <= r)
{
int mid = (l + r) >> ;
if (check(mid))
{
ans = mid;
r = mid - ;
} else l = mid + ;
}
printf("%d\n",ans); return ; }
[TJOI2016&HEOI2016] 排序的更多相关文章
- BZOJ 4552: [Tjoi2016&Heoi2016]排序
4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 579 Solved: 322[Sub ...
- bzoj千题计划128:bzoj4552: [Tjoi2016&Heoi2016]排序
http://www.lydsy.com/JudgeOnline/problem.php?id=4552 二分答案 把>=mid 的数看做1,<mid 的数看做0 这样升序.降序排列相当于 ...
- [Tjoi2016&Heoi2016]排序[01序列]
4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 994 Solved: 546[Sub ...
- 4552: [Tjoi2016&Heoi2016]排序
4552: [Tjoi2016&Heoi2016]排序 链接 分析: 因为只询问一次,所以考虑二分这个数.显然是没有单调性的,但是我们可以二分所有大于等于mid的数中,是否有满足条件的x(而不 ...
- 【BZOJ4552】[Tjoi2016&Heoi2016]排序 二分+线段树
[BZOJ4552][Tjoi2016&Heoi2016]排序 Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ...
- [bzoj4552][Tjoi2016][Heoi2016]排序
Description 给出一个$1$到$n$的全排列,现在对这个全排列序列进行$m$次局部排序,排序分为$2$种: $1.(0,l,r)$表示将区间$[l,r]$的数字升序排序; $2.(1,l,r ...
- BZOJ4552: [Tjoi2016&Heoi2016]排序
Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...
- [bzoj4552][Tjoi2016&Heoi2016]排序-二分+线段树
Brief Description DZY有一个数列a[1..n],它是1∼n这n个正整数的一个排列. 现在他想支持两种操作: 0, l, r: 将a[l..r]原地升序排序. 1, l, r: 将a ...
- bzoj 4552 [Tjoi2016&Heoi2016]排序 (二分答案 线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4552 题意: 给你一个1-n的全排列,m次操作,操作由两种:1.将[l,r]升序排序,2 ...
- BZOJ 4552 [Tjoi2016&Heoi2016]排序 | 二分答案 线段树
题目链接 题面 题目描述 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...
随机推荐
- JavaEE JDBC 了解数据库连接池
了解数据库连接池 @author ixenos 数据库连接是有限的资源,如果用户需要离开应用一段时间,那么他占用的连接就不应该保持开放状态: 另一方面,每次查询都获取连接并在随后关闭它的代价也很高. ...
- NYOJ-517-最小公倍数,大数啊~~~
最小公倍数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致.但也并非纯粹的偶然:60是个优秀的数字,它的 ...
- [luoguP1773] 符文之语_NOI导刊2010提高(02)(DP)
传送门 f[i][j]表示前i个数余数为j的最优解 sum[i][j]表示字符串i~j所构成的数 #include <cstdio> #include <cstring> #d ...
- BZOJ:[JSOI2009]游戏Game【二分图匹配乱搞】
题目大意:n*m的棋盘,其中有些区域是禁区,两个人在棋盘上进行博弈,后手选择棋子的初始位置,然后先后手轮流将棋子往上下左右移动,走过的区域不能再走,问能否有一个位置使得后手必胜 Input 输入数据首 ...
- Android Application基本组成部分
Android Application基本组成部分 四个核心的组件 Activity活动,主要用于前台和用户交互,即UI,Activity只是加载一个View而并非一个UI对象 Service服务,主 ...
- msp430入门编程21
msp430中C语言的扩展--#pragma编译命令
- ACM-ICPC 2018 沈阳赛区网络预赛 G 容斥原理
https://nanti.jisuanke.com/t/31448 解析 易得an=n*n+n O(1)得到前n项和 再删除与m不互素的数 我们用欧拉函数求出m的质因数 枚举其集合的子集 进行 ...
- 免费SSL申请
https://letsencrypt.org/ https://letsencrypt.org/docs/client-options/ ACMESharp (.NET, PowerShell) w ...
- Milking Time---poj3616
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- 动态规划: HDU 1789Doing Homework again
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...