2018焦作网络赛 E Jiu Yuan Wants to Eat(线段树+树链剖分)

题意

对一个有1e5个点,点权初值为0的树上进行4种操作:

1、结点u到结点v上的所有点权乘x。

2、结点u到结点v上所有的点权加x。

3、结点u到结点v上所有的点权取非。

4、结点u到结点v路径上点权的和。

答案模\(2^{64}\)

思路

对操作1、2树链剖分加线段树维护即可,对操作3,取非操作为:一个二进制位上都为1

的数减去当前值,即:\(2^{64}-1-x\) , 由于答案模\(2^{64}\),通过点权用unsigned long long,只需维护\(-(x+1)\) 操作即可。

Code

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5+10;
const int SZ = 1 << 20;  //快速io
struct fastio {
    char inbuf[SZ];
    char outbuf[SZ];
    fastio() {
        setvbuf(stdin, inbuf, _IOFBF, SZ);
        setvbuf(stdout, outbuf, _IOFBF, SZ);
    }
} io;

void read(int &x) {
    x=0; char ch, c=getchar();
    while(c<'0' || c>'9') ch=c, c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-'0', c=getchar();
    if(ch=='-') x=-x;
}

int n, m;
int dfn[maxn], rnk[maxn], top[maxn], tot;
int sz[maxn], son[maxn], dep[maxn], fa[maxn];
vector<int> g[maxn];

void dfs1(int x, int f) {
    sz[x]=1, son[x]=0, dep[x] = dep[f]+1;
    for (auto v: g[x]) {
        fa[v] = x;
        dfs1(v, x);
        sz[x] += sz[v];
        if(sz[v] > sz[son[x]]) son[x] = v;
    }
}
void dfs2(int x, int topf) {
    dfn[x]=++tot, rnk[tot]=x, top[x]=topf;
    if(!son[x]) return;
    dfs2(son[x], topf);
    for (auto v: g[x]) {
        if(v!=son[x])
            dfs2(v, v);
    }
}

typedef unsigned long long ull;
struct segTree {
    struct node {
        int l, r;
        ull val, add, mul;
        inline int length() { return r-l+1; }
    }tr[maxn<<2];
    void pushup(int i) {
        tr[i].val = tr[i<<1].val + tr[i<<1|1].val;
    }
    void pushdown(int i) {
        if(tr[i].add==0 && tr[i].mul==1) return;
        tr[i<<1].val = tr[i<<1].val*tr[i].mul + tr[i].add*(tr[i<<1].r-tr[i<<1].l+1);
        tr[i<<1|1].val = tr[i<<1|1].val*tr[i].mul + tr[i].add*(tr[i<<1|1].r-tr[i<<1|1].l+1);
        tr[i<<1].add = tr[i<<1].add*tr[i].mul+tr[i].add;
        tr[i<<1|1].add = tr[i<<1|1].add*tr[i].mul+tr[i].add;
        tr[i<<1].mul *= tr[i].mul;
        tr[i<<1|1].mul *= tr[i].mul;
        tr[i].add = 0, tr[i].mul = 1;
    }
    void build(int l, int r, int i) {
        tr[i] = node{l, r, 0, 0, 1};
        if(l==r) return;
        int mid = l+r>>1;
        build(l, mid, i<<1);
        build(mid+1, r, i<<1|1);
        pushup(i);
    }
    void update(int l, int r, ull val, int op, int i) {
        if(l<=tr[i].l && tr[i].r<=r) {
            if(op==2) {
                tr[i].val += val*tr[i].length();
                tr[i].add += val;
            } else {
                tr[i].val *= val;
                tr[i].mul *= val;
                tr[i].add *= val;
            }
            return;
        }
        int mid = tr[i].l+tr[i].r>>1;
        pushdown(i);
        if(l<=mid) update(l, r, val, op, i<<1);
        if(r>mid) update(l, r, val, op, i<<1|1);
        pushup(i);
    }
    ull query(int l, int r, int i) {
        if(l<=tr[i].l && tr[i].r<=r) return tr[i].val;
        int mid=tr[i].l+tr[i].r>>1;
        ull res=0;
        pushdown(i);
        if(l<=mid) res += query(l, r, i<<1);
        if(r>mid) res += query(l, r, i<<1|1);
        return res;
    }
}st;

void update(int u, int v, ull val, int op) {
    while(top[u] != top[v]) {
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        st.update(dfn[top[u]], dfn[u], val, op, 1);
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) swap(u, v);
    st.update(dfn[u], dfn[v], val, op, 1);
}
ull query(int u, int v) {
    ull res = 0;
    while(top[u] != top[v]) {
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        res += st.query(dfn[top[u]], dfn[u], 1);
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) swap(u, v);
    res += st.query(dfn[u], dfn[v], 1);
    return res;
}

int main() {
//    freopen("input.in", "r", stdin);
    while(~scanf("%d", &n)) {
        tot = 0;
        for (int i=1; i<=n; ++i) g[i].clear();
        for (int x, i=2; i<=n; ++i) {
            read(x);
            g[x].push_back(i);
        }
        st.build(1, n, 1);
        dfs1(1, 0);
        dfs2(1, 1);
//        for (int i=1; i<=n; ++i) printf("%d ", top[i]); puts("");
//        for (int i=1; i<=n; ++i) printf("%d ", dep[i]); puts("");
        scanf("%d", &m);
        for (int op, u, v, i=1; i<=m; ++i) {
            read(op); read(u); read(v);
//            scanf("%d%d%d", &op, &u, &v);
            if(op==1 || op==2) {
                ull x;
                scanf("%llu", &x);
                update(u, v, x, op);
            } else if(op==3) {
                update(u, v, 1, 2);
                update(u, v, -1, 1);
            } else {
                ull ans = query(u, v);
                printf("%llu\n", ans);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/acerkoo/p/11295766.html

时间: 2024-10-31 01:03:58

2018焦作网络赛 E Jiu Yuan Wants to Eat(线段树+树链剖分)的相关文章

2018焦作网络赛-E- Jiu Yuan Wants to Eat

题目描述 You ye Jiu yuan is the daughter of the Great GOD Emancipator.  And when she becomes an adult, she will be queen of Tusikur, so she wanted to travel the world while she was still young. In a country, she found a small pub called Whitehouse. Just

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)

树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^{64}\)的特殊性,可将其转化为线性变换. 显然 \[-x\equiv (2^{64}-1)*x (mod\ 2^{64})\] 因为\[!x = (2^{64}-1) -x \] 所以 \[ !x = (2^{64}-1) + (2^{64}-1)x\] #include<bits/stdc++

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat

分析 除了树剖没想到其他解法. 用线段树维护区间和,同时针对修改区间修改操作建立两个lazy标记,一个是\(lazy_{mul}\),另一个是\(lazy_{add}\),代表区间里的数都需要先乘以\(lazy_{mul}\),再加上\(lazy_{add}\).如果一个区间需要被重复标记,那么我们可以先把新的lazy标记施加在原有的lazy上. 如果是区间乘以val,那么就可以$ lazy_{mul}=lazy_{mul} \times val,lazy_{add}=lazy_{add}\ti

ACM-ICPC 2018 焦作赛区网络预赛 E Jiu Yuan Wants to Eat (树链剖分+线段树)

题目链接:https://nanti.jisuanke.com/t/31714 题意:给你一棵树,初始全为0,有四种操作: 1.u-v乘x    2.u-v加x   3. u-v取反  4.询问u-v的和 思路: 除去第三个操作就是很简单的树链剖分+线段树多重标记下放,所以我们只要考虑怎么维护第三个操作就好了, 由题目给的取反可知:!x =  (2^64-1) - x;   但是这样维护还是很麻烦,因为这道题是对2^64取模的,我们可以 尝试把这个式子转换成只有加法和乘法的,这样就可以将其和前面

2018焦作网络赛B

dp维护一下最大值与最小值,注意边界情况的判定. #include <iostream> #include <cstring> using namespace std; const long long inf = (long long)1e18+(long long)9; long long dp[7][1005]; long long dpx[7][1005]; int f[1005]; char s[15]; int main() { int T; // int l; int

2018焦作网络赛E

区间更新加法与乘法,x取反是2^64-x-1,由于取模所以取反变成-x-1,就区间+1再*-1就可以了,最后区间询问求和. 待补 原文地址:https://www.cnblogs.com/LMissher/p/9655105.html

2018焦作网络赛 - Poor God Water 一道水题的教训

本题算是签到题,但由于赛中花费了过多的时间去滴吧格,造成了不必要的浪费以及智商掉线,所以有必要记录一下 题意:方格从1到n,每一格mjl可以选择吃鱼/巧克力/鸡腿,求走到n格时满足 1.每三格不可重复同一种食物 2.每三格均不同食物时中间格子不可吃巧克力 3.每三格前后两格不可同时吃巧克力 以上三个条件的方案数,n<1e10 太长不看版:打表+快速幂AC 长篇吐槽版 很显然的,设\(dp[n][i][j][k]\),走到第\(n\)格时第\(n-2\)格的食物是\(i\),第\(n-1\)的食物

2018焦作网络赛J

不知道题意,队友用java大数+二分过了? import java.util.Arrays; import java.util.Scanner; import java.io.*; import java.math.*; public class Main { static boolean check(BigInteger num,BigInteger n) { boolean flag = true; BigInteger a = new BigInteger("0"); BigIn

2018焦作网络赛G-Give Candies

G: Give Candies 时间限制: 1 Sec  内存限制: 128 MB 题目描述 There are N children in kindergarten. Miss Li bought them N candies.To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1…N),