链接: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. LVDS原理及设计指南--以及衍生的B-LVDS-M-LVDS--CML-LVPECL电平等

    LVDS是一种低摆幅的差分信号技术,它使得信号能在差分PCB 线对或平衡电缆上以几百Mbps的速率传输,其低压幅和低电流驱动输出实现了低噪声和低功耗.      IEEE 在两个标准中对LVDS 信号 ...

  2. Ubuntu 安装google chrome

    sudo apt-get install google-chrome-stable /usr/bin/google-chrome-stable

  3. Ionic App 启动时报Application Error - The connection to the server was unsuccessful

    最近在更新App的时候,发现在华为手机上报这个错误,有点困惑,查找资料分析,大概原因是程序在加载index.html网页时,加载的资源过多,造成时间超时, 这个时原因分析https://stackov ...

  4. [转]zookeeper集群 initLimit和syncLimit

    initLimit和syncLimit是针对集群的参数 1.tickTime:CS通信心跳数 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就 ...

  5. NOIp2018停课刷题记录

    Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...

  6. Luogu P2597 [ZJOI2012]灾难

    一道非常综合的好题然后就莫名其妙地知道了动态LCA的求法 果然是ZJOI的题目,只能说这思路服了 首先我们发现每次操作只会灭绝一种动物,然后我们想一下就知道如果有\(n(n>=2)\)个食物的动 ...

  7. .net mvc数据库操作添加数据的几中方法

    1. Defining sets on a derived context 1) DbSet属性:指定集合为Entity类型 2) IDbSet属性 3) 只读set属性 public IDbSet& ...

  8. LESS SCSS 预编译语言

      前  言 JRedu LESS  是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. LESS 为 Web 开发者带来了福音,它在 ...

  9. 解密自动CPS变换

    7.2 1 前言 我最一开始听到 CPS 变换这个词是在王垠的博客里 (请求不要喷我),就是那篇他第一次宣传他的40行代码的文章. 我当时什么都看不懂,所以没太注意,不过我也正在学程序语言方面的东西, ...

  10. linux下监控某个目录是否被更改

    需求:对一个目录(比如/data/test)进行监控,当这个目录下文件或子目录出现变动(如修改.创建.删除.更名等操作)时,就发送邮件!针对上面的需求,编写shell脚本如下: [root@cento ...