题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601

Problem Description
N sticks are arranged in a row, and their lengths are a1,a2,...,aN.

There are Q querys. For i-th of them, you can only use sticks between li-th to ri-th. Please output the maximum circumference of all the triangles that you can make with these sticks, or print −1 denoting no triangles you can make.

 
Input
There are multiple test cases.

Each case starts with a line containing two positive integers N,Q(N,Q≤105).

The second line contains N integers, the i-th integer ai(1≤ai≤109) of them showing the length of the i-th stick.

Then follow Q lines. i-th of them contains two integers li,ri(1≤li≤ri≤N), meaning that you can only use sticks between li-th to ri-th.

It is guaranteed that the sum of Ns and the sum of Qs in all test cases are both no larger than 4×105.

 
Output
For each test case, output Q lines, each containing an integer denoting the maximum circumference.
 
Sample Input
5 3
2 5 6 5 2
1 3
2 4
2 5
 
Sample Output
13
16
16
 
主席树求最大三条边,如果三边能组成三角形即为答案,否则再找第二大第三大第四大的边,一直找到符合的
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100005
#define ll long long
int T[maxn*],L[maxn*],R[maxn*],sum[maxn*],tot;
ll a[maxn],b[maxn];
inline int update(int pre,int l,int r,int x)
{
int rt=++tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre]+;
if(l<r)
{
int mid=l+r>>;
if(x<=mid)L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+,r,x);
}
return rt;
}
inline int query(int u,int v,int l,int r,int k)
{
if(l>=r)return l;
int x=sum[L[v]]-sum[L[u]],mid=l+r>>;
if(x>=k)return query(L[u],L[v],l,mid,k);
else return query(R[u],R[v],mid+,r,k-x);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int len=unique(b+,b++n)-b-;
T[]=L[]=R[]=sum[]=tot=;
for(int i=;i<=n;i++)
{
int pos=lower_bound(b+,b++len,a[i])-b;
T[i]=update(T[i-],,len,pos);
}
for(int i=;i<=m;i++)
{
int l,r;
int flag=;
ll ans=;
scanf("%d%d",&l,&r);
for(int i=r-l+;i>=;i--)
{
ll A=b[query(T[l - ],T[r],,len,i)];
ll B=b[query(T[l - ],T[r],,len,i-)];
ll C=b[query(T[l - ],T[r],,len,i-)];
if(B+C>A)
{
flag=;
ans=A+B+C;
break;
}
}
if(flag)printf("%lld\n",ans);
else printf("-1\n");
}
} return ;
}

hdu6601 主席树的更多相关文章

  1. hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树

    题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...

  2. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  3. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  4. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  5. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7143  Solved: 2968[Su ...

  8. [bzoj3932][CQOI2015][任务查询系统] (主席树)

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  9. [bzoj2588][count on a tree] (主席树+lca)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

随机推荐

  1. 陈志生:德国信贷工厂风控模式对P2P的启发

    上海合盘金融信息服务股份有限公司董事长陈志生 和讯银行消息 "2014中国金融论坛"于5月14-15日在北京召开,本次论坛主题为“全面深化金融体制改革与实体经济增长”.和讯网作为指 ...

  2. mac默认截图、截图代码

    苹果系统自带截图功能   1 截取全屏:快捷键(Shift+Command+3) 直接按“Shift+Command+3“快捷键组合,即可截取电脑全屏,图片自动保存在桌面. 2 截图窗口:快捷键(Sh ...

  3. js粘贴图片并显示

    var fromDa; $(function(){ document.getElementById('app').addEventListener('paste', function(e) { if( ...

  4. 【50.00%】【codeforces 747C】Servers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Linux内核 结构 struct urb

    struct urb 结构中和 USB 设备驱动有关的成员是: struct usb_device *dev 指向这个 urb 要发送到的 struct usb_device 的指针. 这个变量必须被 ...

  6. sed工具

    SED是一个非交互式文本编辑器,它可对文本文件和标准输入进行编辑,标准输入可以来自键盘输入.文本重定向.字符串.变量,甚至来自于管道的文本,与VIM编辑器类似,它一次处理一行内容,Sed可以编辑一个或 ...

  7. Cortex-A8/A76

    Cortex-A8 关于Cortex-A8的微处理架构参考<ARM_Cortex-A8微处理器的架构和实现> 其中关于NEON有两段话摘录如下: NEON媒体引擎拥有自己的10段流水线,它 ...

  8. C语言图形界面常用函数集锦

    (以下函数均应在图形方式初始之后使用(initgraph(a,b)),在win-tc中使用BGI图形程序模板时,其中已经定义有一个initgr函数,在main函数中应在执行initgr函数之后再使用这 ...

  9. js/jq判断鼠标滚轮方向

    js判断鼠标滚轮方向: var scrollFunc = function (e) { e = e || window.event; if (e.wheelDelta) { //判断浏览器IE,谷歌滑 ...

  10. callback、promise和async、await的使用方法

    callback 回调是一个函数被作为一个参数传递到另一个函数里,在那个函数执行完后再执行.通俗的讲就是 B函数被作为参数传递到A函数里,在A函数执行完后再执行B. promise Promise 是 ...