Bzoj4129: Haruna’s Breakfast

Posted cjoieryl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bzoj4129: Haruna’s Breakfast相关的知识,希望对你有一定的参考价值。

题面

Bzoj

Sol

树上带修改莫队

\(mex\)可以对数字也分块
数字大于\(n\)就设为\(n+1\)
查询就找到那个不满的块,在块内找到\(mex\)

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

template <class Int>
IL void Input(RG Int &x){
    RG int z = 1; RG char c = getchar(); x = 0;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= z;
}

const int maxn(5e4 + 5);

int n, q, first[maxn], cnt, w[maxn], sum1[maxn], sum2[300], ans[maxn], sz;
int dfn[maxn], st[20][maxn << 1], lg[maxn << 1], deep[maxn], idx, fa[maxn];
int sta[maxn], bl[maxn], blo, num, vis[maxn], tot1, tot2;

struct Edge{
    int to, next;
} edge[maxn << 1];

struct Query{
    int l, r, id, t;

    IL int operator <(RG Query B) const{
        if(bl[l] != bl[B.l]) return bl[l] < bl[B.l];
        if(bl[r] != bl[B.r]) return bl[r] < bl[B.r];
        return t < B.t;
    }
} qry[maxn];

struct Change{
    int x, y;
} mdy[maxn];

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Dfs(RG int u){
    dfn[u] = ++idx, st[0][idx] = u;
    RG int l = sta[0];
    for(RG int e = first[u]; e != -1; e = edge[e].next){
        RG int v = edge[e].to;
        if(dfn[v]) continue;
        deep[v] = deep[u] + 1, fa[v] = u;
        Dfs(v);
        st[0][++idx] = u;
        if(sta[0] - l < blo) continue;
        for(++num; sta[0] != l; --sta[0]) bl[sta[sta[0]]] = num;
    }
    sta[++sta[0]] = u;
}

IL void Chk(RG int &x, RG int u, RG int v){
    x = deep[u] < deep[v] ? u : v;
}

IL int LCA(RG int u, RG int v){
    u = dfn[u], v = dfn[v];
    if(u > v) swap(u, v);
    RG int log2 = lg[v - u + 1], t;
    Chk(t, st[log2][u], st[log2][v - (1 << log2) + 1]);
    return t;
}

IL void Update(RG int x){
    if(vis[x]){
        --sum1[w[x]];
        if(!sum1[w[x]]) --sum2[w[x] / sz];
    }
    else{
        if(!sum1[w[x]]) ++sum2[w[x] / sz];
        ++sum1[w[x]];
    }
    vis[x] ^= 1;
}

IL void Adjust(RG int x, RG int &y){
    if(vis[x]) Update(x), swap(w[x], y), Update(x);
    else swap(w[x], y);
}

IL void Modify(RG int u, RG int v){
    while(u != v){
        if(deep[u] > deep[v]) swap(u, v);
        Update(v), v = fa[v];
    }
}

IL int Calc(){
    for(RG int i = 0; ; ++i){
        if(sum2[i] == sz) continue;
        for(RG int j = 0; j < sz; ++j)
            if(!sum1[i * sz + j]) return i * sz + j;
    }
    return 233;
}

IL void Init_Graph(){
    Input(n), Input(q);
    blo = pow(n, 0.2 / 0.3), sz = sqrt(n);
    for(RG int i = 1; i <= n; ++i){
        Input(w[i]), first[i] = -1;
        if(w[i] > n) w[i] = n + 1;
    }
    for(RG int i = 1, u, v; i < n; ++i) Input(u), Input(v), Add(u, v), Add(v, u);
    Dfs(1);
    for(++num; sta[0]; --sta[0]) bl[sta[sta[0]]] = num;
    for(RG int i = 2; i <= idx; ++i) lg[i] = lg[i >> 1] + 1;
    for(RG int j = 1; j <= lg[idx]; ++j)
        for(RG int i = 1; i + (1 << j) - 1 <= idx; ++i)
            Chk(st[j][i], st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}

IL void Init_Task(){
    for(RG int i = 1, op, x, y; i <= q; ++i){
        Input(op), Input(x), Input(y);
        if(op){
            if(dfn[x] > dfn[y]) swap(x, y);
            qry[++tot1] = (Query){x, y, tot1, tot2};
        }
        else mdy[++tot2] = (Change){x, y};
    }
    sort(qry + 1, qry + tot1 + 1);
}

int main(RG int argc, RG char* argv[]){
    Init_Graph(), Init_Task();
    RG int j = 0, lca = LCA(qry[1].l, qry[1].r);
    while(j < qry[1].t) ++j, Adjust(mdy[j].x, mdy[j].y);
    Modify(qry[1].l, qry[1].r);
    Update(lca), ans[qry[1].id] = Calc(), Update(lca);
    for(RG int i = 2; i <= tot1; ++i){
        while(j < qry[i].t) ++j, Adjust(mdy[j].x, mdy[j].y);
        while(j > qry[i].t) Adjust(mdy[j].x, mdy[j].y), --j;
        lca = LCA(qry[i].l, qry[i].r);
        Modify(qry[i - 1].l, qry[i].l), Modify(qry[i - 1].r, qry[i].r);;
        Update(lca), ans[qry[i].id] = Calc(), Update(lca);
    }
    for(RG int i = 1; i <= tot1; ++i) printf("%d\n", ans[i]);
    return 0;
}

以上是关于Bzoj4129: Haruna’s Breakfast的主要内容,如果未能解决你的问题,请参考以下文章

[BZOJ 4129]Haruna’s Breakfast(树上带修改莫队)

bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

BZOJ4129(树上带修莫队)

4129:变换的迷宫(bfs)

c编程中,warning C4129: 's' : unrecognized character escape sequence这是啥意思

xml 1dccb019-784e-4129-9c73-a47d2a2f966a