题目链接: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
 
主席树求最大三条边,如果三边能组成三角形即为答案,否则再找第二大第三大第四大的边,一直找到符合的
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5. #define maxn 100005
  6. #define ll long long
  7. int T[maxn*],L[maxn*],R[maxn*],sum[maxn*],tot;
  8. ll a[maxn],b[maxn];
  9. inline int update(int pre,int l,int r,int x)
  10. {
  11. int rt=++tot;
  12. L[rt]=L[pre];
  13. R[rt]=R[pre];
  14. sum[rt]=sum[pre]+;
  15. if(l<r)
  16. {
  17. int mid=l+r>>;
  18. if(x<=mid)L[rt]=update(L[pre],l,mid,x);
  19. else R[rt]=update(R[pre],mid+,r,x);
  20. }
  21. return rt;
  22. }
  23. inline int query(int u,int v,int l,int r,int k)
  24. {
  25. if(l>=r)return l;
  26. int x=sum[L[v]]-sum[L[u]],mid=l+r>>;
  27. if(x>=k)return query(L[u],L[v],l,mid,k);
  28. else return query(R[u],R[v],mid+,r,k-x);
  29. }
  30. int main()
  31. {
  32. int n,m;
  33. while(scanf("%d%d",&n,&m)!=EOF)
  34. {
  35. for(int i=;i<=n;i++)
  36. {
  37. scanf("%lld",&a[i]);
  38. b[i]=a[i];
  39. }
  40. sort(b+,b++n);
  41. int len=unique(b+,b++n)-b-;
  42. T[]=L[]=R[]=sum[]=tot=;
  43. for(int i=;i<=n;i++)
  44. {
  45. int pos=lower_bound(b+,b++len,a[i])-b;
  46. T[i]=update(T[i-],,len,pos);
  47. }
  48. for(int i=;i<=m;i++)
  49. {
  50. int l,r;
  51. int flag=;
  52. ll ans=;
  53. scanf("%d%d",&l,&r);
  54. for(int i=r-l+;i>=;i--)
  55. {
  56. ll A=b[query(T[l - ],T[r],,len,i)];
  57. ll B=b[query(T[l - ],T[r],,len,i-)];
  58. ll C=b[query(T[l - ],T[r],,len,i-)];
  59. if(B+C>A)
  60. {
  61. flag=;
  62. ans=A+B+C;
  63. break;
  64. }
  65. }
  66. if(flag)printf("%lld\n",ans);
  67. else printf("-1\n");
  68. }
  69. }
  70.  
  71. return ;
  72. }

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. 访问HTML可以,访问PHPfile not found

    www目录的所有者和所属组都改为nginx用户试一下. 参考命令: chown nginx.nginx /home/www ps aux |grep nginx   看一下您的nginx是以哪个用户的 ...

  2. Python--day36--操作系统的作用;多道技术;

  3. UVA 1347"Tour"(经典DP)

    传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...

  4. H3C VLSM

  5. HDU 1087 Super Jumping....(动态规划之最大递增子序列和)

    Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...

  6. Js中没有方法的重载

    <script type="text/javascript"> //方法名相同,后面的方法覆盖了前面的方法.后面的方法需要一个name,但没给传值,所以是undefin ...

  7. 2018-2-13-Visual-studio-C#-代码使用-NotNull

    title author date CreateTime categories Visual studio C# 代码使用 NotNull lindexi 2018-2-13 17:23:3 +080 ...

  8. BZOJ3527 推出卷积公式FFT求值

    BZOJ3527 推出卷积公式FFT求值 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 题意: \(F_{j}=\sum_{i&l ...

  9. nixyx —— 一个小巧的项目工程/编译文件生成器(构建系统?)

    恩..nixyx确实算不上是一个构建系统. 所谓构建系统,比如GNU的Autotools,那是一套很完整的构建体系,包括了程序的配置,编译和安装三大部分. 类似的软件还有:google的gyp.腾讯的 ...

  10. android studio 配置HTTP proxy

    Android SDK在线更新镜像服务器 南阳理工学院镜像服务器地址: mirror.nyist.edu.cn 端口:80 中国科学院开源协会镜像站地址: IPV4/IPV6: mirrors.ope ...