题目链接

给一个序列, 两种操作, 一种是将[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. Oracle User Management FAQ翻译及学习笔记

    转载 最近了解到AME 的东西,很迫切,先转载一篇 [@more@] Oracle User Management FAQ翻译及学习笔记 写在前面 本文主要是翻译的英文版的Oracle User Ma ...

  2. javascript 正则表达式代码

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ...

  3. codevs1387

    题目描述                     Description 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始 图案按照 ...

  4. 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library

    1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...

  5. VC ++ 后台消息模拟

    —HWND TO=; —//TO=::FindWindow(_T("Chrome_RenderWidgetHostHWND"),NULL); —TO=::FindWindow(_T ...

  6. PHP和C#可共用的可逆加密算法

    PHP 加密用法 <?phpclass DES{    var $key;    var $iv; //偏移量        function DES($key = '11001100', $i ...

  7. Drawable类及XMLDrawable的使用

    一.性质 可直接使用.png..jpg..gif.9.png等图片作为资源,也可使用多种XML文件作为资源.(就是这些资源都能生成Drawable对象).并对XML文件作出相关处理 二.XMLDraw ...

  8. distance.c

    #include "stdio.h" #include "string.h" #include "math.h" #include &quo ...

  9. 05-0. 求序列前N项和(15)

    本题要求编写程序,计算序列 2/1+3/2+5/3+8/5+... 的前N项之和.注意该序列从第2项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子. 输入格式: 输入在一行中给出一个正整数 ...

  10. JSONObject和JSONArray

    点击下载json工具 点击下载支持jar包 1.从Object到String 要先用Object对象构造一个JSONObject或者JSONArray对象,然后调用它的toString()方法即可 ( ...