杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解
题意:
有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形。棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\)。
思路:
由于不构成三角形的数组为菲波那切数列,所以当棍数超过44时,长度超过1e9,所以从最大开始数最多不超过45次就能找到构成三角形。所以直接主席树查询区间第k大。复杂度\(O(45 * q * logn)\)。
代码:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<stack>
#include<ctime>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
using namespace std;
int n, q, tot;
int root[maxn];
ll a[maxn];
vector<ll> vv;
int getId(ll x){
return lower_bound(vv.begin(), vv.end(),x) - vv.begin() + 1;
}
struct node{
int lson, rson;
int sum;
}T[maxn * 40];
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> 1;
if(m >= pos)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + 1, r, T[now].rson, T[pre].rson, v, pos);
}
int query(int l, int r, int pre, int now, int k){
if(l == r) return l;
int m = (l + r) >> 1;
int sum = T[T[now].lson].sum - T[T[pre].lson].sum;
if(sum >= k)
return query(l, m, T[pre].lson, T[now].lson, k);
else
return query(m + 1, r, T[pre].rson, T[now].rson, k - sum);
}
void init(){
memset(T, 0, sizeof(T));
tot = 0;
vv.clear();
}
int main(){
while(~scanf("%d%d", &n, &q)){
init();
for(int i = 1; i <= n; i++){
scanf("%lld", &a[i]), vv.push_back(a[i]);
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
for(int i = 1; i <= n; i++){
update(1, vv.size(), root[i], root[i - 1], 1, getId(a[i]));
}
while(q--){
int l, r;
scanf("%d%d", &l, &r);
int num = 0;
ll ans = -1;
ll a1, a2, a3;
for(int i = r - l + 1; i >= 1; i--){
if(num == 0){
a3 = query(1, vv.size(), root[l - 1], root[r], i);
num++;
}
else if(num == 1){
a2 = query(1, vv.size(), root[l - 1], root[r], i);
num++;
}
else if(num == 2){
a1 = query(1, vv.size(), root[l - 1], root[r], i);
num++;
}
else{
a3 = a2, a2 = a1;
a1 = query(1, vv.size(), root[l - 1], root[r], i);
}
if(num == 3 && vv[a1 - 1] + vv[a2 - 1] > vv[a3 - 1]){
ans = vv[a1 - 1] + vv[a2 - 1] + vv[a3 - 1];
break;
}
}
printf("%lld\n", ans);
}
}
return 0;
}
杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解的更多相关文章
- HDU - 6601 Keen On Everything But Triangle 主席树
Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...
- 杭电多校HDU 6579 Operation (线性基 区间最大)题解
题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...
- 杭电多校HDU 6656 Kejin Player(概率DP)题解
题意: 最低等级\(level\ 1\),已知在\(level\ i\)操作一次需花费\(a_i\),有概率\(p_i\)升级到\(level\ i+1\),有\(1 - p_i\)掉级到\(x_i( ...
- 杭电多校HDU 6599 I Love Palindrome String (回文树)题解
题意: 定义一个串为\(super\)回文串为: \(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\) \(\bullet\ ...
- 杭电多校HDU 6586 String(预处理 + 贪心)题解
题意: 给你一个串,现需要你给出一个子序列,满足26个约束条件,\(len(A_i) >= L_i\) 且 \(len(A_i) <= R_i\), \(A_i\)为从a到z的26个字母. ...
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
- [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...
- 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)
题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...
- 2019杭电多校第三场hdu6609 Find the answer(线段树)
Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...
随机推荐
- 从零开始学spring源码之xml解析(一):入门
谈到spring,首先想到的肯定是ioc,DI依赖注入,aop,但是其实很多人只是知道这些是spring核心概念,甚至不知道这些代表了什么意思,,作为一个java程序员,怎么能说自己对号称改变了jav ...
- Linux系统中的Page cache和Buffer cache
Linux系统中的Page cache和Buffer cache Linux中有两个很容易混淆的概念,pagecache和buffercache,首先简单将一些Linux系统下内存的分布,使用free ...
- RPC 框架要实现这个功能,我们可以使用泛化调用。那什么是泛化调用呢?我们带着这个问题,先学习下如何在没有接口的情况下进行 RPC 调用。
RPC 框架要实现这个功能,我们可以使用泛化调用.那什么是泛化调用呢?我们带着这个问题,先学习下如何在没有接口的情况下进行 RPC 调用.
- nginx proxy pass redirects ignore port
nginx proxy pass redirects ignore port $host in this order of precedence: host name from the request ...
- assert False 与 try 结合 在开发中的使用
让错误抛出 发现其中的问题 # coding=utf-8 from rest_framework.views import exception_handler from rest_framework. ...
- LOJ10159旅游规划
题目描述 W 市的交通规划出现了重大问题,市政府下定决心在全市各大交通路口安排疏导员来疏导密集的车流.但由于人员不足,W 市市长决定只在最需要安排人员的路口安排人员. 具体来说,W 市的交通网络十分简 ...
- Eslint错误提示
"Missing semicolon." : "缺少分号.","Use the function form of \"use strict\ ...
- JDBC连接数据库,数据库访问层
为什么需要JDBC JDBC API DriverManager JDBC驱动 JDBC的功能 JDBC步骤 数据访问层DAO DAO模式的组成 DAO模式的实际应用 为什么需要JDBC? JDBC是 ...
- ajax带checkbox选择值到后台
function togglefun(checkbox){//全选 $('input[name=cids]').prop('checked', $(checkbox).prop('checked')) ...
- 【函数分享】每日PHP函数分享(2021-2-6)
array_combine - 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 说明: array_combine ( array $keys , array $values ) : ...