SPOJ QTREE5 lct裸题

题目链接

对于每个节点,记录这个节点所在链的信息:

ls:(链的上端点)距离链内部最近的白点距离

rs:(链的下端点)距离链内部最近的白点距离

注意以上都是实边

虚边的信息用一个set维护。

set维护的是对于每个不是链上,但是this的子树,那些子树中距离this最近的白点距离。

#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != ‘-‘ && (c<‘0‘ || c>‘9‘)) c = getchar();
    sgn = (c == ‘-‘) ? -1 : 1;
    ret = (c == ‘-‘) ? 0 : (c - ‘0‘);
    while (c = getchar(), c >= ‘0‘&&c <= ‘9‘) ret = ret * 10 + (c - ‘0‘);
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar(‘-‘);
        x = -x;
    }
    if (x>9) pt(x / 10);
    putchar(x % 10 + ‘0‘);
}
using namespace std;
typedef long long ll;
const int inf = 1e9;
const int N = 1e5 + 10;
struct Node *null;
struct Node{
    multiset<int>chain;
    Node *fa, *ch[2];
    int size;
    int col, ls, rs, id, len, edge;
    //黑色col=-inf, 白色col=0 len是链长
    bool rev;
    inline void put(){
        printf("%d son:%d,%d fa:%d len:%d (%d,%d) col:%d\n", id, ch[0]->id, ch[1]->id, fa->id, len, ls, rs, col);
        for (auto i : chain)printf("%d ", i); puts("");
    }
    inline void clear(int _id){
        fa = ch[0] = ch[1] = null;
        size = 1;
        rev = 0;
        len = edge = 0;
        id = _id;
        chain.clear(); chain.insert(inf);
        col = inf;
        ls = rs = inf;
    }
    inline void push_up(){
        size = 1 + ch[0]->size + ch[1]->size;
        len = edge + ch[0]->len + ch[1]->len;
        int m0 = min(col, *chain.begin()), ml = min(m0, ch[0]->rs + edge), mr = min(m0, ch[1]->ls);
        ls = min(ch[0]->ls, ch[0]->len + edge + mr);
        rs = min(ch[1]->rs, ch[1]->len + ml);
    }
    inline void push_down(){
        if (rev){
            ch[0]->flip();
            ch[1]->flip();
            rev = 0;
        }
    }
    inline void setc(Node *p, int d){
        ch[d] = p;
        p->fa = this;
    }
    inline bool d(){
        return fa->ch[1] == this;
    }
    inline bool isroot(){
        return fa == null || fa->ch[0] != this && fa->ch[1] != this;
    }
    inline void flip(){
        if (this == null)return;
        swap(ch[0], ch[1]);
        rev ^= 1;
    }
    inline void go(){//从链头开始更新到this
        if (!isroot())fa->go();
        push_down();
    }
    inline void rot(){
        Node *f = fa, *ff = fa->fa;
        int c = d(), cc = fa->d();
        f->setc(ch[!c], c);
        this->setc(f, !c);
        if (ff->ch[cc] == f)ff->setc(this, cc);
        else this->fa = ff;
        f->push_up();
    }
    inline Node*splay(){
        go();
        while (!isroot()){
            if (!fa->isroot())
                d() == fa->d() ? fa->rot() : rot();
            rot();
        }
        push_up();
        return this;
    }
    void debug(Node *x){
        if (x == null)return;
        x->put();
        debug(x->ch[0]);
        debug(x->ch[1]);
    }
    inline Node* access(){//access后this就是到根的一条splay,并且this已经是这个splay的根了
        for (Node *p = this, *q = null; p != null; q = p, p = p->fa){
            p->splay();
    //      debug(q); puts("");     debug(p); puts("");
            if (p->ch[1] != null)
                p->chain.insert(p->ch[1]->ls);
            if (q != null)
                p->chain.erase(p->chain.find( q->ls ));
            p->setc(q, 1);
            p->push_up();
    //      debug(q); puts("");     debug(p); puts("");
        }
        return splay();
    }
    inline Node* find_root(){
        Node *x;
        for (x = access(); x->push_down(), x->ch[0] != null; x = x->ch[0]);
        return x;
    }
    void make_root(){
        access()->flip();
    }
    void cut(){//把这个点的子树脱离出去
        access();
        ch[0]->fa = null;
        ch[0] = null;
        push_up();
    }
    void cut(Node *x){
        if (this == x || find_root() != x->find_root())return;
        else {
            x->make_root();
            cut();
        }
    }
    void link(Node *x){
        if (find_root() == x->find_root())return;
        else {
            make_root(); fa = x;
        }
    }
};
Node pool[N], *tail;
Node *node[N];

vector<int>G[N];
void dfs(int u, int fa){
    for (int v : G[u]){
        if (v == fa)continue;
        node[v]->fa = node[u];
        node[v]->edge = 1;
        dfs(v, u);
        node[u]->chain.insert(node[v]->ls);
    }
    node[u]->push_up();
}
int n, q;
void debug(Node *x){
    if (x == null)return;
    x->put();
    debug(x->ch[0]);
    debug(x->ch[1]);
}
int main(){
    while (cin >> n){
        tail = pool;
        null = tail++;
        null->clear(0);
        null->size = 0;
        for (int i = 1; i <= n; i++)
        {
            G[i].clear();
            node[i] = tail++;
            node[i]->clear(i);
        }
        for (int i = 1, u, v; i < n; i++){
            rd(u); rd(v);
            G[u].push_back(v); G[v].push_back(u);
        }
        dfs(1, 1);
    //  for (int i = 1; i <= n; i++)debug(node[i]), puts("");
        rd(q);
        while (q--){
            int u, v; rd(u); rd(v);
            if (u == 0)
            {
                node[v]->access();
                if (node[v]->col == inf)node[v]->col = 0;
                else node[v]->col = inf;
                node[v]->push_up();
            }
            else
            {
                node[v]->access();
                int ans = min(*node[v]->chain.begin(), node[v]->rs);
                if (ans == inf)ans = -1;
                pt(ans); puts("");
            }
    //      for (int i = 1; i <= n; i++)debug(node[i]), puts("");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 02:44:12

SPOJ QTREE5 lct裸题的相关文章

SPOJ QTREE3 lct裸题

题目链接 题意: 给定n个点 q个询问 下面n-1行给出树边,点有黑或白色,初始化为白色 下面q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑点的点标, 若不存在黑点则输出-1 思路: lct裸题 #include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector> #inc

SPOJ QTREE2 lct裸题

题目链接 题意: 给一棵树,有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector> #include <map> #include <queue> #include <algorithm> #inclu

SPOJ QTREE6 lct裸题

题目链接 岛娘出的题,还是比较容易的 #include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector> #include <map> #include <queue> #include <algorithm> #include <stack> #include <cstrin

SPOJ Query on a tree lct裸题

题目链接:点击打开链接 把边权化成点权,每个点的点权表示父边的边权. 求path(x, y) 把x access后,则 x 就变成了根所在的splay , 且x是这条链上深度最大的节点.(下面对于根所在的splay称为splay_root) 那么y沿着父节点爬上去,当父节点 fa_y 坐落在splay_root上时,fa_y深度一定比x小,即一定在x的上方. 再把y access上去, 在y最后要和splay_root合并之前, 把fa_y splay一下, 那么path(x, y) 的一部分就

一些LCT裸题

又来回炉lct了= = [bzoj3514]: Codechef MARCH14 GERALD07加强版 模版题.常见姿势,把边也当成点. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 const int maxn=200233<<1; 6 const int inf=1000023333; 7 struct zs{ 8 int u,v

【BZOJ2049】【SDOI2008】Cave 洞穴勘测 LCT裸题 模版题 数组版

数组,至少目前我只写数组,不写指针. LCT这种东西我不打算讲或者什么乱七八糟的,反正这一篇是自用. 同样,看这篇博客的人可以先去别的地方学LCT,然后来我这扒代码. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ls son[x][0] #define rs son[x][1] #define is(x) (x==son[f

bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /************************************************************** Problem: 2049 User: walfy Language: C++ Result: Accepted Time:1896 ms Memory:1508 kb ************************************************

SPOJ QTREE5 lct

题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set维护的是对于每一个不是链上,可是this的子树,那些子树中距离this近期的白点距离. #include <stdio.h> #include <string.h> #include <set> #include <algorithm> #include <

SPOJ QTREE2 lct

题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector> #include <map> #include <queue> #include <algorithm> #inclu