记录一个菜逼的成长。。
就是枚举区间,rmq查询区间最大值。
ps:跟暴力程序对拍了下才发现查询函数写错了。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define clr clear()
#define pb push_back
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 5000 + 10;
LL a[maxn],b[maxn];
struct Node{
int l,r;
LL mx;
}node[maxn<<2];
void pushup(int t)
{
node[t].mx = max(node[t<<1].mx,node[t<<1|1].mx);
}
void build(int t,int l,int r)
{
node[t].l = l;
node[t].r = r;
if(l == r){
node[t].mx = a[l];
return ;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(t);
}
LL query(int t,int l,int r)
{
if(node[t].l >= l && node[t].r <= r){
return node[t].mx;
}
int mid = (node[t].l + node[t].r) >> 1;
LL ret = 0;
if(l <= mid)ret = max(ret,query(t<<1,l,r));
if(r > mid)ret = max(ret,query(t<<1|1,l,r));
return ret;
}
int main()
{
//fin,fout;
int T;
scanf("%d",&T);
while(T--){
int n;
LL k;
scanf("%d%lld",&n,&k);
for( int i = 1; i <= n; i++ )
scanf("%lld",a+i);
for( int i = 1; i <= n; i++ )
scanf("%lld",b+i);
build(1,1,n);
LL ans = 0,s1 = 0,s2 = 0,ans1 = 0;
int l,r;
for( int j = 1,i = 1; i <= n; i++ ){
while(j <= n && s1 + a[j] - query(1,i,j) <= k){s1 += a[j];s2 += b[j++];}
ans = max(ans,s2);
s1 -= a[i];s2 -= b[i];
}
printf("%lld\n",ans);
}
return 0;
}