题目链接

给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列。

所有操作完了之后, 问你a[k]等于多少。





真心是涨见识了这题..好厉害。

因为最后只询问一个位置, 所以我们二分这个位置的值。 将所有大于等于它的值赋为1, 小于的赋为0. 然后现在整个序列只有01, 更改什么的线段树就很好搞。

如果a[k]最后为1, 那么我们增加l, 否则减少r。



那么为什么可以这样呢。

因为二分mid的时候, 将等于mid的也赋为了1, 所以如果a[k]是0的话,代表mid比答案大, 所以减少r。

那么a[k]为1的时候有两种, 一种是mid刚好是答案, 另一种是mid比答案小, 所以我们增大l逼近答案。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int sum[maxn<<2], a[maxn], cover[maxn<<2];
pair<int, pll> q[maxn];
void push_down(int rt, int m) {
if(~cover[rt]) {
sum[rt<<1] = cover[rt]*(m-(m>>1));
sum[rt<<1|1] = cover[rt]*(m>>1);
cover[rt<<1] = cover[rt<<1|1] = cover[rt];
cover[rt] = -1;
}
}
void update(int L, int R, int l, int r, int rt, int val) {
if(L>R)
return ;
if(L<=l&&R>=r) {
cover[rt] = val;
sum[rt] = val*(r-l+1);
return ;
}
push_down(rt, r-l+1);
int m = l+r>>1;
if(L<=m)
update(L, R, lson, val);
if(R>m)
update(L, R, rson, val);
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return sum[rt];
}
push_down(rt, r-l+1);
int m = l+r>>1, ret = 0;
if(L<=m)
ret += query(L, R, lson);
if(R>m)
ret += query(L, R, rson);
return ret;
}
void check(int x, int n, int m) {
mem(sum); mem1(cover);
for(int i = 1; i <= n; i++)
if(a[i]>=x)
update(i, i, 1, n, 1, 1);
for(int i = 0; i < m; i++) {
int tmp = query(q[i].fi, q[i].se.fi, 1, n, 1);
if(q[i].se.se) {
update(q[i].fi, q[i].fi+tmp-1, 1, n, 1, 1);
update(q[i].fi+tmp, q[i].se.fi, 1, n, 1, 0);
} else {
update(q[i].se.fi-tmp+1, q[i].se.fi, 1, n, 1, 1);
update(q[i].fi, q[i].se.fi-tmp, 1, n, 1, 0);
} } }
void solve() {
int n, m, k;
cin>>n>>m;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &q[i].se.se, &q[i].fi, &q[i].se.fi);
}
cin>>k;
int l = 1, r = n, ans;
while(l<=r) {
int mid = l+r>>1;
check(mid, n, m);
if(query(k, k, 1, n, 1)) {
ans = mid;
l = mid+1;
} else {
r = mid-1;
}
}
cout<<ans<<endl;
}
int main()
{
int t;
cin>>t;
while(t--) {
solve();
}
return 0;
}

hdu 5649 DZY Loves Sorting 二分+线段树的更多相关文章

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

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

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

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

  3. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

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

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

  5. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  6. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

  7. HDU 4614 Vases and Flowers(二分+线段树区间查询修改)

    描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...

  8. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  9. codeforces 446C DZY Loves Fibonacci Numbers 线段树

    假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...

随机推荐

  1. ES6笔记① var 和 let的区别

    let 和 var的区别    答:不同点在于作用域 1.(全局下)首先  let关键字声明的变量是这样写会导致错误. let声明的变量类似于”本地变量“,函数内如何不重新声明,还是会被改变 var ...

  2. PendingIntent Bundle null解决方案

    在安卓开发中,用通知栏,如果点击通知栏条目,跳转Intent需要传值的时候会出现问题,传入值失败. Intent intent; PendingIntent sender=PendingIntent. ...

  3. iOS中不透明度的查看

    模拟器工具条 Debug-->Color Blended Layers 即中文显示下 调试 -->颜色混合层 绿色代表不透明部分,红色代表透明部分,红色越多对性能影响越大

  4. UML-状态图,顺序图,活动图

    一.编写用例文档      1.用例的内容:   用例编号   用例名  执行者  前置条件  后置条件  基本路径  扩展路径  字段列表  业务规则                         ...

  5. 【自学php】第二天 - php快速入门

    打算看<php和mysql web开发>来学习php,所以也算是这本书的学习笔记吧,也按照书里的例子来练习,但是也有些取舍.第一章是一个订单表单的例子,php用于处理提交的表单. 1.先创 ...

  6. 关于Char* ,CString ,WCHAR*之间的转换问题

    GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用往往是CString,用IO流读文件数据又得到char *.得益 ...

  7. Painting Storages(ZOJ)

    There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to pai ...

  8. git push报错

    git: No refs in common and none specified; doing no (2012-10-28 11:43:10) 转载▼ 标签: 杂谈 分类: 项目管理 用gitol ...

  9. .NET,你忘记了么?(八)—— 从dynamic到特性误用 [转]

    1. 摘要 每个程序员都想写出漂亮的代码,但是什么是漂亮,这个我想每个人都有着自己的看法.那么我就说几种典型的想法: A. 写出别人看不懂的代码,让别人觉得很高深. B. 写出简短的代码 C. 用最新 ...

  10. MAC xampp 启动失败

    原文地址: http://meiyitianabc.blog.163.com/blog/static/1050221272013116232752/ 问题:80port被暂用,导致server无法启动 ...