DZY Loves Sorting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 753    Accepted Submission(s): 249

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:

0lr: Sort a[l..r] in increasing order.

1lr: 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$.

 
Source
 
Recommend
wange2014
 

题意就是一个n的排列,执行Q次操作,每次操作是对某个区间从小到大排序或者从大到小排序。最后只查询一次,输出第k个位置当前的数。

因为只查询一次,而且这是n的全排列,所以直接二分答案,比mid小的赋值为0,大的赋值为1。区间查询判断的时候直接与0和1比较就可以了。

代码:

  1 //M-线段树+二分-HDU5649
2 #include<iostream>
3 #include<algorithm>
4 #include<cstring>
5 #include<iomanip>
6 #include<stdio.h>
7 #include<stdlib.h>
8 #include<math.h>
9 #include<cstdlib>
10 #include<set>
11 #include<map>
12 #include<ctime>
13 #include<stack>
14 #include<queue>
15 #include<vector>
16 #include<set>
17 using namespace std;
18 typedef long long ll;
19 const int inf=0x3f3f3f3f;
20 const double eps=1e-5;
21 const int maxn=1e5+10;
22 #define lson l,m,rt<<1
23 #define rson m+1,r,rt<<1|1
24
25 struct node{
26 int c,l,r;
27 }q[maxn];
28 int n,m,k;
29 int a[maxn],tree[maxn<<2],add[maxn<<2];
30
31 void pushup(int rt)
32 {
33 tree[rt]=tree[rt<<1]+tree[rt<<1|1];
34 }
35 void pushdown(int rt,int m)
36 {
37 if(add[rt]!=-1){
38 add[rt<<1]=add[rt<<1|1]=add[rt];
39 tree[rt<<1]=(m-(m>>1))*add[rt];
40 tree[rt<<1|1]=(m>>1)*add[rt];
41 add[rt]=-1;
42 }
43 }
44 void build(int x,int l,int r,int rt)
45 {
46 add[rt]=-1;
47 if(l==r){
48 tree[rt]=a[l]<=x?0:1;
49 return ;
50 }
51
52 int m=(l+r)>>1;
53 build(x,lson);
54 build(x,rson);
55 pushup(rt);
56 }
57 void update(int L,int R,int c,int l,int r,int rt)
58 {
59 if(L>R) return ;
60 if(L<=l&&r<=R){
61 add[rt]=c;
62 tree[rt]=(r-l+1)*c;
63 return ;
64 }
65
66 pushdown(rt,r-l+1);
67 int m=(l+r)>>1;
68 if(L<=m) update(L,R,c,lson);
69 if(R> m) update(L,R,c,rson);
70 pushup(rt);
71 }
72 int query(int L,int R,int l,int r,int rt)
73 {
74 if(L<=l&&r<=R){
75 return tree[rt];
76 }
77
78 pushdown(rt,r-l+1);
79 int m=(l+r)>>1;int ret=0;
80 if(L<=m) ret+=query(L,R,lson);
81 if(R> m) ret+=query(L,R,rson);
82 return ret;
83 }
84 bool check(int x)
85 {
86 build(x,1,n,1);
87 for(int i=1;i<=m;i++){
88 int c=q[i].c,l=q[i].l,r=q[i].r;
89 int cnt=query(l,r,1,n,1);
90 if(c){
91 update(l,l+cnt-1,1,1,n,1);
92 update(l+cnt,r,0,1,n,1);
93 }
94 else{
95 update(r-cnt+1,r,1,1,n,1);
96 update(l,r-cnt,0,1,n,1);
97 }
98 }
99 return query(k,k,1,n,1);
100 }
101 int main()
102 {
103 int t;
104 scanf("%d",&t);
105 while(t--){
106 scanf("%d%d",&n,&m);
107 for(int i=1;i<=n;i++)
108 scanf("%d",&a[i]);
109 for(int i=1;i<=m;i++)
110 scanf("%d%d%d",&q[i].c,&q[i].l,&q[i].r);
111 scanf("%d",&k);
112 int l=1,r=n;
113 while(l<=r){
114 int mid=(l+r)>>1;
115 if(!check(mid)) r=mid-1;
116 else l=mid+1;
117 }
118 printf("%d\n",r+1);
119 }
120 return 0;
121 }

HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数的更多相关文章

  1. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

  2. 数据结构(线段树):HDU 5649 DZY Loves Sorting

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

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

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

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

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

  5. HDU5649 DZY Loves Sorting 线段树

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

  6. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  8. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  9. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

随机推荐

  1. Httpclient httpdelete 参数

    Httpclient 中常用的请求有2个,HttpPost 和 HttpGet,今天在对某个网站进行分析的时候,突然发现用到了 HttpDelete,并且传参 是 Json. 1.一般 HttpPos ...

  2. HUAWEI TAG-AL00 找IMEI的过程

    前几天,遇到一台华为机型,IMEI获取有问题,然后就找了一下. 以下是解决过程,权当记录一下,尽管为知笔记已经有备份了 :) 0x01: 起因 测试小哥发现,一台机型IMEI获取不全,有问题,拨号页面 ...

  3. JMeter学习笔记(六) 文件下载接口测试

    本次测试的是文件下载接口,文件是PDF文档,步骤如下: 1.通过jmeter的录制功能,获取了文件下载接口的地址和参数,和其他的HTTP请求一样的配置 2.执行此接口后,察看结果树,点击下载接口的结果 ...

  4. 爬虫:Scrapy14 - Telnet 终端(Telnet Console)

    Scrapy 提供了内置的 Telnet 终端,以供检查,控制 Scrapy 运行的进程.Telnet 仅仅是一个运行在 Scrapy 进程中的普通 Python 终端.因此你可以在其中做任何是. T ...

  5. Android记事本08

    昨天: Anr问题异常的原因和解决方案. 今天: Activity数据传递之通用方式. Activity数据传递之静态变量. Activity数据传递之全局变量. 遇到的问题: 无.

  6. try...catch 语句

    一般情况下,我们很少用到 try...catch 语句,但是有时候为了测试代码中的错误,也有可能会用到.小白我也在工作中用到过.那么好的程序设计,什么时候会用到呢? try...catch 一般用来捕 ...

  7. Java中Object.equals和String.equals的区别详解

    前言 Java中的堆和常量池的区别是什么呢?Object.equals与String.equals的区别呢?下面让我们通过一个小示例让你明白它- 1.基础知识 Java的存储空间:寄存器.栈.堆.静态 ...

  8. spring 配置问题记录1-@ResponseBody和favorPathExtension

    在搭建springmvc+easyui的项目时,有一个地方参照网上说的方法一直没实现出来, 就是前台的datagrid的数据渲染不上去, 尝试了好多种方法,包括也找了目前手里的项目来进行比较,也没发现 ...

  9. ZCC loves cube(cube)

    题目描述 调戏完了狗,ZCC开始玩起了积木.ZCC的面前有一块n*n的棋盘,他要用这些1*1*1的积木在棋盘上搭出一个宏伟的建筑.积木有三种颜色,ZCC认为一个建筑要被称为宏伟的应该满足能从正面看到的 ...

  10. poj 2367 拓扑排序入门

    Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when ...