hdu 5649 DZY Loves Sorting 二分+线段树
题目链接
给一个序列, 两种操作, 一种是将[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 二分+线段树的更多相关文章
- 数据结构(线段树):HDU 5649 DZY Loves Sorting
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)
题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- HDU 4614 Vases and Flowers(二分+线段树区间查询修改)
描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- 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. ...
随机推荐
- [翻译]理解 ASP.NET 5
**原文:http://docs.asp.net/en/latest/conceptual-overview/understanding-aspnet5-apps.html** 英文捉急,花了挺多时间 ...
- 【IOS学习基础】OC类的相关
几天前突然在别人的类的.m文件中看到这么一句代码:@synthesize xxxx = _xxxx; 当时愣是没理解啥意思,过后才缓过神来发现原来是把一些类的基础知识忘记了,虽然不用过多去深究以前的一 ...
- C# process 使用方法
public static string ExecuteAaptCommand(string appName, string command) { string result = string.Emp ...
- java接口的理解
接口的最主要的作用是达到统一访问,就是在创建对象的时候用接口创建,[接口名] [对象名]=new [实现接口的类],这样你像用哪个类的对象就可以new哪个对象了,不需要改原来的代码,就和你的USB接口 ...
- mysql 查询重复的(不区分大小写)数据的SQL优化
在mysql中查询不区分大小写重复的数据,往往会用到子查询,并在子查询中使用upper函数来将条件转化为大写.如: select * from staticcatalogue WHERE UPPER( ...
- document.ready()的用法
1.Jquery是优秀的Javascrīpt框架,$是jquery库的申明,它很不稳定(我就常遇上),换一种稳定的写法jQuery.noConflict(); jQuery(document).rea ...
- Memcache缓存系统原理
在Web服务开发中,服务端缓存是服务实现中所常常采用的一种提高服务性能的方法.其通过记录某部分计算结果来尝试避免再次执行得到该结果所需要的复杂计算,从而提高了服务的运行效率. 除了能够提高服务的运行效 ...
- Oracle EBS-SQL (PO-7):检查异常-非批准的供应商设置供货比例.sql
select distinct msr.sourcing_rule_name 名称,msi.description 说明,msi ...
- 论山寨手机与Android联姻 【4】手机产业链
前文说到,生产手机以前,制造厂家需要预先得到软硬件的产品级设计方案,然后按照设计方案亦步亦趋地做,就可以制造出手机了.软硬件的产品级设计包括以下内容, 1. 主板设计,或者Gerber文件,或者PCB ...
- 【Lucene】挖掘相关搜索词
搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索.一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表.另一种方法 ...