codeforces 484E
题意:给定n<=105的数组h,有m<=105的询问,每个询问为l,r,w求[l,r]区间内连续w个的最小高度最大是多少..
思路:首先把h数组从大到小排序,然后用建立一个可持久化的下标线段树,线段树维护区间最长的连续长度为多少。
那么对于每个询问直接二分高度,然后查询这个高度之前的线段树[l,r]区间是否存在至少w个连续的。。
以前总是套主席树模板,这次自己写感觉也还是蛮好写的。。
据说cdq分治也可搞。。应该是整体二分吧。。明天再想想
code:
/*
* Author: Yzcstc
* Created Time: 2014/11/10 22:17:29
* File Name: cf276E.cpp
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#define M0(a) memset(a, 0, sizeof(a))
#define N 101010
#define M 3100000
#define x first
#define y second
#define ls lson[rt]
#define rs rson[rt]
using namespace std;
typedef pair<int, int> pii;
struct node{
int c, lc, rc;
node(int _c = , int _lc = , int _rc = ):c(_c), lc(_lc), rc(_rc){}
};
pair<int, int> p[N];
int T[N], lc[M], rc[M], c[M], lson[M], rson[M], tot;
int n, m; int build(const int l, const int r){
int root = tot++;
c[root] = lc[root] = rc[root] = ;
if (l < r){
int mid = (l + r) >> ;
lson[root] = build(l, mid);
rson[root] = build(mid+, r);
}
return root;
} void push_up(const int& rt, const int& llen, const int &rlen){
lc[rt] = (c[ls] >= llen) ? llen + lc[rs] : lc[ls];
rc[rt] = (c[rs] >= rlen) ? rlen + rc[ls] : rc[rs];
c[rt] = max(c[ls], c[rs]), c[rt] = max(lc[rs] + rc[ls], c[rt]);
} int update(const int rt, const int l, const int r, const int& p){
int root = tot++;
if (p <= l && r <= p){
c[root] = rc[root] = lc[root] = ;
return root;
}
int mid = (l + r) >> ;
lson[root] = ls, rson[root] = rs;
if (p <= mid)
lson[root] = update(ls, l, mid, p);
else
rson[root] = update(rs, mid+, r, p);
push_up(root, mid-l+, r-mid);
return root;
} node tmp, t;
void merge(node &a, const node& b,const int& llen,const int& rlen){
t.c = max(a.c, b.c), t.c = max(t.c, a.rc + b.lc);
t.lc = (a.lc >= llen) ? a.lc + b.lc : a.lc;
t.rc = (b.rc >= rlen) ? b.rc + a.rc : b.rc;
a = t;
} node query(const int& rt,const int& l,const int& r,const int& L,const int& R){
if (L <= l && r <= R) return node(c[rt], lc[rt], rc[rt]);
int mid = (l + r) >> ;
if (R <= mid) return query(ls, l, mid, L, R);
else if (L > mid) return query(rs, mid + , r, L, R);
else{
node res = query(ls, l, mid, L, R);
tmp = query(rs, mid+, r, L, R);
merge(res, tmp, mid - max(L, l) + , min(R,r) - mid);
return res;
}
} void init(){
scanf("%d", &n);
tot = ;
for (int i = ; i <= n; ++i)
scanf("%d", &p[i].x), p[i].y = i;
sort(p + , p + + n, greater<pii>() );
} void solve(){
T[] = build(, n);
for (int i = ; i <= n; ++i)
T[i] = update(T[i-], , n, p[i].y);
int q, ll, rr, w, l, r, mid, ans;
scanf("%d", &q);
while (q--){
scanf("%d%d%d", &ll, &rr, &w);
l = , r = n, ans = ;
while (l <= r){
mid = (l + r) >> ;
if (query(T[mid], , n, ll, rr).c >= w){
ans = max(ans, p[mid].x);
r = mid - ;
} else l = mid + ;
}
printf("%d\n", ans);
} } int main(){
init();
solve();
return ;
}
codeforces 484E的更多相关文章
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- Sign on Fence CodeForces - 484E
http://codeforces.com/problemset/problem/484/E 题意: 给定一个长度为n的数列,有m次询问,询问形如l r k 要你在区间[l,r]内选一个长度为k的区间 ...
- 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 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- IIS内存溢出-设置IIS的应用程序池
在ASP.NET Web服务器上,ASP.NET所能够用到的内存,通常不会等同于所有的内存数量.在machine.config(C:/WINDOWS/Microsoft.NET/Framework/v ...
- python实践——批量统计mongodb数据库的集合大小
#!/usr/bin/env python import os,sys list = [] for i in range(3,50): l = os.popen("/bin/echo 'sh ...
- HTML 导航框架 (使用 Frame )
文章出处 : http://blog.sina.com.cn/s/blog_6f6d07f80100oqlt.html (本文转自此博客) 在浏览网页时,我们经常会看到一个导航条,当点击导航条上 ...
- python-mysqldb安装
出现错误 command 'gcc' failed with exit status 1 解决办法: yum install python-devel mysql-devel zlib-devel ...
- JavaScript Window 对象
< JavaScript Window Object > && < IE check > JavaScript Window Object Window.loa ...
- 关于在gridview中有dorpdownlist的情况下使用自带编辑模板的方法
今天记录一下在gridview中,如果有dropdownlist的情况下使用gridview自带编辑模式的方法. 好吧,今天的这个问题有点绕,详细解释一下目的. 因为gridview中的某些列的数据是 ...
- 第五章 springboot + mybatis(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...
- display转块状化
display:block block元素会独占一行,多个block元素会各自新起一行.默认情况下,block元素宽度自动填满其父元素宽度. block元素 ...
- 危险的“我以为”DDoS&丑陋的现实
有些话题可能终其一生你也可以假装不知道就好了,比如全球气候在变暖,不过有些话题确是不得不面对的冰冷现实,在互联网日益发达的当下,比如你不得不面对的DDoS攻击. DDoS代表了分布式拒绝服务,通过许多 ...
- Session失效之 IE iframe cookie问题(p3p)
项目中,在做门户系统时,使用了iframe嵌套展示各个子系统的页面,其中有个页面在ie8下,始终无法正常登陆. 后来项目经理分析,应该是iframe跨域导致,赶忙查看了连接地址,还真是一个跨域的页面. ...