hdu6601 主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601
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.
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.
- #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 主席树的更多相关文章
- hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树
题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...
- bzoj3207--Hash+主席树
题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
- 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 ...
- BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7143 Solved: 2968[Su ...
- [bzoj3932][CQOI2015][任务查询系统] (主席树)
Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
随机推荐
- 访问HTML可以,访问PHPfile not found
www目录的所有者和所属组都改为nginx用户试一下. 参考命令: chown nginx.nginx /home/www ps aux |grep nginx 看一下您的nginx是以哪个用户的 ...
- Python--day36--操作系统的作用;多道技术;
- UVA 1347"Tour"(经典DP)
传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...
- H3C VLSM
- HDU 1087 Super Jumping....(动态规划之最大递增子序列和)
Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...
- Js中没有方法的重载
<script type="text/javascript"> //方法名相同,后面的方法覆盖了前面的方法.后面的方法需要一个name,但没给传值,所以是undefin ...
- 2018-2-13-Visual-studio-C#-代码使用-NotNull
title author date CreateTime categories Visual studio C# 代码使用 NotNull lindexi 2018-2-13 17:23:3 +080 ...
- BZOJ3527 推出卷积公式FFT求值
BZOJ3527 推出卷积公式FFT求值 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 题意: \(F_{j}=\sum_{i&l ...
- nixyx —— 一个小巧的项目工程/编译文件生成器(构建系统?)
恩..nixyx确实算不上是一个构建系统. 所谓构建系统,比如GNU的Autotools,那是一套很完整的构建体系,包括了程序的配置,编译和安装三大部分. 类似的软件还有:google的gyp.腾讯的 ...
- android studio 配置HTTP proxy
Android SDK在线更新镜像服务器 南阳理工学院镜像服务器地址: mirror.nyist.edu.cn 端口:80 中国科学院开源协会镜像站地址: IPV4/IPV6: mirrors.ope ...