[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的全排列,现在对这 ...
随机推荐
- Coloring Brackets (区间DP)
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...
- 【ITOO 4】WCF中,分布式事务处理
导读:事务可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源.通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠.在项目中,就有 ...
- jsp动态页面访问报错:HTTP Status 500 - java.lang.NullPointerException,org.apache.jasper.JasperException: java.lang.NullPointerException
今天把项目导入进去一个新的项目中去结果出现了: org.apache.jasper.JasperException: java.lang.NullPointerException 错误,jsp居然访问 ...
- SystemInformationRequestHandlers
SystemInformationRequestHandlers - Solr Wiki Search: Solr Wiki Login SystemInformationRequestHandler ...
- Python学习之-- IO 操作
阻塞IO / 非阻塞IO /IO多路复用 / 异步IO 说明:同步IO包含(阻塞IO / 非阻塞IO /IO多路复用),因为他们有个共同特性就是都需要内核态到用户态的一个等待. 基本概念解释,环境限定 ...
- 一个开发的Linux使用心得总结
Liunx介绍 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协 ...
- C. The Two Routes---cf602C(Dij)
http://codeforces.com/problemset/problem/602/C 题目大意: 有n个城市 有m条铁路 如果两个城市没有铁路 那么一定有公路 求从1 到 n 用铁路和公 ...
- JVM(零):走入JVM
JVM(零):走入JVM 本系列主要讲述JVM相关知识,作为本系列的第一篇文章,本文从Java为什么是一个跨平台的语音开始介绍,逐步引入Java虚拟机的概念,并给出一个JVM相关知识图谱,可以让读者从 ...
- DTRACE FOR MYSQL PHP
Using DTrace to troubleshoot Apache DTrace for MySQL , Inspecting MySQL with DTrace , MySQL DTrace P ...
- sqlite中常见的问题总结
一.sqlite中不能使用日期进行相减,执行结果无效 例如:SELECT count(*) as cnt FROM DayBanalces WHERE (date(ofDay)- date('2013 ...