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

Problem Description
  DZY has a sequence a[1..n]. It is a permutation of integers 1∼n.
  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].
Input
  First line contains t, denoting the number of testcases.
  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)
Output
  For each testcase, output one line - the value of a[k] after performing all m operations.
Sample Input
  1
  6 3
  1 6 2 5 3 4
  0 1 4
  1 3 6
  0 2 4
  3
Sample Output
  5

Hint

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的更多相关文章

  1. hdu 5649 DZY Loves Sorting 二分+线段树

    题目链接 给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列. 所有操作完了之后, 问你a[k]等于多少. 真心是涨见识了这题..好厉害. 因为最后只询问一个位置, 所以 ...

  2. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...

  3. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  4. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

  5. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  6. 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 ...

  7. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  8. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  9. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

随机推荐

  1. inverse 相关设置

    <set name="students" table = "student" inverse="true"> <!-- 指 ...

  2. LINQ高级编程 笔记

    相关资料:http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 1.什么是LINQ 语言集成查询是一系列标准查询操作符的集合, ...

  3. PE File.

    Figure 1 - PE File The CLR header stores information to indicate that the PE file is a .NET executab ...

  4. Oracle主键自动生成_表and存储过程

    -- Create table create table T_EB_SYS_DN_SEQUENCE_CONFIG ( sequence_id VARCHAR2(36) default sys_guid ...

  5. java_设计模式_模板方法模式_Template Method Pattern(2016-08-11)

    定义: 定义一个操作中算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤.这里的算法的结构,可以理解为你根据需求设计出来的业务流程.特定的步骤就是指那些 ...

  6. SGU 143.Long Live the Queen(女王万岁)

    时间限制:0.25s 空间限制:4M 题意: 有n(n<=16000)个小镇,每两个小镇有且仅有一条路径相连.每个小镇有一个收益x(-1000<=x<=1000). 现在要求,选择一 ...

  7. iOS 获取项目名称及版本号

    可用于版本让用户手动检测是否有版本更新可用. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  CFSho ...

  8. 用arm-linux-gcc v4.3.4交叉编译Qt4.8.3

    1.解压缩 #tar zxvf  qt-everywhere-opensource-src-4.8.3.tar.gz 2. configure #mkdir buildarm-static #cd b ...

  9. CSS动画:Transform中使用频繁的scale,rotate,translate动画

    动画中,skew只是transform中的一种形式的动画,我们还可以学习scale,rotate,translate.这是目前使用比较频繁的属性动作. 1.scale动画的定义:(单位数值) scal ...

  10. sql语句复制表

    1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...