Sign on Fence(连续的长为W的一段中最小的数字的最大值)
题目链接: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的一段中最小的数字的最大值)的更多相关文章
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- 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的数 维护 ...
- CF 484E - Sign on Fence
E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...
- 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 ...
- 【CF484E】Sign on Fence(主席树)
[CF484E]Sign on Fence(主席树) 题面 懒得贴CF了,你们自己都找得到 洛谷 题解 这不就是[TJOI&HEOI 排序]那题的套路吗... 二分一个答案,把大于答案的都变成 ...
- 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 ...
- 读入一个字符串str,输出字符串str中连续最长的数字串
要求: 读入一个长度不超过256的字符串,例如“abc123defg123456789hjfs123456”.要求输出“123456789” 思路: 遍历字符串,如果是数字串则计算往后一共有多少个数字 ...
- ZT 查找字符串中连续最长的数字串
查找字符串中连续最长的数字串 有俩方法,1)比较好理解一些.2)晦涩 1) /* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回, 并把这个最长数字串付给其中一个函数参数outputstr ...
- CF484E Sign on Fence && [国家集训队]middle
CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...
随机推荐
- vue2.0父子组件通信以及同级组件通信
1.父向子通信 父组件为singer.vue.子组件为list-view.vue.需要把歌手的数据传给子组件.则绑定 :data = 'singers' ,singers为父组件的值.data为子组件 ...
- 使用screen管理后台程序
我们常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了.这时,我们可以用sc ...
- Java第三周课程总结&实验报告一
第三周课程总结 1.关于面向对象的一些具体内容,明白了类与对象以及Java的封装性和构造方法以及对对象匿名的相关知识. 2.this关键字,它是表示类的成员属性(变量),使用this构造方法时必须放在 ...
- API接口设计
1.场景描述 比如说我们要做一款APP,需要通过api接口给app提供数据.假设我们是做商城,比如我们卖书的.我们可以想象下这个APP大概有哪些内容: 1)首页:banner区域(可以是一些热门书籍的 ...
- 时间处理插件moment.js
monment.js插件 处理时间:http://momentjs.cn/
- python爬取b站排行榜
爬取b站排行榜并存到mysql中 目的 b站是我平时看得最多的一个网站,最近接到了一个爬虫的课设.首先要选择一个网站,并对其进行爬取,最后将该网站的数据存储并使其可视化. 网站的结构 目标网站:bil ...
- 学习C++的意义
1,常见的观点: 1,并不是每个应届生都有机会写操作系统和驱动程序: 2,嵌入式系统也是软家系统,只不过是软件在出厂的时候已经被烧写到硬件中了,用户没有办法修改软件而已,因此嵌入式系统也是软件系统,C ...
- Django中Model进阶操作
一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pr ...
- HDU 1594 find the max
数序问题. 题意是说 一个数列 a1,a2,--ai,--an; x=i , y = ai:找两个点斜率绝对值.!最大. 第一次没找绝对值,--认真读题. .. x 每次加1 . 仅仅须要找 相邻的 ...
- Linux系统性能测试工具(八)——网络性能测试工具之netperf
本文介绍关于Linux系统(适用于centos/ubuntu等)的网络性能测试工具-iperf.磁盘io性能测试工具包括: iperf: netperf 参考链接:https://www.jiansh ...