社区讨论

求dalao找错。。实在找不出来了。。 8WA 2TLE

P2042[NOI2005] 维护数列参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mi6mi3ai
此快照首次捕获于
2025/11/20 07:18
4 个月前
此快照最后确认于
2025/11/20 07:18
4 个月前
查看原帖
int max3(int x, int y, int z) { return (x>y) ? ((x>z) ? x : z) : ((y>z) ? y : z); }
CPP
struct Splay_Tree
{
    int rt, tot;
    int fa[MAXN], son[MAXN][2], key[MAXN], size[MAXN];
    int sum[MAXN], optr[MAXN], optc[MAXN], lmax[MAXN], rmax[MAXN], nowmax[MAXN];
    vector <int> recycle;
    inline void clear()
    {
        rt = 0; tot = 0;
        memset(fa, 0, sizeof(fa));
        memset(son, 0, sizeof(son));
        memset(key, 0, sizeof(key));
        memset(sum, 0, sizeof(sum));
        memset(size, 0, sizeof(size));
        memset(optr, 0, sizeof(optr));
        memset(lmax, 0, sizeof(lmax));
        memset(rmax, 0, sizeof(rmax));
        memset(nowmax, 0, sizeof(nowmax));
        for(int i=0; i<MAXN; i++) optc[i] = INF;
    }
    inline void clear_node(int o)
    {
        recycle.push_back(o);
        fa[o] = son[o][0] = son[o][1] = size[o] = key[o] = 0;
        sum[o] = optr[o] = lmax[o] = rmax[o] = nowmax[o] = 0;
        optc[o] = INF; 
    }
    inline int get(int o) { return son[fa[o]][1] == o; }
    inline int get_address()
    {
        if(recycle.size() == 0) return ++tot;
        int tmp = recycle[recycle.size() -1];
        recycle.pop_back();
        return tmp;
    }
    inline void pushup(int o)
    {
         size[o] = size[son[o][0]] + size[son[o][1]] + 1;
         sum[o] = sum[son[o][0]] + sum[son[o][1]] + key[o];
         nowmax[o] = max3(nowmax[son[o][0]], nowmax[son[o][1]], rmax[son[o][0]] + key[o] + lmax[son[o][1]]);
         lmax[o] = max(lmax[son[o][0]], sum[son[o][0]] + key[o] + lmax[son[o][1]]);
         rmax[o] = max(rmax[son[o][1]], sum[son[o][1]] + key[o] + rmax[son[o][0]]);
    }
    inline void pushdown(int o)
    {
        if(optr[o] == 0 && optc[o] == INF) return ;
        if(optr[o] != 0) 
        {
            swap(son[o][0], son[o][1]);
            optr[son[o][0]] ^= 1;
            optr[son[o][1]] ^= 1;
            optr[o] = 0;
        }
        if(optc[o] != INF)
        {
            key[son[o][0]] = optc[o];
            key[son[o][1]] = optc[o];
            optc[son[o][0]] = optc[o];
            optc[son[o][1]] = optc[o];
            sum[son[o][0]] = optc[o] * size[son[o][0]];
            sum[son[o][1]] = optc[o] * size[son[o][1]];
            lmax[son[o][0]] = rmax[son[o][0]] = nowmax[son[o][0]] = (optc[o] > 0) ? optc[o] * size[son[o][0]] : 0;
            lmax[son[o][1]] = rmax[son[o][1]] = nowmax[son[o][1]] = (optc[o] > 0) ? optc[o] * size[son[o][1]] : 0;
            optc[o] = INF;
        }
        pushup(o);
    }    
    inline int get_loc(int l, int r)
    {
        splay(select(l), 0);
        int tmppre = pre();
        splay(select(r), 0);
        int tmpsuf = suf();
        if(tmpsuf == -1 && tmppre == -1) return rt;
        splay((tmppre == -1) ? tmpsuf : tmppre, 0);
        if(tmppre != -1 && tmpsuf != -1) 
        {
            splay(tmpsuf, rt);
            return son[son[rt][1]][0];
        }
        return son[rt][tmpsuf == -1];
    }
    inline void rorate(int o)
    {
        int p = fa[o], g = fa[p], which = get(o);
        son[p][which] = son[o][which^1]; fa[son[p][which]] = p;
        son[o][which^1] = p; fa[p] = o;
        fa[o] = g;
        if(g != 0) son[g][son[g][1] == p] = o;
        pushup(p); pushup(o);
    }
    inline void splay(int o, int to)
    {
        for(int p = fa[o]; p != to && p > 0; rorate(o), p = fa[o])
            if(fa[p] != to && fa[p] > 0) rorate((get(o) == get(p)) ? p : o);
        if(to == 0) rt = o;
    }
    inline void build(int &o, int l, int r, int p)
    {
        if(l == r)
        {
            o = get_address();
            key[o] = a[l]; size[o] = 1; fa[o] = p;
            lmax[o] = rmax[o] = nowmax[o] = (key[o] > 0) ? key[o] : 0;
            pushup(o);
            return ;
        }
        if(l +1 == r)
        {
            o = get_address(); son[o][1] = get_address();
            key[o] = a[l]; size[o] = 2; fa[o] = p;
            key[son[o][1]] = a[l+1]; size[son[o][1]] = 1; fa[son[o][1]] = o;
            lmax[son[o][1]] = rmax[son[o][1]] = nowmax[son[o][1]] = (key[son[o][1]] > 0) ? key[son[o][1]] : 0;
            pushup(son[o][1]); pushup(o);
            return ; 
        }
        int mid = (l+r) >>1;
        o = get_address();
        key[o] = a[mid]; size[o] = 1; fa[o] = p;
        build(son[o][0], l, mid-1, o);
        build(son[o][1], mid+1, r, o);
        pushup(o);
}

回复

2 条回复,欢迎继续交流。

正在加载回复...