RE求调
查看原帖
RE求调
1256042
hyl2718281828楼主2024/10/3 15:04
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+1;
struct node {
    long long val;
    int o,l,r,r0;
    node (int po=0, int pl=0, int pr=0, int pv = 0, int pr0=0) {
        o = po; l = pl; r = pr; val = pv; r0 = pr0;
    }
    bool operator<(const node& a) const {return val < a.val;}
};
priority_queue <node> q;
int n,k,L,R,a[N],lg[N];
long long s[N],st[N][20],ans;
int query(int l,int r) {
    int t = lg[r-l+1];
    int x = st[l][t], y = st[r-(1<<t)+1][t];
    return s[x] > s[y] ? x : y;
}
int main() {
    scanf("%d%d%d%d",&n,&k,&L,&R);
    for (int i = 1; i <= n; i++) {
        scanf("%d",&a[i]);
        s[i] = s[i-1]+a[i];
        st[i][0] = i;
    }
    for (int i = 2; i <= n; i++) lg[i] = lg[i-1]+1;
    for (int j=1; j<=lg[n]; j++) for (int i=1; i+(1<<j)-1<=n; i++) {
        int x = st[i][j-1], y = st[i+(1<<(j-1))][j-1];
        st[i][j] = s[x] > s[y] ? x : y;
    }
    for (int i = 1; i+L-1 <= n; i++) {
        int l = i+L-1, r = min(i+R-1,n);
        q.push(node(i,l,r,s[r]-s[l-1],query(l,r)));
    }
    while (k--) {
        int o = q.top().o, l = q.top().l, r = q.top().r, x = q.top().r0;
        q.pop();
        ans += s[x]-s[o-1];
        if (l != x) q.push(node(o,l,x-1,s[x-1]-s[l-1],query(l,x-1)));
        if (x != r) q.push(node(o,x+1,r,s[r]-s[x],query(x+1,r)));
    }
    printf("%lld",ans);
    return 0;
}
2024/10/3 15:04
加载中...