1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define fi first #define se second #define pb push_back #define SZ(a) ((int) a.size()) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define ROF(i, a, b) for (int i = (a); i >= (b); --i) #define getFro(vec) (vec.empty()?0:vec.front()) #define getBac(vec) (vec.empty()?0:vec.front()) #define debug(var) cerr << #var <<":"<<var<<"\n"; #define DEBUG(variable) \ do { \ std::cerr << #variable << ":"; \ for (const auto& elem : variable) { \ std::cerr << elem << " "; \ } \ std::cerr << "\n"; \ } while (0) #define uniVec(var) \ do { \ sort(var.begin(),var.end());\ var.resize(unique(var.begin(),var.end())-var.begin());\ } while (0)
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef __int128 i128; typedef pair<ll,ll> pll; typedef array<ll,2> arr; typedef double DB; typedef pair<DB,DB> pdd; typedef pair<ll,bool> plb; constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e18)+3; constexpr ll mod=static_cast<ll>(1e9)+7; constexpr double eps=1e-8;
ll N,M,K,T; ll ans=0; arr ab[MAXN]; ll diff[MAXN],Sum[MAXN];
inline void solve(){ cin>>N>>K; vector<ll> li,Vals,B; FOR(i,0,3*N+5){ Sum[i]=0; diff[i]=0; } FOR(i,1,N){ cin>>ab[i][0]; li.pb(ab[i][0]+1); li.pb(ab[i][0]); Vals.pb(ab[i][0]); } FOR(i,1,N){ cin>>ab[i][1]; li.pb(ab[i][1]); Vals.pb(ab[i][1]); B.pb(ab[i][1]); } sort(all(B)); uniVec(li); uniVec(Vals); li.pb(li.back()+1); FOR(i,1,N){ ll beg=lower_bound(all(li),ab[i][0]+1)-li.begin(), ed=lower_bound(all(li),ab[i][1])-li.begin(); diff[beg]+=1; diff[ed+1]-=1; } Sum[0]=diff[0]; FOR(i,1,3*N){ Sum[i]=Sum[i-1]+diff[i]; } #ifdef LOCAL cerr<<"Sum:"; FOR(i,0,2*N){ cerr<<Sum[i]<<" "; } cerr<<"\n"; DEBUG(li); DEBUG(B); #endif ans=0; FOR(i,0,SZ(Vals)-1){ ll val=Vals[i]; ll idx=lower_bound(all(li),val)-li.begin(); #ifdef LOCAL if(val==1){ debug(Sum[idx]); debug(K); } #endif if(Sum[idx]<=K){ ll ind=lower_bound(all(B),val)-B.begin(); ll num=N-ind; ans=max(num*val,ans); #ifdef LOCAL debug(ind); #endif } } cout<<ans<<"\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>T; while(T--){ solve(); } return 0; }
|