@loj - 3044 @ "ZJOI2019" Minimax search


@description@

Nine is a poor girl likes to play games. In order to enhance the level of their game, she wants to arm themselves with weapons theory. This question and search for the famous Minimax.

The poor have a rooted tree, the root node number is 1. The depth of the root node is defined as 1, the depth of the other nodes of the depth of its parent plus one. While the leaf nodes at a given weight, the poor defines the weight of each non-node values ​​in the following manner:

对于深度为奇数的非叶子节点,它的权值是它所有子节点的权值最大值。
对于深度为偶数的非叶子节点,它的权值是它所有子节点的权值最小值。

Initially, the poor make the right leaf node number i value of i, and get right to the root of the calculated value of W.

Now, evil Cedyks want to modify the weights of some leaf node to the root node of the value of the right to make changes. Cedyks a quantum attack is designed, after challenge is launched, Cedyks randomly get a non-empty leaf node set S of control, and may take some energy to modify the weights of the S in the leaf node.

However, modifying the weight of the leaf node which consumes energy, for the leaf node S i, its initial weight value i, it is assumed that Cedyks modified into weights wi (wi may be any integer, including negative) then Cedyks energy in the attack, it takes as \ (max_ {i \ in S} | i - w_i | \) .

Cedyks want to save energy as much as possible, so he will always be the least energy to complete the attack, that at the minimum cost of energy, the right to make a change in the value of the root node. So that W (S) is the energy Cedyks After gaining control of the set S it will take. Particularly, for some set S, the weights may be changed in any case S of leaf nodes, the root node will not change the weight, this time, the value of W (S) is defined to n. For convenience, we call w (S) for the stability of S.

When there are m leaf nodes, a total of 2 ^ m-1 Species of different non-empty leaf node. Before the attack, Cedyks want to look at the estimated cost of the energy they need. He gives an interval [L, R], he wants to know, for each k ∈ [L, R], the number of sets S satisfies w (S) = k.

The answer may be very large. Please 998,244,353 modulo output pair.

And prompt data range
to 100% of the data, to ensure that 2 <= n <= 200000, 1 <= L <= R <= n.

@solution@

Given the stability k, we can get the weight of each leaf of the range of values can be changed.
However, there may be no i such that \ (max_ {i \ in S} | i - W_i | = K \) , the definition does not meet the degree of stability,
we find ans [k] represents all i satisfy \ (max_ {i \ in S} | i - w_i | \ le k \) the number of collections, and finally make a difference.

Leaves assumed a given set S, may want to change the maximum value of the root, the leaves must take S takes the maximum and minimum values can be taken.
Root weights established value w, if the change may result in root weight min <w, if the change may result in root weight max> w.

Thus it is conceivable a dp: definition of dp [0/1] [i] i represents the number of leaf subtree tree set, the leaves change weights after weight value i min / max w. Finally get dp [0] [rt] + dp [1] [rt] is the answer.
But there is a problem, some legal collection may also take min, max also take legal, it will re-count.

Notice into min, w smaller than those leaf nodes actually did not change (it has not changed, because it must have been eliminated w directly or indirectly in a max node).
Similarly, when changed max, w is greater than that of the leaf node does not need to be changed.

However, for the leaf node is equal to w, both may become large or may become small.
W is equal to only one note, if a collection comprises a leaf w, its stability is necessarily 1 (= direct change of the weights w, the root naturally changed).
So we can force the leaf collection does not contain w, w, and finally coupled contains contributions.

We denote the f [x] represents only the> w min if changed to <w, denoted g [x] represents only the w. Dp can be obtained twice as f, g.
Finally, according to The number of leaves w calculate the answer.

Each is dp (n) complexity of O, but we require all dp values [L, R] in.
Note that i and i + 1 corresponds to the case at most only two leaves will change, to direct dynamic dp.

@accepted code@

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 200000;
const int MOD = 998244353;
struct mint{
    int x;
    mint(int _x=0) : x(_x) {}
    friend mint operator + (mint a, mint b) {return a.x + b.x >= MOD ? a.x + b.x - MOD : a.x + b.x;}
    friend mint operator - (mint a, mint b) {return a.x - b.x < 0 ? a.x - b.x + MOD : a.x - b.x;}
    friend mint operator * (mint a, mint b) {return 1LL * a.x * b.x % MOD;}
    friend void operator *= (mint &a, mint b) {a = a * b;}
    friend void operator /= (mint &a, mint b) {
        int p = MOD - 2;
        while( p ) {
            if( p & 1 ) a *= b;
            b *= b;
            p >>= 1;
        }
    }
};
struct matrix{
    mint m[3][3];
    matrix() {
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                m[i][j] = 0;
    }
    friend matrix operator * (matrix A, matrix B) {
        matrix C;
        C.m[0][0] = A.m[0][0]*B.m[0][0] + A.m[0][1]*B.m[1][0] + A.m[0][2]*B.m[2][0];
        C.m[0][1] = A.m[0][0]*B.m[0][1] + A.m[0][1]*B.m[1][1] + A.m[0][2]*B.m[2][1];
        C.m[0][2] = A.m[0][0]*B.m[0][2] + A.m[0][1]*B.m[1][2] + A.m[0][2]*B.m[2][2];
        C.m[1][0] = A.m[1][0]*B.m[0][0] + A.m[1][1]*B.m[1][0] + A.m[1][2]*B.m[2][0];
        C.m[1][1] = A.m[1][0]*B.m[0][1] + A.m[1][1]*B.m[1][1] + A.m[1][2]*B.m[2][1];
        C.m[1][2] = A.m[1][0]*B.m[0][2] + A.m[1][1]*B.m[1][2] + A.m[1][2]*B.m[2][2];
        C.m[2][0] = A.m[2][0]*B.m[0][0] + A.m[2][1]*B.m[1][0] + A.m[2][2]*B.m[2][0];
        C.m[2][1] = A.m[2][0]*B.m[0][1] + A.m[2][1]*B.m[1][1] + A.m[2][2]*B.m[2][1];
        C.m[2][2] = A.m[2][0]*B.m[0][2] + A.m[2][1]*B.m[1][2] + A.m[2][2]*B.m[2][2];
        return C;
    }
}I;
mint pw2[MAXN + 5];
void init() {
    pw2[0] = 1;
    for(int i=1;i<=MAXN;i++)
        pw2[i] = 2*pw2[i-1];
    I.m[0][0] = I.m[1][1] = I.m[2][2] = 1;
}
struct edge{
    int to; edge *nxt;
}edges[2*MAXN + 5], *adj[MAXN + 5], *ecnt = edges;
void addedge(int u, int v) {
    edge *p = (++ecnt);
    p->to = v, p->nxt = adj[u], adj[u] = p;
    p = (++ecnt);
    p->to = u, p->nxt = adj[v], adj[v] = p;
}
int val[MAXN + 5], dep[MAXN + 5];
int fa[MAXN + 5], siz[MAXN + 5], hvy[MAXN + 5];
void dfs1(int x, int f) {
    fa[x] = f, siz[x] = 1, hvy[x] = 0, dep[x] = dep[f] + 1;
    for(edge *p=adj[x];p;p=p->nxt) {
        if( p->to == f ) continue;
        dfs1(p->to, x), siz[x] += siz[p->to];
        val[x] = (val[x] ? ((dep[x] & 1) ? max(val[x], val[p->to]) : min(val[x], val[p->to])) : val[p->to]);
        if( siz[hvy[x]] < siz[p->to] )
            hvy[x] = p->to;
    }
    if( !hvy[x] ) val[x] = x;
}
struct node{
    int z; mint x;
    node(int _x=0) : z(0), x(_x) {}
    friend void operator *= (node &a, mint b) {
        if( b.x == 0 ) a.z++;
        else a.x *= b;
    }
    friend void operator /= (node &a, mint b) {
        if( b.x == 0 ) a.z--;
        else a.x /= b;
    }
    mint key() {return z ? 0 : x;}
}a[MAXN + 5], b[MAXN + 5];
mint f[MAXN + 5], g[MAXN + 5];
/*
f -> (only modify leaf > v1, vi < w)
g -> (only modify leaf < v1, vi > w)
*/
int lc1[MAXN + 5], lc2[MAXN + 5]; 
int top[MAXN + 5], dfn[MAXN + 5], tid[MAXN + 5], btm[MAXN + 5], dcnt;
int d;
int ff(int x) {return (x > val[1] && x - d < val[1]) + (x < val[1]);}
int gg(int x) {return (x < val[1] && x + d > val[1]) + (x > val[1]);}
//选入集合 + 不选入集合
int dfs2(int x, int tp) {
    top[x] = tp, dfn[++dcnt] = x, tid[x] = dcnt;
    if( !hvy[x] ) {
/*
        f[x] = (x > val[1] && x - 0 < val[1]) + (x < val[1]);
        g[x] = (x < val[1] && x + 0 > val[1]) + (x > val[1]);
*/
        f[x] = ff(x), g[x] = gg(x);
        lc1[x] += (x > val[1]), lc2[x] += (x < val[1]);
        return btm[x] = x;
    }
    btm[x] = dfs2(hvy[x], tp), lc1[x] += lc1[hvy[x]], lc2[x] += lc2[hvy[x]];
    for(edge *p=adj[x];p;p=p->nxt)
        if( p->to != hvy[x] && p->to != fa[x] )
            dfs2(p->to, p->to), lc1[x] += lc1[p->to], lc2[x] += lc2[p->to];
    if( dep[x] & 1 ) {
        a[x] = b[x] = 1;
        for(edge *p=adj[x];p;p=p->nxt)
            if( p->to != hvy[x] && p->to != fa[x] )
                a[x] *= f[p->to], b[x] *= (pw2[lc2[p->to]] - g[p->to]);
        f[x] = a[x].key()*f[hvy[x]];
        g[x] = pw2[lc2[x]] - b[x].key()*pw2[lc2[hvy[x]]] + b[x].key()*g[hvy[x]];
    }
    else {
        a[x] = b[x] = 1;
        for(edge *p=adj[x];p;p=p->nxt)
            if( p->to != hvy[x] && p->to != fa[x] )
                a[x] *= g[p->to], b[x] *= (pw2[lc1[p->to]] - f[p->to]);
        g[x] = a[x].key()*g[hvy[x]];
        f[x] = pw2[lc1[x]] - b[x].key()*pw2[lc1[hvy[x]]] + b[x].key()*f[hvy[x]];
    }
    return btm[x];
}
matrix getM(int x) {
    if( !hvy[x] ) return I;
    matrix M;
    if( dep[x] & 1 ) {
        M.m[0][0] = a[x].key(), M.m[1][1] = b[x].key();
        M.m[1][2] = pw2[lc2[x]] - b[x].key()*pw2[lc2[hvy[x]]];
        M.m[2][2] = 1;
    }
    else {
        M.m[1][1] = a[x].key(), M.m[0][0] = b[x].key();
        M.m[0][2] = pw2[lc1[x]] - b[x].key()*pw2[lc1[hvy[x]]];
        M.m[2][2] = 1;
    }
    return M;
}
struct segtree{
    #define lch (x << 1)
    #define rch (x << 1 | 1)
    struct node{
        int le, ri;
        matrix M;
    }t[4*MAXN + 5];
    void pushup(int x) {t[x].M = t[lch].M * t[rch].M;}
    void build(int x, int l, int r) {
        t[x].le = l, t[x].ri = r;
        if( l == r ) {
            t[x].M = getM(dfn[l]);
            return ;
        }
        int m = (l + r) >> 1;
        build(lch, l, m), build(rch, m + 1, r);
        pushup(x);
    }
    void update(int x, int p) {
        if( p > t[x].ri || p < t[x].le )
            return ;
        if( t[x].le == t[x].ri ) {
            t[x].M = getM(dfn[p]);
            return ;
        }
        update(lch, p), update(rch, p);
        pushup(x);
    }
    matrix query(int x, int l, int r) {
        if( l > t[x].ri || r < t[x].le )
            return I;
        if( l <= t[x].le && t[x].ri <= r )
            return t[x].M;
        return query(lch, l, r) * query(rch, l, r);
    }
}T;
void modify(int x) {
    while( fa[top[x]] ) {
        x = top[x];
        if( dep[fa[x]] & 1 )
            a[fa[x]] /= f[x], b[fa[x]] /= (pw2[lc2[x]] - g[x]);
        else a[fa[x]] /= g[x], b[fa[x]] /= (pw2[lc1[x]] - f[x]);
        matrix M = T.query(1, tid[x], tid[btm[x]]);
        f[x] = M.m[0][0]*ff(btm[x]) + M.m[0][2];
        g[x] = M.m[1][1]*gg(btm[x]) + M.m[1][2];
        if( dep[fa[x]] & 1 )
            a[fa[x]] *= f[x], b[fa[x]] *= (pw2[lc2[x]] - g[x]);
        else a[fa[x]] *= g[x], b[fa[x]] *= (pw2[lc1[x]] - f[x]);
        x = fa[x];
        T.update(1, tid[x]);
    }
}
mint get_ans() {
    matrix M = T.query(1, tid[1], tid[btm[1]]);
    mint f1 = M.m[0][0]*ff(btm[1]) + M.m[0][2];
    mint g1 = M.m[1][1]*gg(btm[1]) + M.m[1][2];
    return pw2[lc1[1] + lc2[1]] - (pw2[lc1[1]] - f1) * (pw2[lc2[1]] - g1);
}
int n, L, R;
mint ans[MAXN + 5];
int read() {
    int x = 0; char ch = getchar();
    while( ch > '9' || ch < '0' ) ch = getchar();
    while( '0' <= ch && ch <= '9' ) x = 10*x + ch - '0', ch = getchar();
    return x;
}
void write(int x) {
    if( !x ) return ;
    write(x/10);
    putchar(x%10 + '0');
}
int main() {
    init(), n = read(), L = read(), R = read();
    for(int i=1;i<n;i++) {
        int u = read(), v = read();
        addedge(u, v);
    }
    dfs1(1, 0), dfs2(1, 1), T.build(1, 1, n);
    for(d=1;d<=n;d++) {
        int p = (val[1] + 1 - d);
        if( p < val[1] && p >= 1 && !hvy[p] )
            modify(p);
        p = (val[1] - 1 + d);
        if( p > val[1] && p <= n && !hvy[p] )
            modify(p);
        ans[d] = get_ans();
    }
    ans[n] = pw2[lc1[1]+lc2[1]] - ans[n] - 1;
    for(int i=n-1;i>=1;i--) ans[i] = ans[i] - ans[i-1];
    ans[1] = ans[1] + pw2[lc1[1]+lc2[1]];
    for(int i=L;i<=R;i++) {
        if( ans[i].x ) write(ans[i].x); else putchar('0');
        putchar(i == n ? '\n' : ' ');
    }
}

@details@

So I do not know why the local big test sample to run the AC 5s actually pay up.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11915347.html