救救孩子,啥都没过
查看原帖
救救孩子,啥都没过
236416
_stOrz_楼主2022/2/8 00:21
#include<bits/stdc++.h>
using namespace std;
char user;
template <typename T> inline void read(T &x){
    char ch = getchar(); T f = 1; x = 0;
    for(; (!isdigit(ch)) and ch != '-'; ch = getchar());
    if(ch == '-') f = -1, ch = getchar();
    for(; isdigit(ch); x = (x << 3) + (x << 1) + ch - 48, ch = getchar());
    x *= f;
}
template<typename T, typename ...Arg>void read(T &x, Arg& ...arg){
    read(x);
    read(arg...);
}
                                               
template <typename T>void write(T x){
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');    
}
                                                                  
template <typename T, typename ...Arg> void write(T x, Arg ...arg){
    write(x);
    putchar(user);
    write(arg...);
}
const int N = 5e5 + 5;
struct node{
    int l, r, x, id, start; 
    friend bool operator <(node x, node y){
        return x.x < y.x;
    }
};
int n, k, L, R, a[N], sum[N], lg[N], st[N][30], pos[N][30], ans;
priority_queue<node>q;
signed main(signed agrc, char const *argv[]){
    clock_t cl = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
//---------------------------------------------------------------
    read(n, k, L, R); lg[0] = -1;
    for(int i = 1; i <= n; i++) read(a[i]), sum[i] = sum[i - 1] + a[i], lg[i] = lg[i >> 1] + 1, pos[i][0] = i, st[i][0] = a[i];
    for(int j = 1; j <= lg[n]; j++)
        for(int i = 1; i <= n; i++)
            if(st[i][j - 1] >= st[i + (1 << j - 1)][j - 1]) st[i][j] = st[i][j - 1], pos[i][j] = pos[i][j - 1];
                else st[i][j] = st[i + (1 << j - 1)][j - 1], pos[i][j] = pos[i + (1 << j - 1)][j - 1];
                
    for(int i = 1; i <= n - L + 1; i++){
        int l = i + L - 1, r = i + R - 1, z = lg[R - L + 1];
        q.push((node){l, r, max(st[l][lg[R - L + 1]], st[r - (1 << lg[R - L + 1]) + 1][lg[R - L + 1]]), (st[l][z] > st[r - (1 << z) + 1][z] ? (pos[l][z]) : (pos[r - (1 << z) + 1][z])), i});
       // write(max(st[l][lg[R - L + 1]], st[r - (1 << lg[R - L + 1]) + 1][lg[R - L + 1]]), (st[l][z] > st[r - (1 << z) + 1][z] ? (pos[l][z]) : (pos[r - (1 << z) + 1][z])));puts("");
       // user = ' ';
        //write(l, r);
    }
    
    for(int i = 1; i <= k; i++){
        node x = q.top(); q.pop();
        int l = x.l, r = x.id, z;
        ans += x.x - sum[x.start - 1];
        
        r--;if(l > r) continue; z = lg[r - l + 1], q.push((node){l, r, max(st[l][z], st[r - (1 << z) + 1][z]), (st[l][z] > st[r - (1 << z) + 1][z] ? (pos[l][z]) : (pos[r - (1 << z) + 1][z])), x.start});
        user = '-';
        l = x.id + 1, r = x.r;if(l > r) continue; z = lg[r - l + 1], write(l, r, r - l + 1);q.push((node){l, r, max(st[l][z], st[r - (1 << z) + 1][z]), ((st[l][z] > st[r - (1 << z) + 1][z]) ? (pos[l][z]) : (pos[r - (1 << z) + 1][z])), x.start});
    }
    
    write(ans);
//---------------------------------------------------------------
end:
    cerr << "Time Used: " << clock() - cl << "ms" << endl;
    return 0;
}

2022/2/8 00:21
加载中...