제출 #1132262

#제출 시각아이디문제언어결과실행 시간메모리
1132262steveonalexCake 3 (JOI19_cake3)C++20
100 / 100
495 ms10328 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }


struct FenwickTree{
    int n;
    vector<ll> a;

    FenwickTree(int _n){
        n = _n;
        a.resize(n+1);
    }

    void update(int i, ll v){
        while(i <= n){
            a[i] += v;
            i += LASTBIT(i);
        }
    }

    ll get(int i){
        ll ans = 0;
        while(i > 0){
            ans += a[i];
            i -= LASTBIT(i);
        }
        return ans;
    }

    ll get(int l, int r){return get(r) - get(l-1);}

    int binary_lift(ll x){
        int p = MASK(logOf(n)), idx =0;
        while(p > 0){
            if (idx + p <= n && x > a[idx + p]){
                idx += p;
                x -= a[idx];
            }
            p >>= 1;
        }
        return idx + 1;
    }
};

const int N = 2e5 + 69;
const ll INF = 1e18 + 69;
int n, m;
FenwickTree bit(N), sum(N);
int L = 1, R = 0;
ll cost[N];
vector<pair<int, int>> val;
vector<pair<int, int>> a;

void update_idx(int i, int mul){
    i = a[i].first;
    bit.update(i, mul);
    sum.update(i, val[i].first * mul);
}

ll calc_cost(int l, int r){
    while(R < r)
        update_idx(++R, 1);
    while(L > l)
        update_idx(--L, 1);
    while(R > r) 
        update_idx(R--, -1);
    while(L < l)
        update_idx(L++, -1);

    return sum.get(bit.binary_lift(m)) - (a[r].second - a[l].second) * 2;
}

void dnc(int l, int r, int optL, int optR){
    if (l > r) return;
    int mid = (l + r) >> 1;
    int opt = -1;
    if (R <= max(optL, mid + m - 1)) {
        for(int i = max(optL, mid + m - 1); i <= optR; ++i){
            if (maximize(cost[mid], calc_cost(mid, i)))
                opt = i;
        }
    }
    else{
        for(int i = optR; i >= max(optL, mid + m - 1); --i){
            if (maximize(cost[mid], calc_cost(mid, i)))
                opt = i;
        }

    }
    dnc(l, mid - 1, optL, opt);
    dnc(mid + 1, r, opt, optR);
}

void solve(){
    cin >> n >> m;
    a.resize(n);
    for(int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
    a.push_back({-1, -1});
    sort(ALL(a), [](pair<int, int> x, pair<int, int> y){return x.second < y.second;});

    val.push_back({1e9 + 69, 0});
    for(int i = 1; i <= n; ++i) val.push_back({a[i].first, i});
    sort(ALL(val), greater<pair<int, int>>());

    for(int i = 1; i <= n; ++i) a[val[i].second].first = i;

    for(int i = 1; i<= n; ++i) cost[i] = -INF;

    dnc(1, n - m + 1, m, n);
    ll ans = -INF;
    for(int i = 1; i <= n - m + 1; ++i) maximize(ans, cost[i]);

    cout << ans << "\n";
}

int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);

    clock_t start = clock();

    solve();

    cerr << "Time elapsed: " << clock() - start << " ms\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...