LOJ刷题记录:2018-2020

LOJ刷题记录:2018-2020


loj#2018. 「HNOI2017」单旋

神题…

手玩一下就会发现单旋到顶在中间是不会改变父子关系的…所以用LCT统计答案,再用个数组 lc,rc,fa 手动维护父子关系即可。

貌似有线段树+set做法…?

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

int chl[MAXN][2], fa[MAXN], rev[MAXN], siz[MAXN];
int stk[MAXN], stk_top;
inline bool is_rt(int x)
{ return chl[fa[x]][0] != x && chl[fa[x]][1] != x; }
inline void update(int nd)
{
    if (!nd) return;
    siz[nd] = siz[chl[nd][0]]+siz[chl[nd][1]]+1;
}
inline void pdw(int nd)
{
    if (!rev[nd]) return;
    int &lc = chl[nd][0], &rc = chl[nd][1];
    if (lc) rev[lc] ^= 1;
    if (rc) rev[rc] ^= 1;
    rev[nd] = 0, swap(lc, rc);
}

void zig(int nd)
{
    int p = fa[nd], g = fa[p], tp = chl[p][0] != nd, tg = chl[g][0] != p;
    int son = chl[nd][tp^1];
    if (!is_rt(p)) chl[g][tg] = nd;
    if (son) fa[son] = p;
    fa[p] = nd, fa[nd] = g, chl[nd][tp^1] = p, chl[p][tp] = son;
    update(p), update(nd);
}

void splay(int nd)
{
    stk[stk_top = 1] = nd;
    for (int i = nd; !is_rt(i); i = fa[i]) stk[++stk_top] = fa[i];
    while (stk_top) pdw(stk[stk_top--]);
    while (!is_rt(nd)) {
        int p = fa[nd], g = fa[p], tp = chl[p][0] != nd, tg = chl[g][0] != p;
        if (is_rt(p)) { zig(nd); break; }
        else if (tp == tg) zig(p), zig(nd);
        else zig(nd), zig(nd);
    }
}

void access(int x)
{
    for (int y = 0; x; x = fa[y = x])
        splay(x), chl[x][1] = y, update(x);
}

void make_rt(int x)
{ access(x), splay(x), rev[x] ^= 1; }

void link(int x, int y)
{ make_rt(x), fa[x] = y; }

set<pair<int,int> > st;
int m, c;
int top = 0, root = 0;

int father(int x)
{
    make_rt(root), access(x), splay(x);
    int nd = chl[x][0];
    while (chl[nd][1]) nd = chl[nd][1];
    return nd;
}

void cut(int x, int y)
{
    make_rt(x), access(y), splay(y), chl[y][0] = fa[x] = 0;
}

int lc[MAXN], rc[MAXN], f[MAXN];

int main()
{
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &c);
        int ans = 0;
        if (c == 1) {
            int key; scanf("%d", &key);
            if (!root) root = ++top, st.insert(make_pair(key, top));
            else {
                int up = st.upper_bound(make_pair(key, top+1))->second;
                int lb = (--(st.lower_bound(make_pair(key, top+1))))->second;
                if (lb && !rc[lb]) { link(++top, lb), st.insert(make_pair(key, top)), rc[lb] = top, f[top] = lb; }
                else  { link(++top, up), st.insert(make_pair(key, top)), lc[up] = top, f[top] = up; }
            }
            make_rt(root), access(top), splay(top), ans += siz[chl[top][0]]+1;
        } else if (c == 2) {
            int xmin = st.begin()->second, son = rc[xmin];
            if (xmin == root) ans++;
            else {
                make_rt(root), access(xmin), splay(xmin), ans += siz[chl[xmin][0]]+1;
                if (son) cut(son, xmin), link(son, f[xmin]);
                cut(xmin, f[xmin]), link(root, xmin);
                if (son) f[son] = f[xmin];
                lc[f[xmin]] = son, f[root] = xmin, rc[xmin] = root, root = xmin;
            }
        } else if (c == 3) {
            int xmax = (--st.end())->second, son = lc[xmax];
            if (xmax == root) ans++;
            else {
                make_rt(root), access(xmax), splay(xmax), ans += siz[chl[xmax][0]]+1;
                if (son) cut(son, xmax), link(son, f[xmax]);
                cut(xmax, f[xmax]), link(root, xmax);
                if (son) f[son] = f[xmax];
                rc[f[xmax]] = son, f[root] = xmax, lc[xmax] = root, root = xmax;
            }
        } else if (c == 4) {
            int xmin = st.begin()->second, son = rc[xmin];
            if (xmin == root) {
                cut(xmin, son), f[son] = 0, root = son, ans++;
            } else {
                make_rt(root), access(xmin), splay(xmin), ans += siz[chl[xmin][0]]+1;
                if (son) cut(son, xmin), link(son, f[xmin]);
                cut(xmin, f[xmin]);
                if (son) f[son] = f[xmin];
                lc[f[xmin]] = son;
            }
            st.erase(*st.begin());
        } else {
            int xmax = (--st.end())->second, son = lc[xmax];
            if (xmax == root) {
                cut(xmax, son), f[son] = 0, root = son, ans++;
            } else {
                make_rt(root), access(xmax), splay(xmax), ans += siz[chl[xmax][0]]+1;
                if (son) cut(son, xmax), link(son, f[xmax]);
                cut(xmax, f[xmax]);
                if (son) f[son] = f[xmax];
                rc[f[xmax]] = son;
            }
            st.erase(*(--st.end()));
        }
        printf("%d\n", ans);
    }
    return 0;
}

loj#2019. 「HNOI2017」影魔

对于每一个位置计算贡献,可以转换成一行区间加,矩形求和的二位数点…

然而树套树会被卡…和暴力一个分

树套树(TLE30)

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
int n, m, p1, p2;
int k[MAXN];
int lpos[MAXN], rpos[MAXN];
int stk[MAXN], stk_top = 0;

int l[MAXN*80
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值