//#define _GLIBCXX_DEBUG //#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include using namespace std; #ifdef LOCAL #include #define OUT(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define OUT(...) (static_cast(0)) #endif #define endl '\n' #define lfs cout<= (ll)(n); i--) namespace template_tute{ using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; template using PQ = priority_queue; template using QP = priority_queue,greater>; templatebool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} templatebool chmax(T1 &a,T2 b){if(a({a,b,c})-min({a,b,c});} void ans1(bool x){if(x) cout<<"Yes"<void ans(bool x,T1 y,T2 z){if(x)cout<void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);}; templatevoid debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;ivoid debug(const T &v,ll n,string sv=" "){if(n!=0)cout<void debug(const vector&v){debug(v,v.size());} templatevoid debug(const vector>&v){for(auto &vv:v)debug(vv,vv.size());} templatevoid debug(stack st){while(!st.empty()){cout<void debug(queue st){while(!st.empty()){cout<void debug(deque st){while(!st.empty()){cout<void debug(PQ st){while(!st.empty()){cout<void debug(QP st){while(!st.empty()){cout<void debug(const set&v){for(auto z:v)cout<void debug(const multiset&v){for(auto z:v)cout<void debug(const array &a){for(auto z:a)cout<void debug(const map&v){for(auto z:v)cout<<"["<vector>vec(ll x, ll y, T w){vector>v(x,vector(y,w));return v;} vectordx={1,-1,0,0,1,1,-1,-1};vectordy={0,0,1,-1,1,-1,1,-1}; templatevector make_v(size_t a,T b){return vector(a,b);} templateauto make_v(size_t a,Ts... ts){return vector(a,make_v(ts...));} templateostream &operator<<(ostream &os, const pair&p){return os << "(" << p.first << "," << p.second << ")";} templateostream &operator<<(ostream &os, const vector &v){os<<"[";for(auto &z:v)os << z << ",";os<<"]"; return os;} templatevoid rearrange(vector&ord, vector&v){ auto tmp = v; for(int i=0;ivoid rearrange(vector&ord,Head&& head, Tail&&... tail){ rearrange(ord, head); rearrange(ord, tail...); } template vector ascend(const vector&v){ vectorord(v.size());iota(ord.begin(),ord.end(),0); sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i) vector descend(const vector&v){ vectorord(v.size());iota(ord.begin(),ord.end(),0); sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);}); return ord; } template vector inv_perm(const vector&ord){ vectorinv(ord.size()); for(int i=0;i0);return n>=0?n/div:(n-div+1)/div;} ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;} ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;} ll modulo(ll n,ll d){return (n%d+d)%d;}; templateT min(const vector&v){return *min_element(v.begin(),v.end());} templateT max(const vector&v){return *max_element(v.begin(),v.end());} templateT acc(const vector&v){return accumulate(v.begin(),v.end(),T(0));}; templateT reverse(const T &v){return T(v.rbegin(),v.rend());}; //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); int popcount(ll x){return __builtin_popcountll(x);}; int poplow(ll x){return __builtin_ctzll(x);}; int pophigh(ll x){return 63 - __builtin_clzll(x);}; templateT poll(queue &q){auto ret=q.front();q.pop();return ret;}; templateT poll(priority_queue &q){auto ret=q.top();q.pop();return ret;}; templateT poll(QP &q){auto ret=q.top();q.pop();return ret;}; templateT poll(stack &s){auto ret=s.top();s.pop();return ret;}; ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;} ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;} ll POW(ll x, ll k){ll ret=1;for(int i=0;isputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } namespace converter{ int dict[500]; const string lower="abcdefghijklmnopqrstuvwxyz"; const string upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string digit="0123456789"; const string digit1="123456789"; void regi_str(const string &t){ for(int i=0;ito_int(const string &s,const string &t){ regi_str(t); vectorret(s.size()); for(int i=0;ito_int(const string &s){ auto t=s; sort(t.begin(),t.end()); t.erase(unique(t.begin(),t.end()),t.end()); return to_int(s,t); } vector>to_int(const vector&s,const string &t){ regi_str(t); vector>ret(s.size(),vector(s[0].size())); for(int i=0;i>to_int(const vector&s){ string t; for(int i=0;i&s,const string &t){ regi_int(t); string ret; for(auto z:s)ret+=dict[z]; return ret; } vector to_str(const vector>&s,const string &t){ regi_int(t); vectorret(s.size()); for(int i=0;i struct edge { int to; T cost; int id; edge():to(-1),id(-1){}; edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){} operator int() const { return to; } }; template using Graph = vector>>; template Graphrevgraph(const Graph &g){ Graphret(g.size()); for(int i=0;i Graph readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){ Graph ret(n); for(int es = 0; es < m; es++){ int u,v; T w=1; cin>>u>>v;u-=indexed,v-=indexed; if(weighted)cin>>w; ret[u].emplace_back(v,w,es); if(!directed)ret[v].emplace_back(u,w,es); } return ret; } template Graph readParent(int n,int indexed=1,bool directed=true){ Graphret(n); for(int i=1;i>p; p-=indexed; ret[p].emplace_back(i); if(!directed)ret[i].emplace_back(p); } return ret; } } using namespace template_tute; namespace GeneralSlopeTrick{ using I = ll; const I minf = -1.5e9; //無限小の座標 const I max_identity = minf * 2; //座標の単位元 // minf での値はオーバーフローしても問題ない(はず) //base: https://nyaannyaan.github.io/library/rbst/lazy-reversible-rbst.hpp template struct RBSTBase { //using Ptr = Node *; // template // inline Ptr my_new(Args... args) { // return new Node(args...); // } // inline void my_del(Ptr t) { delete t; } // inline Ptr make_tree() const { return nullptr; } // for avoiding memory leak, activate below using Ptr = shared_ptr; template inline Ptr my_new(Args... args) { return make_shared(args...); } inline void my_del(Ptr t) {} Ptr make_tree() {return Ptr();} int size(Ptr t) const { return count(t); } using Key = decltype(Node::key); //checkを満たす境界位置 0~size template int find_first(Ptr t, check &C) { int ret = 0; Key now; if (!C(sum(t)))return count(t); while(1){ if(!t)return ret; push(t); if(t->l)push(t->l); if(C(f(now, sum(t->l)))){ t = t->l; } else{ now = f(now, sum(t->l)); ret += count(t->l); now = f(now, t->key); if(C(now))return ret; ret++; t = t->r; } } } Ptr merge(Ptr l, Ptr r) { if (!l || !r) return l ? l : r; if (int((rng() * (l->cnt + r->cnt)) >> 32) < l->cnt) { push(l); l->r = merge(l->r, r); return update(l); } else { push(r); r->l = merge(l, r->l); return update(r); } } pair split(Ptr t, int k) { if (!t) return {nullptr, nullptr}; push(t); if (k <= count(t->l)) { auto s = split(t->l, k); t->l = s.second; return {s.first, update(t)}; } else { auto s = split(t->r, k - count(t->l) - 1); t->r = s.first; return {update(t), s.second}; } } Ptr build(int l, int r, const vector &v) { if (l + 1 == r) return my_new(v[l]); int m = (l + r) >> 1; Ptr pm = my_new(v[m]); if (l < m) pm->l = build(l, m, v); if (m + 1 < r) pm->r = build(m + 1, r, v); return update(pm); } Ptr build(const vector &v) { return build(0, (int)v.size(), v); } template void insert(Ptr &t, int k, const Args &... args) { auto x = split(t, k); t = merge(merge(x.first, my_new(args...)), x.second); } template void push_back(Ptr &t, const Args &... args) { t = merge(t, my_new(args...)); } template void push_front(Ptr &t, const Args &... args) { t = merge(my_new(args...), t); } void erase(Ptr &t, int k) { auto x = split(t, k); auto y = split(x.second, 1); my_del(y.first); t = merge(x.first, y.second); } inline int count(const Ptr t) const { return t ? t->cnt : 0; } inline Key sum(const Ptr t) const { return t ? t->sum : Key(); } //protected: static uint64_t rng() { static uint64_t x_ = 88172645463325252ULL; return x_ ^= x_ << 7, x_ ^= x_ >> 9, x_ & 0xFFFFFFFFull; } virtual void push(Ptr) = 0; virtual Ptr update(Ptr) = 0; }; template struct LazyRBSTNode { typename RBSTBase::Ptr l, r; T key, sum; E lazy; int cnt; LazyRBSTNode(const T &t = T(), const E &e = E()) : l(), r(), key(t), sum(t), lazy(e), cnt(1){} }; template struct LazyRBST : RBSTBase> { using Node = LazyRBSTNode; using base = RBSTBase>; using base::merge; using base::split; using typename base::Ptr; LazyRBST() = default; T fold(Ptr &t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void apply(Ptr &t, int a, int b, const E &e) { auto x = split(t, a); auto y = split(x.second, b - a); propagate(y.first, e); t = merge(x.first, merge(y.first, y.second)); } inline T sum(const Ptr t) const { return t ? t->sum : T(); } //protected: Ptr update(Ptr t) override { push(t); t->cnt = 1; t->sum = t->key; if (t->l) t->cnt += t->l->cnt, t->sum = f(t->l->sum, t->sum); if (t->r) t->cnt += t->r->cnt, t->sum = f(t->sum, t->r->sum); return t; } void push(Ptr t) override { if (t->lazy != E()) { if (t->l) propagate(t->l, t->lazy); if (t->r) propagate(t->r, t->lazy); t->lazy = E(); } } void propagate(Ptr t, const E &x) { t->lazy = h(t->lazy, x); t->key = g(t->key, x); t->sum = g(t->sum, x); } }; struct T{ I pos_mx, grad, sum; T():pos_mx(max_identity), grad(0), sum(0){} T(I pos_mx, I grad, I sum): pos_mx(pos_mx), grad(grad), sum(sum){} }; using E = I; T f(T a,T b){ return T(max(a.pos_mx, b.pos_mx), a.grad + b.grad, a.sum + b.sum); } T g(T a,E b){ return T(a.pos_mx + b, a.grad, a.sum + a.grad * b); } E h(E a,E b){ return a + b; } struct SlopeTrick{ LazyRBSTbbst; using Ptr = LazyRBST::Ptr; Ptr root; I minf_val, minf_grad; SlopeTrick(){ root = bbst.build(vector({T(0, 0, 0)})); minf_val = 0; minf_grad = 0; } void insert(I pos,I val){ auto check = [&](T s){ return s.pos_mx > pos; }; int idx = bbst.find_first(root, check); bbst.insert(root, idx, T(pos, val, pos * val)); } void add_all(I a){ minf_val += a; } // add c(x-a)_+ _____/ void add_xma(I a, I c = 1){ insert(a, c); } // add c(a-x)_+ \_____ void add_amx(I a, I c = 1) { minf_grad -= c; minf_val += c * (a - minf); insert(a, c); } // add |x-a| \____/ void add_abs(I a, I c = 1) { add_xma(a, c), add_amx(a, c); } void add_abs_fast(I a, I c = 1) { minf_grad -= c; minf_val += c * (a - minf); insert(a, 2*c); } pairzero_split(){ auto check = [&](T s){ return s.grad + minf_grad >= 0; }; int idx = bbst.find_first(root, check); auto [l, r] = bbst.split(root, idx + 1); return make_pair(l, r); } tuplezero_split3(){ auto check = [&](T s){ return s.grad + minf_grad >= 0; }; int idx = bbst.find_first(root, check); auto [l, r] = bbst.split(root, idx); auto [rl, rr] = bbst.split(r, 1); return make_tuple(l, rl, rr); } // chmin right side \_/ -> \__ void chmin_right() { auto [l, m, r] = zero_split3(); auto lsum = bbst.sum(l); m->key.grad = -minf_grad - lsum.grad; m->key.sum = (-minf_grad - lsum.grad) * m->key.pos_mx; m = bbst.update(m); root = bbst.merge(l, m); } // chmin left side \_/ -> __/ void chmin_left() { auto [l, m, r] = zero_split3(); auto lsum = bbst.sum(l); minf_val += (minf_grad + lsum.grad) * m->key.pos_mx - (minf_grad * minf + lsum.sum); m->key.grad += minf_grad + lsum.grad; m->key.sum = m->key.grad * m->key.pos_mx; minf_grad = 0; m = bbst.update(m); root = bbst.merge(m, r); } // move left with cost c // limit が -1 なら無限、そうでなければ limit だけ左に移動できる // 最小値の位置が無限遠になるときはすべて move系/add系で処理する(他は壊れがち) void move_left(I c, I limit = -1){ assert(limit >= -1); if(minf_grad >= -c)return; auto root_grad = bbst.sum(root).grad; if(minf_grad + root_grad < -c){ assert(limit != -1); // 無限に小さくなる bbst.propagate(root, -limit); minf_val += (minf_grad + c) * limit; return; } auto check = [&](T s){ return s.grad >= -c - minf_grad; }; int idx = bbst.find_first(root, check); auto [l, r] = bbst.split(root, idx); auto [rl, rr] = bbst.split(r, 1); auto lsum=bbst.sum(l); I dec = -c - (lsum.grad + minf_grad); rl->key.grad -= dec; rl->key.sum = rl->key.grad * rl->key.pos_mx; rl = bbst.update(rl); if(limit == -1){ minf_grad = -c; minf_val -= lsum.sum - minf * lsum.grad; minf_val -= (rl->key.pos_mx - minf) * dec; } else{ minf_val -= (-c - minf_grad) * limit; bbst.push_back(l, T(rl->key.pos_mx, dec, dec * rl->key.pos_mx)); bbst.propagate(l, -limit); rl = bbst.merge(l, rl); } root = bbst.merge(rl, rr); } // move right with cost c // limit が -1 なら無限、そうでなければ limit だけ右に移動できる void move_right(I c, I limit = -1){ assert(limit >= -1); if(minf_grad + root->sum.grad <= c)return; if(minf_grad > c){ assert(limit != -1); // 無限に小さくなる bbst.propagate(root, limit); minf_val -= (minf_grad - c) * limit; return; } auto check = [&](T s){ return s.grad + minf_grad >= c; }; int idx = bbst.find_first(root, check); auto [l, r] = bbst.split(root, idx); auto [rl, rr] = bbst.split(r, 1); auto rrsum=bbst.sum(rr); I dec = (minf_grad + bbst.sum(l).grad + rl->key.grad) - c; rl->key.grad -= dec; rl->key.sum = rl->key.grad * rl->key.pos_mx; rl = bbst.update(rl); if(limit != -1){ bbst.push_front(rr, T(rl->key.pos_mx, dec, dec * rl->key.pos_mx)); bbst.propagate(rr, limit); rl = bbst.merge(rl, rr); } root = bbst.merge(l, rl); } void shift(I k){ bbst.propagate(root, k); minf_val -= k * minf_grad; } I get_min(){ auto [l, r] = zero_split(); auto lsum = bbst.sum(l); I ret = minf_val + (minf_grad + lsum.grad) * lsum.pos_mx - (minf_grad * minf + lsum.sum); root = bbst.merge(l, r); return ret; } I calc(I pos){ auto check = [&](T s){ return s.pos_mx > pos; }; int idx = bbst.find_first(root, check); auto [l, r] = bbst.split(root, idx); auto lsum = bbst.sum(l); I ret = minf_val + (minf_grad + lsum.grad) * pos - (minf_grad * minf + lsum.sum); root = bbst.merge(l, r); return ret; } void slide_min(I a,I b){ assert(a <= b); shift(a); auto [l, m, r] = zero_split3(); auto lsum = bbst.sum(l); I nxt_grad = m->key.grad - (-minf_grad - lsum.grad); m->key.grad -= nxt_grad; m->key.sum = m->key.grad * m->key.pos_mx; m = bbst.update(m); bbst.insert(m, 1, T(m->key.pos_mx + b - a, nxt_grad, (m->key.pos_mx + b - a) * nxt_grad)); if(r)bbst.propagate(r, b - a); root = bbst.merge(m, r);root = bbst.merge(l, root); } void print_grad(){ for(int i=0;i>m>>n; vectora(m); rep(i,0,m)cin>>a[i]; vectorb(n); rep(i,0,n)cin>>b[i]; vector

p; rep(i,0,m)p.EB(a[i],0); rep(i,0,n)p.EB(b[i],1); sort(ALL(p)); rep(k,1,m+1){ GeneralSlopeTrick::SlopeTrick st; st.add_abs_fast(0,1e18); ll cnt=0; rep(i,0,p.size()){ if(p[i].se==0){ cnt++; } else{ st.move_right(0,k); } if(i+1>T; while(T--){ solve(); } return 0; }