Keen On Everything But Triangle
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
一道模板题,主席树是,求第k小的模板。
题意:求这个区间内能组成的最大三角形,若不能就输出-1
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct node
{
int l, r, sum;
} tree[N * 20];
int root[N], s[N];
vector<int>vec;
int cnt;
void build(int &now, int l, int r)
{
now = ++cnt;
if(l == r)
{
tree[now].sum = 0;
return;
}
int mid = (l + r) >> 1;
build(tree[now].l, l, mid);
build(tree[now].r, mid + 1, r);
}
int getid(int x)
{
return lower_bound(vec.begin(), vec.end(), x) - vec.begin() + 1;
}
void update(int l, int r, int &x, int y, int pos)
{
x = ++cnt;
tree[x] = tree[y];
tree[x].sum++;
if(l == r)
return;
int mid = (l + r) >> 1;
if(pos <= mid)
update(l, mid, tree[x].l, tree[y].l, pos);
else
update(mid + 1, r, tree[x].r, tree[y].r, pos);
}
int query(int l, int r, int x, int y, int k)
{
if(l == r)
return l;
int mid = (l + r) >> 1;
int sum = tree[tree[y].l].sum - tree[tree[x].l].sum;
if(sum >= k)
return query(l, mid, tree[x].l, tree[y].l, k);
else
return query(mid + 1, r, tree[x].r, tree[y].r, k - sum);
}
int main()
{
int n, m;
while(scanf("%d %d", &n, &m) == 2)
{
vec.clear();
cnt = 0;
build(root[0], 1, n);
for(int i = 1; i <= n; i++)
scanf("%d", &s[i]), vec.push_back(s[i]);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i = 1; i <= n; i++)
update(1, n, root[i], root[i - 1], getid(s[i]));
for(int i = 1; i <= m; i++)
{
int l, r;
scanf("%d %d", &l, &r);
int t = r - l + 1;
if(t < 3)
{
printf("-1\n");
}
else
{
deque<ll> q;
int a = vec[query(1, n, root[l - 1], root[r], t--) - 1];
int b = vec[query(1, n, root[l - 1], root[r], t--) - 1];
int c = vec[query(1, n, root[l - 1], root[r], t--) - 1];
q.push_back(a);
q.push_back(b);
q.push_back(c);
long long ans = 0;
bool flag = false;
while(t >= 0)
{
if(q[0] + q[1] > q[2] && q[0] + q[2] > q[1] && q[2] + q[1] > q[0])
{
ans = q[0] + q[1] + q[2];
flag = true;
break;
}
if(!flag)
{
q.push_back(vec[query(1, n, root[l - 1], root[r], t--) - 1]);
q.pop_front();
}
}
if(flag)
{
printf("%lld\n", ans);
}
else
printf("-1\n");
}
}
}
return 0;
}