QTREE3 分类: templates spoj 2015-08-06 22:09 9人阅读 评论(0) 收藏


树链剖分,线段树维护区间内最近黑点。


写起来很爽。。。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}

const int maxn = 1e5 + 5, maxm = 1e5 + 5;
const int INF = 0x3f3f3f3f, Nya = -1;

int n, m;
struct Edge
{
    int v, next;

    Edge(int v=0,int next=0):v(v),next(next){}

}edge[maxn<<1];

int head[maxn], el;
int fa[maxn], dep[maxn], son[maxn], size[maxn], top[maxn];
int dfn[maxn<<1], dl, st[maxn], ed[maxn];

namespace seg
{
    int tree[maxn<<2];

#define L(x) ((x)<<1)
#define R(x) ((x)<<1|1)
    void build(int ll,int rr,int si)
    {
        if(ll != rr)
        {
            int mid = (ll + rr)>>1;
            build(ll, mid, L(si));
            build(mid + 1, rr, R(si));
        }
        tree[si] = INF;
    }
    void change(int k,int ll,int rr,int si)
    {
        if(ll == rr) 
        {
            tree[si] = (tree[si] == INF)?k:INF; 
            return;
        }

        int mid = (ll + rr) >> 1;
        if(k <= mid) change(k, ll, mid, L(si));
        else change(k, mid + 1, rr, R(si));

        tree[si] = std::min(tree[L(si)], tree[R(si)]);
    }
    int query(int l,int r,int ll,int rr,int si)
    {
        if(l == ll && r == rr) return tree[si];

        int mid = (ll + rr)>>1;

        if(mid < l) return query(l, r, mid + 1, rr, R(si));
        else if(r <= mid) return query(l, r, ll, mid, L(si));
        else return std::min(query(l, mid, ll, mid, L(si)), query(mid + 1, r, mid + 1, rr, R(si)));
    }
#undef L
#undef R    
}

void NewEdge(int u,int v)
{
    edge[++el] = Edge(v, head[u]), head[u] = el;
}
void init()
{
    int u, v;

    read(n), read(m);
    for(int i = 1; i < n; i++)
        read(u), read(v), NewEdge(u, v), NewEdge(v, u);     
}
void dfs(int a)
{
    size[a] = 1, dep[a] = dep[fa[a]] + 1;

    for(int i = head[a], p; i; i = edge[i].next)
    {
        if((p = edge[i].v) == fa[a]) continue;

        fa[p] = a, dfs(p), size[a] += size[p];
        if(size[p] > size[son[a]]) son[a] = p;
    }
}
void build(int a)
{
    dfn[++dl] = a, st[a] = dl;

    top[a] = (son[fa[a]] == a)?top[fa[a]]:a;

    if(son[a]) build(son[a]);

    for(int i = head[a], p; i; i = edge[i].next)
        if(!((p = edge[i].v) == fa[a] || p == son[a])) build(p);

    ed[a] = dl;
}
void prework()
{
    fa[1] = 0, dfs(1), build(1), seg::build(1, n, 1);
}
int ask(int x)
{
    int ans = INF;

    while(x) ans = std::min(ans, seg::query(st[top[x]], st[x], 1, n, 1)), x = fa[top[x]];

    return (ans < INF)?dfn[ans]:Nya;
}
void solve()
{
    int op, x;

    for(int i = 1; i <= m; i++)
    {
        read(op), read(x);

        if(op)
            write(ask(x)), puts("");
        else
            seg::change(st[x], 1, n, 1);
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("qtree3.in","r",stdin);
    freopen("qtree3.out","w",stdout);
#endif

    init();
    prework();
    solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

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

posted @ 2015-08-06 22:09  <Dash>  阅读(133)  评论(0编辑  收藏  举报