Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)
链接:http://codeforces.com/contest/484/problem/E
题意:
给你n个数的,每个数代表高度;
再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值;
思路:
因为查询的到的值一定是输入的其中一个,那么我们可以二分答案,判断二分得到的答案是否符合,那么在这里我们就只需要找到某个数x,查询区间[l,r]有多少个连续的数大于x,这个操作只需要将高度从小到达排序,倒着插入主席树中,值设为1,那么只要维护有多少个连续的1(线段树区间合并的方法),就代表有多少个大于x的连续的数,如果查询到的数大于w,那么就查更大的数并更新答案,如果小于w的话,就找更小的数。
之前写过倒着插入的主席树,二分答案的线段树,这次合在一起了。。。。
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 1e5+;
int sum[M*],lsum[M*],rsum[M*],ls[M*],rs[M*],root[M];
int idx;
struct node{
int x,id;
bool operator < (const node k) const{
if(x == k.x) return id < k.id;
return x < k.x;
}
}a[M]; void pushup(int l,int r,int rt){
lsum[rt] = lsum[ls[rt]];
rsum[rt] = rsum[rs[rt]];
int m = (l + r) >> ;
if(lsum[rt] == m-l+) lsum[rt] += lsum[rs[rt]];
if(rsum[rt] == r - m) rsum[rt] += rsum[ls[rt]];
sum[rt] = max(lsum[rs[rt]]+rsum[ls[rt]],max(sum[ls[rt]],sum[rs[rt]]));
} void update(int old,int &rt,int p,int c,int l,int r){
rt = ++idx; ls[rt] = ls[old]; rs[rt] = rs[old];
sum[rt] = sum[old];
if(l == r) {
lsum[rt] = sum[rt] = rsum[rt] = c;
return ;
}
int m = (l + r) >> ;
if(p <= m) update(ls[old],ls[rt],p,c,l,m);
else update(rs[old],rs[rt],p,c,m+,r);
pushup(l,r,rt);
} int query(int L,int R,int l,int r,int rt){
if(L > R) return ;
if(L == l&&R == r) return sum[rt];
int ret = ;
int m = (l + r) >> ;
if(R <= m) ret = query(L,R,l,m,ls[rt]);
else if(L > m) ret = query(L,R,m+,r,rs[rt]);
else{
ret = max(query(L,m,l,m,ls[rt]),query(m+,R,m+,r,rs[rt]));
int lx = min(rsum[ls[rt]],m-L+);
int rx = min(lsum[rs[rt]],R - m);
ret = max(ret,lx+rx);
}
return ret;
} int main()
{
int n,q,x,y,w;
scanf("%d",&n);
idx = ; root[n+] = ;
for(int i = ;i <= n;i ++) scanf("%d",&a[i].x),a[i].id = i;
sort(a+,a++n);
for(int i = n;i >= ;i --) update(root[i+],root[i],a[i].id,,,n);
scanf("%d",&q);
for(int i = ;i <= q;i ++){
scanf("%d%d%d",&x,&y,&w);
int l = ,r = n,ans = n;
while(l <= r){
int m = (l + r) >> ;
int num = query(x,y,,n,root[m]);
if(num >= w) l = m+,ans = m;
else r = m - ;
}
printf("%d\n",a[ans].x);
}
return ;
}
Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)的更多相关文章
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
E. Sign on Fence Bizon the Champion has recently finished painting his wood fence. The fence consi ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案
http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory limit p ...
- Codeforces Round #402 (Div. 2) D. String Game(二分答案水题)
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】
D. String Game Little Nastya has a hobby, she likes to remove some letters from word, to obtain anot ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
随机推荐
- 学习Android(入门基础和实用教程)
为了方便大家学习,准备录制Android基础篇的视频教程, https://item.taobao.com/item.htm?spm=0.7095261.0.0.17a61debAKIDPI& ...
- CF888G Xor-MST 生成树、分治、Trie树合并
传送门 第一次接触到Boruvka求最小生成树 它的原版本是:初始每一个点构成一个连通块,每一次找到每一个连通块到其他的连通块权值最短的边,然后合并这两个连通块.因为每一次连通块个数至少减半,所以复杂 ...
- BZOJ3252 攻略 贪心、长链剖分
传送门 给树竟直接给父子关系!!!真良心 首先一个贪心策略:每一次选择的链一定是所有链中权值最大的.这应该比较显然 那么我们接下来考虑如何维护这个贪心.我们可以使用长链剖分进行维护,对权值进行长链剖分 ...
- [Socket]Socket聊天小程序
一个简单是Socket聊天小程序,读写操作在不同的线程中.服务器端采用线程池. 1.Server import java.io.IOException; import java.net.ServerS ...
- (6)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- Canary机制的绕过
目标程序下载 提取码:8ypi 1.检查程序开启了哪些安全保护机制 Canary与NX开启了 Canary机制简介 64位的canary机制,会在函数头部添加: mov rax,QWORD PTR f ...
- jackjson-databind-2.9.3 笔记
问题 客户端请求: {"skip":0,"take":10,"corpName":"","cityCode&q ...
- JSON.NET VS BinaryFormatter 性能
近期有个性能调优工作.通过dottrace 分析,发现几处问题,其中json.net 在序列化和反序列化的时候也比较耗性能,所以考虑能不能通过其它序列化方式来提高性能. 1 object 序列化代码 ...
- Ansible之playbook的使用总结 - 运维笔记
之前详细介绍了Ansible的安装, 配置, 以及Ansible常用模块的使用. 下面对Ansible的playbook用法做一小结. 为什么引入playbook?一般运维人员完成一个任务, 比如安装 ...
- hadoop-mapreduce-(1)-统计单词数量
编写map程序 package com.cvicse.ump.hadoop.mapreduce.map; import java.io.IOException; import org.apache.h ...