题目链接:http://codeforces.com/problemset/problem/484/E

题意:给你个n,n个木板都有高度,宽度都为1 ,给你两个数[l,r]和一个w,求在[l,r]区间的木板里宽度w的最大值,也就是连续的长为W的一段中最小的数字的最大值。

思路:首先想到了二分,找高度,然后就是如果对于每个木板高度,我们把大于等于i木板高度的线段树叶子设为1,其他为0,那么就肯定对于一个高度最长连续的1的值也就是维护一个线段树的和如果大于W

那么就肯定是可行的。所以是个求线段树中最长的连续的1的长度是多少的问题,这个问题要维护线段树的左边连续和,右边连续和,连续和的最大值。对高度从大到小排序,依次分别插入到n颗线段树中形成可持久化线段树(主席树),就能节省许多空间复杂度。维护起来 比较麻烦,好繁琐。。。

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
#include<set>
#define ll long long
using namespace std;
const int maxn=1e5+;
int root[maxn],cnt;
struct Node{
int val;
int id;
}a[maxn];
struct node{
int l;
int r;
int len;
int left;
int right;
int sum;
}tree[maxn*];
void pushup(int cur)
{
tree[cur].len=tree[tree[cur].l].len+tree[tree[cur].r].len;
tree[cur].left=tree[tree[cur].l].left;
if(tree[tree[cur].l].left==tree[tree[cur].l].len)
tree[cur].left+=tree[tree[cur].r].left;
tree[cur].right=tree[tree[cur].r].right;
if(tree[tree[cur].r].right==tree[tree[cur].r].len)
tree[cur].right+=tree[tree[cur].l].right;
tree[cur].sum=max(tree[tree[cur].l].sum,tree[tree[cur].r].sum);
tree[cur].sum=max(tree[cur].sum,tree[tree[cur].l].right+tree[tree[cur].r].left);
}
void build(int &cur,int l,int r)
{
cur=++cnt;
if(l==r)
{
tree[cur].left=;
tree[cur].right=;
tree[cur].sum=;
tree[cur].len=;
return ;
}
int m=(l+r)>>;
build(tree[cur].l,l,m);
build(tree[cur].r,m+,r);
pushup(cur);
}
void update(int &now,int last,int l,int r,int tar)
{
tree[++cnt]=tree[last];
now=cnt;
if(l==r)
{
tree[now].left=;
tree[now].right=;
tree[now].sum=;
tree[now].len=;
return ;
}
int m=(l+r)>>;
if(tar<=m)
update(tree[now].l,tree[last].l,l,m,tar);
else
update(tree[now].r,tree[last].r,m+,r,tar);
pushup(now);
}
int query(int now,int L,int R,int l,int r)
{
if(L<=l&&r<=R)
return tree[now].sum;
int m=(l+r)>>;
if(R<=m)
return query(tree[now].l,L,R,l,m);
if(L>m)
return query(tree[now].r,L,R,m+,r);
int res=;
res=max(query(tree[now].l,L,R,l,m),query(tree[now].r,L,R,m+,r));
int ll=min(tree[tree[now].l].right,m-L+);
int rr=min(tree[tree[now].r].left,R-m);
res=max(res,ll+rr);
return res;
}
bool cmp(Node x,Node y)
{
return x.val>y.val;
}
int main()
{
int n,m;
cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id=i;
}
sort(a+,a++n,cmp);
build(root[],,n);
for(int i=;i<=n;i++)
update(root[i],root[i-],,n,a[i].id);
scanf("%d",&m);
int ans=;
while(m--)
{
int l,r,w;
scanf("%d%d%d",&l,&r,&w);
int ll=,rr=n;
while(ll<=rr)
{
int mid=(ll+rr)>>;
int res=query(root[mid],l,r,,n);
if(res>=w)
{
ans=mid;
rr=mid-;
}
else ll=mid+;
} printf("%d\n",a[ans].val);
}
return ;
}

Sign on Fence(连续的长为W的一段中最小的数字的最大值)的更多相关文章

  1. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  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. CF 484E - Sign on Fence

    E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

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

  5. 【CF484E】Sign on Fence(主席树)

    [CF484E]Sign on Fence(主席树) 题面 懒得贴CF了,你们自己都找得到 洛谷 题解 这不就是[TJOI&HEOI 排序]那题的套路吗... 二分一个答案,把大于答案的都变成 ...

  6. AC日记——Sign on Fence Codeforces 484e

    E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

  7. 读入一个字符串str,输出字符串str中连续最长的数字串

    要求: 读入一个长度不超过256的字符串,例如“abc123defg123456789hjfs123456”.要求输出“123456789” 思路: 遍历字符串,如果是数字串则计算往后一共有多少个数字 ...

  8. ZT 查找字符串中连续最长的数字串

    查找字符串中连续最长的数字串 有俩方法,1)比较好理解一些.2)晦涩 1) /* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回, 并把这个最长数字串付给其中一个函数参数outputstr ...

  9. CF484E Sign on Fence && [国家集训队]middle

    CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...

随机推荐

  1. docker 一些命令

    docker的基本命令 (1)创建一个虚拟机:docker-machine create --driver virtualbox default, (2)列出所有虚拟机:docker-machine ...

  2. Mybatis-学习笔记(5)动态SQL

    1.Mybatis采用功能强大的基于ONGL的表达式来完成动态SQL. 2.ONGL常用的元素有: 1>if <if test="id != null "> an ...

  3. shell学习笔记3---shell变量

    Shell变量的定义.赋值和删除 脚本语言在定义变量时通常不需要指明类型,直接赋值就可以,Shell 变量也遵循这个规则. 在 Bash shell 中,每一个变量的值都是字符串,无论你给变量赋值时有 ...

  4. link标签中的integrity和crossorigin字段

    https://blog.csdn.net/SNP_fuyun/article/details/77113985?locationNum=10&fps=1

  5. Scrapy 教程(九)-日志系统

    最新版本的 scrapy 已经废弃了 scrapy.log 的使用,赞成显示调用python标准日志记录. Python 内建日志系统 import logging ### python 内建 log ...

  6. Sql 字符串自增列的实现

    ALTER FUNCTION [dbo].[f_NextID](@tabname VARCHAR()) RETURNS ) AS BEGIN DECLARE @charval CHAR() IF LO ...

  7. Trait讲解

    <?php /** * Trait解决PHP单继承的一种方法,使开发人员在不同层次结构的类中复用属性和方法 * Trait无法实例化 * Trait不是类,不能被继承,所以不能再Trait中不能 ...

  8. Android 点击跳转到蓝牙设置界面

    不再重写一遍了,看csdn: https://blog.csdn.net/qq_42866164/article/details/101353709

  9. poj 1564 Sum It Up(dfs)

    Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7191   Accepted: 3745 Descrip ...

  10. AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)

    Problem Statement You are given two integer sequences, each of length N: a1,…,aN and b1,…,bN. There ...