数据结构(线段树):HDU 5649 DZY Loves Sorting
DZY Loves Sorting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 294 Accepted Submission(s): 77
Now he wants to perform two types of operations:
0 l r: Sort a[l..r] in increasing order.
1 l r: Sort a[l..r] in decreasing order.
After doing all the operations, he will tell you a position k, and ask you the value of a[k].
t testcases follow. For each testcase:
First line contains n,m. m is the number of operations.
Second line contains n space-separated integers a[1],a[2],⋯,a[n], the initial sequence. We ensure that it is a permutation of 1∼n.
Then m lines follow. In each line there are three integers opt,l,r to indicate an operation.
Last line contains k.
(1≤t≤50,1≤n,m≤100000,1≤k≤n,1≤l≤r≤n,opt∈{0,1}. Sum of n in all testcases does not exceed 150000. Sum of m in all testcases does not exceed 150000)
1 6 2 5 3 4 -> [1 2 5 6] 3 4 -> 1 2 [6 5 4 3] -> 1 [2 5 6] 4 3. At last a[3]=5.
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int n,m,k;
int tr[maxn<<],mark[maxn<<];
int L[maxn],R[maxn],X[maxn],a[maxn]; void Make_same(int x,int l,int r,int d){
tr[x]=(r-l+)*d;
mark[x]=d;
} void Push_down(int x,int l,int r){
if(mark[x]!=-){
int mid=(l+r)>>;
Make_same(x<<,l,mid,mark[x]);
Make_same(x<<|,mid+,r,mark[x]);
mark[x]=-;
}
} void Build(int x,int l,int r,int g){
mark[x]=-;
if(l==r){
tr[x]=a[l]<=g?:;
return;
}
int mid=(l+r)>>;
Build(x<<,l,mid,g);
Build(x<<|,mid+,r,g);
tr[x]=tr[x<<]+tr[x<<|];
} int Query(int x,int l,int r,int a,int b){
Push_down(x,l,r);
if(l>=a&&r<=b)return tr[x];
int mid=(l+r)>>,ret=;
if(mid>=a)ret=Query(x<<,l,mid,a,b);
if(mid<b)ret+=Query(x<<|,mid+,r,a,b);
return ret;
} void Mark(int x,int l,int r,int a,int b,int d){
if(a>b)return;
if(l>=a&&r<=b){
Make_same(x,l,r,d);
return;
}
Push_down(x,l,r);
int mid=(l+r)>>;
if(mid>=a)Mark(x<<,l,mid,a,b,d);
if(mid<b)Mark(x<<|,mid+,r,a,b,d);
tr[x]=tr[x<<]+tr[x<<|];
} bool Check(){
for(int i=;i<=m;i++){
int sum=Query(,,n,L[i],R[i]);
if(X[i]){
Mark(,,n,L[i],L[i]+sum-,);
Mark(,,n,L[i]+sum,R[i],);
}
else{
sum=R[i]-L[i]+-sum;
Mark(,,n,L[i],L[i]+sum-,);
Mark(,,n,L[i]+sum,R[i],);
}
}
return Query(,,n,k,k);
} void Solve(){
int lo=,hi=n;
while(lo<=hi){
int mid=(lo+hi)>>;
Build(,,n,mid);
if(Check())lo=mid+;
else hi=mid-;
}
printf("%d\n",lo);
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=m;i++)scanf("%d%d%d",&X[i],&L[i],&R[i]);
scanf("%d",&k);
Solve();
}
return ;
}
数据结构(线段树):HDU 5649 DZY Loves Sorting的更多相关文章
- hdu 5649 DZY Loves Sorting 二分+线段树
题目链接 给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列. 所有操作完了之后, 问你a[k]等于多少. 真心是涨见识了这题..好厉害. 因为最后只询问一个位置, 所以 ...
- HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)
题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
随机推荐
- Bash关闭输出(关闭正确、错误输出)
利用&>重定向,不输出任何内容: echo hello &> /dev/null 关闭正确输出: echo hello 1> /dev/null 关闭错误输出: ec ...
- centos6.5 64位 openvpn安装配置
1 查看系统版本 2 cat /etc/redhat-release 3 CentOS release 6.5 (Final) 4 5 查看内核和cpu架构 6 uname -rm 7 2.6.32- ...
- Ant学习笔记(1) 基础知识
Ant Apache Ant 是一个基于 Java的构建工具. 下载Ant google.baidu.Windows用户下载zip格式.解压即可. Windows安装Ant Ant本质上是一个Java ...
- 查询可用的Nuget服务地址
解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...
- VS2015使用OSChina的git功能
好长时间没有写博了,把今天的新的记录一下. 最近开始使用vs2015,vs2015支持git平台和TF功能,因为....,我选择了OSChina的git.一开始学习的此篇文章http://my.osc ...
- Singleton设计模式的一种见解
单实例Singleton设计模式可能是被讨论和使用的最广泛的一个设计模式了,这可能也是面试中问得最多的一个设计模式了.这个设计模式主要目的是想在整个系统中只能出现一个类的实例.这样做当然是有必然的,比 ...
- C蛮的全栈之路-序章 技术栈选择与全栈工程师
目录 C蛮的全栈之路-序章 技术栈选择与全栈工程师C蛮的全栈之路-node篇(一) 环境布置C蛮的全栈之路-node篇(二) 实战一:自动发博客 博主背景 985院校毕业,至今十年C++开发工作经验, ...
- Vijos P1521 跳舞 贪心
本来想找一道网络流的题来着,结果这道题越看越不对劲,总觉得这题存在不用网络流的解法 看了题解区以后坚定了自己的猜想 #include <cstdio> #include <cstri ...
- Cocos Studio1.5.0.1开发学习笔记(一)
听说Cocos Studio很久了,主要是因为骨骼动画.目前看来Cocos2d-x播放动画的方式只有2种: 第一种:是播放序列帧动画,即将动画的每一帧都加载进缓存里,需要播放时再使用Animation ...
- java中的异常结构
1.基类为Throwable. 2.Error和Exception分别继承Throwable. 3.Error类异常描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对 ...