链接: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 (二分答案 主席树 区间合并)的更多相关文章

  1. 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 ...

  2. 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的数 维护 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  8. 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 ...

  9. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

随机推荐

  1. 【小程序】模拟数据支持(mockjs配置模拟服务器接口数据)

    utils目录 ①下载mockjs(地址)放置utils目录中 ②新建api.js :配置模拟数据以及后台接口,通过DEBUG=ture;  //切换数据接口 配置如下: let API_HOST = ...

  2. Ionic 部分手机升级不成功的问题

    Android端的手机App发布之后的一段时间,用户反馈App无法升级的情况. 原因分析: 对代码进行错误在线,提示是FileNOTFindException错误,确定是下载的时候保存的目的路径 不存 ...

  3. MVC ActionResult派生类关系图

    态度决定一切,我要改变的不仅仅是技术,还有对待事情的态度! 先上个图: 由上图可知,ActionResult为根节点,其下有很多子节点!下面简单介绍下: MVC中ActionResult是Action ...

  4. MVC 使用cshtml的一些基础知识-和相关整理

    首先在认识cshtml之前,先要了解一下Razor视图引擎 如果对此有疑问的话可以借鉴 博客园博文:http://kb.cnblogs.com/page/96883/ 或 博客博文:http://ww ...

  5. Zabbix监控系统部署:前端初始化

    1. 概述 在上一篇博客<Zabbix监控系统部署:源码安装.md>中,主要进行了zabbix最新版的源码编译安装. (博客园地址:https://www.cnblogs.com/liwa ...

  6. 时间复杂度O(n^2)和O(nlog n)差距有多大?

    0. 时间复杂度 接触到算法的小伙伴们都会知道时间复杂度(Time Complexity)的概念,这里先放出(渐进)时间复杂度的定义: 假设问题规模是\(n\),算法中基本操作重复执行的次数是\(n\ ...

  7. CEPH Object Gateway

    参考文档: CEPH OBJECT GATEWAY:http://docs.ceph.com/docs/master/radosgw/ 一.环境准备 1. Ceph Object Gateway框架 ...

  8. springcloud 笔记

    官方教程 http://projects.spring.io/spring-cloud/ guide https://github.com/spring-guides 伪官方教程 https://sp ...

  9. PHP中报500错误时如何查看错误信息

    在执行代码中加入下面两行代码即可 ini_set("display_errors","On"); error_reporting(E_ALL);

  10. Python-元组-10

    元祖 Why:对于容器型数据类型list,无论谁都可以对其增删改查,那么有一些重要的数据放在list中是不安全的,所以需要一种容器类的数据类型存放重要的数据,创建之初只能查看而不能增删改,这种数据类型 ...