https://github.com/OI-wiki/libs/blob/master/集训队历年论文/

国家集训队2005论文集

黄源河--左偏树的特点及其应用

LuoguP4331 [BOI2004]Sequence 数字序列

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

#endif

using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}

const int N = 1e6 + 7;

struct Node {
    int rt, l, r, siz;
    long long val;
} sta[N];
int top;
int ch[N][2], dis[N];

int a[N];

#define ls ch[x][0]
#define rs ch[x][1]
inline int Merge(int x, int y) {
    if(!x || !y) return x | y;
    if(a[x] < a[y]) Swap(x, y);
    rs = Merge(rs, y);
    if(dis[rs] > dis[ls]) Swap(ls, rs);
    dis[x] = dis[rs] + 1;
    return x;
}

inline int Del(int x) {
    return Merge(ls, rs);
}

int main() {
    int n;
    io >> n;
    dis[0] = -1;
    R(i,1,n){
        io >> a[i];
        a[i] -= i;
    }
    R(i,1,n){
        sta[++top] = (Node){ i, i, i, 1, a[i]};
        while(top != 1 && sta[top - 1].val > sta[top].val){
            --top;
            sta[top].rt = Merge(sta[top].rt, sta[top + 1].rt);
            sta[top].siz += sta[top + 1].siz;
            sta[top].r = sta[top + 1].r;
            while(sta[top].siz > (sta[top].r - sta[top].l + 2) / 2){
                --sta[top].siz;
                sta[top].rt = Del(sta[top].rt);
             }
             sta[top].val = a[sta[top].rt];
        }
    }
    int tot = 1;
    long long ans = 0;
    R(i,1,n){
        if(sta[tot].r < i) ++tot;
        ans += Abs(sta[tot].val - a[i]);
    }
    printf("%lld\n", ans);
    tot = 1;
    R(i,1,n){
        if(sta[tot].r < i) ++tot;
        printf("%lld ", sta[tot].val + i);
    }
    return 0;
}
01-16 13:14