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. atoi、stoi、strtoi区别

    首先atoi和strtol都是c里面的函数,他们都可以将字符串转为int,它们的参数都是const char*,因此在用string时,必须调c_str()方法将其转为char*的字符串.或者atof ...

  2. 函数对象的prototype总结

    通过看 http://www.cnblogs.com/mindsbook/archive/2009/09/19/javascriptYouMustKnowPrototype.html 该文章和对代码的 ...

  3. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  4. Java反射学习(java reflect)(二)

    ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...

  5. ubuntu系统安装的MySql数据库,远程不能访问的几种可能问题

    安装MySQL数据库后一般会遇到远程计算机不能连接的问题,具体问题需要我们排查.可能一:MySql数据库是否提供了外部访问的用户以及权限?可能二:MySql的配置文件是否只绑定了本机ip(ubuntu ...

  6. 静态方法块 static 以及对象属性&类属性的用法

    使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用.如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复 ...

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

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

  8. 基于GBT28181:SIP协议组件开发-----------第三篇SIP注册流程分析实现

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3941172.html,qq:1269122125. 上两章节简要的 ...

  9. ROW_NUMBER分页的注意事项

    之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据. like this: select * from ( select row_numb ...

  10. HDU 1996

    Problem Description n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上, ...