2019多校训练第二场 hdu6601(一个致命错误的反思)
题目链接和题意
Keen On Everything But Triangle
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2305 Accepted Submission(s): 501
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
题意:给出n根棒子的长度,有m次询问,求在区间[l, r]之间取出3根棒子组成三角形的最大周长。
解法
这题当场我没写出来,赛后写了WA了一天。刚刚才过的。
每次取出区间里前三长的棒子,看看这三根能否组成三角形,如果不能,就接着取第第2、3、4长的棒子,以此类推,直到取到为止。并且,用斐波那契额可以证明,在棒子长度为1~1e9的范围内,最多44次就可以凑出一个三角形来。
那么问题是,怎么取第1、2、3长的棒子呢?
用线段树,查询区间[l, r]的最大值以及最大值所在的下标(构造的时候把最大值的下标也存进去)。然后用一个pair t1存下来最大值和下标,推到一个队列里,接着再把这个最大值改成0。再次查询[l, r]的最大值,因为前面已经把最大值改成0了,所以这次查的是第二大的值,记为t2,查完了置零,把值和下标丢进队列里。第三大也如此,记为t3。然后我们判断1、 2、 3行不行,不行的话就t1 = t2, t2 = t3
,t3 = 新查询的值。也就是说现在t1, t2, t3存的是第2、3、4大的值,每次查询置零完了都丢到队列里。知道找到为止,输出结果。因为队列里存的是元素和下标,所以我们可以把队列里的东西一个一个再放回到线段树对应的位置里去。
我代码实现上的一个致命bug:本人写代码的时候粗心犯了一个错误,对于每个l和r,我们最多查询k - min(r - l + 1, 65)次,最后也要放回这么多次,我在for循环外面查了两次,也就是最大值和次大值,但我没有让k - 2。在接下来的for循环迭代中又查询了k次,这样子我最多一共就查询了k + 2次(for循环里k次,循环外2次)。因为我每次查询完了都置零然后放到队列里,如果查询超过k次,那也就是说k个元素都被置0了,再查询出来的肯定也是0,然后我放到队列里,这样子最后恢复的时候就会把那个元素给置0了而不是恢复原样。
当然,思路是没错的,只是代码实现的时候出了bug,一定要引以为戒,所以写这篇文章来记录这次错误。
另外要注意,当数据取到极限1e9时,三个1e9相加会爆int,因此有的地方需要用long long
AC代码
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <cmath>
using namespace std;
const int SIZE = 1e6 + 7;
struct SegementTree{
int l, r;
int val;
int id;
} tree[SIZE * 4];
int a[SIZE];
int n, m;
void build(int p, int l, int r){
tree[p].l = l;
tree[p].r = r;
if (l == r){
tree[p].val = a[l];
tree[p].id = l;
return;
}
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
if (tree[p * 2].val > tree[p * 2 + 1].val){
tree[p].val = tree[p * 2].val;
tree[p].id = tree[p * 2].id;
}
else{
tree[p].val = tree[p * 2 + 1].val;
tree[p].id = tree[p * 2 + 1].id;
}
}
pair<long long, long long> ask(int p, int l, int r){
// 返回的pair第一维是值,第二维是指所在的下标
if (tree[p].l >= l && tree[p].r <= r){
return make_pair(tree[p].val, tree[p].id);
}
int mid = (tree[p].l + tree[p].r) / 2;
pair<long long , long long> a, b;
if (l <= mid){
a = ask(p * 2, l, r);
}
if (r > mid){
b = ask(p * 2 + 1, l, r);
}
if (a.first > b.first){
return a;
}
else{
return b;
}
}
void change(int p, int x, int v){
if (tree[p].l == tree[p].r){
tree[p].val = v;
a[tree[p].l] = v;
return;
}
int mid = (tree[p].l + tree[p].r) / 2;
if (x <= mid){
change(p * 2, x, v);
}
else{
change(p * 2 + 1, x, v);
}
if (tree[p * 2].val > tree[p * 2 + 1].val){
tree[p].val = tree[p * 2].val;
tree[p].id = tree[p * 2].id;
}
else{
tree[p].val = tree[p * 2 + 1].val;
tree[p].id = tree[p * 2 + 1].id;
}
}
pair<long long, long long> t1, t2, t3;
int main(){
int l, r;
while (scanf("%d%d", &n, &m) != EOF){
for (int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
build(1, 1, n);
while (m--){
scanf("%d%d", &l, &r);
long long ans = -1;
int k = min(r - l + 1, (int)64);
if (k < 3){
printf("-1\n");
continue;
}
queue<pair<long long, long long> > q;
t1 = ask(1, l, r);
q.push(make_pair(t1.first, t1.second));
change(1, t1.second, 0);
k--;
t2 = ask(1, l, r);
change(1, t2.second, 0);
q.push(make_pair(t2.first, t2.second));
k--;
for (int i = 1; i <= k; i++){
t3 = ask(1, l, r);
change(1, t3.second, 0);
q.push(make_pair(t3.first, t3.second));
if ((long long)t2.first + (long long)t3.first > (long long)t1.first){
ans = t1.first + t2.first + t3.first;
break;
}
t1 = t2;
t2 = t3;
}
while (!q.empty()){
change(1, q.front().second, q.front().first);
q.pop();
}
// cout << ans << endl;
printf("%lld\n", ans);
}
}
}