wa了快一个月,原来是维护区间最值的树状数组的更新函数写挂了,:)
这错误写法能过洛谷完全因为它是不带修改的…
正确的:
struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x)
{
while(x<=n){
h[x]=aa[x];
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return ans;
}
};
错误的:
struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x,int k)
{
while(x<=n){
h[x]=k;//错在这里!!!!
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return ans;
}
};
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr1(x) printf("Case %d: ",x)
#define pn1(x) printf("Case %d:\n",x)
#define pr2(x) printf("Case #%d: ",x)
#define pn2(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define sz(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
LL tt,k,n,aa[(int)1e5+9],rk[(int)1e5+9];
struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x)
{
while(x<=n){
h[x]=aa[x];
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return ans;
}
};
void solve(){
cin>>n>>k;
f(i,1,n)cin>>aa[i],rk[i]=i;
ll ans=0;
BIT b;
if(k==0){
b.init(n);
for(int i=1;i<n;++i){
ans=max(ans,min(min(aa[i],aa[i+1]),b.gmin(1,n)*2ll));
}
}
else {
sort(rk+1,rk+1+n,[](long long x,long long y){
return aa[x]<aa[y];
});
for(int i=1;i<k;++i)aa[rk[i]]=1e9;
b.init(n);
aa[n+1]=-10086;
int ma=0,ma2=0;
for(int i=1;i<n;++i){
int tp=min(aa[i],aa[i+1]);
if(tp>=ma){
ma2=ma,ma=tp;
}else if(tp>ma2)ma2=tp;
}
for(int i=1;i<=n;++i){
LL tmp=aa[i];
aa[i]=1e9;
b.update(i);
int usma=max(min(aa[i],aa[i+1]),min(aa[i-1],aa[i]))==ma&&aa[i]==ma?ma2:ma;
usma=max({aa[i-1],aa[i+1],usma+0ll});
ans=max(ans,min(usma+0ll,b.gmin(1,n)*2));
//cerr<<i<<"::"<<usma<<"__"<<b.gmin(1,n)<<"+++"<<ans<<"\n";
aa[i]=tmp;
b.update(i);
}
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
f(sb,1,tt){
solve();
}
return 0;
}