Cattle-off practice match 26 E- path tree (the tree split + chain segment tree)

Links: https://ac.nowcoder.com/acm/contest/180/E
Source: Cattle-off network

Tree path
time limit: C / C ++ 2 seconds and 4 seconds languages other
space restrictions: C / C ++ 262144K, other languages 524288K
64bit the IO the Format:% LLD
subject description
is given of the n points of the tree, the root node is the number 1 each point has a weight
you need to support the following
1. u will be the subtree rooted at the node (including u) adding weights val
2. the right node on the (u, V) plus the value of the path val
3. ask the right node on the (u, v) of the path and the value of pairwise

Input Description:
The first line of two integers n, m, represents the number of operations and the number of nodes of the tree

Next, a line number n, the value for each node weight

1 has side lines of two integers (u, v), represented by (u, v) between the - next n

Then m-th row
begins a few opt, indicates the type of operation
when the opt = 1, the next two integer u, val
if opt = 2, represented by the next three integers (u, v), val
if opt = 3, The next two integer (u, v)
the meanings are as shown in title
output description:
for each of the third operation, a number represented by the output answer to 10 ^ 9 + 710
9
+7 modulo
example 1
input
copy
38
5 3. 1
. 1 2
. 1 3
3. 1 2
3. 1 3
3 2 3
. 1. 1 2
2 2 3. 1
3. 1 2
3. 1 3
3 2 3
output
copy
15
5
23 is
45
45
115
described
the first set of query results: 3 * 5 15 =
second group of interrogation results: 5 = 1 * 5
a third set of query results: 5 + 1 * 3 * 3 * 5 + 1 = 23
Notes:
For 30% 30% of the data, n, m \ leqslant 100n, m⩽100
data to the 100% to 100%, n-, m \ ^ 5N leqslant 10, m⩽10
. 5

Provided a_ia
i represents the weight of the read node i and weights each modification to ensure that a_i \ ^ 10. 4A leqslant i ⩽10 . 4





To ensure that there will be no negative

Idea:
It is clear that the chain tree split title,

The first two operations are split chain of normal operation tree,

We look at three Dir operation, we assume that there are three nodes on the route inquiry, the weights are a, b, c ,, the result is that we ask A B A + how the term c + b * c get this result?

Twenty-two then added result of the multiplication i.e. we can maintain a two interval values ​​(sum values ​​and node weight and the sum of the square of the node weights) to derive the interval (i.e., the output of the operation 3).

We know that tree line maintenance interval sum and is very easy, here I do not speak, then how to maintain the sum of the square of the weight of it (that is, when the interval Each value plus t, how easy it is to get a new square and sum )?

We assume that a change interval of three nodes, the weights are a, b, c

In this way we can see that we can maintain two values ​​from the tree line: sum and square sum to update the square sum

For example interval plus t

Before the square sum of the square sum = new update interval length times t + t + 2T by weight sum and multiply.

So that you can write this question of, laze mark pushdown remember when the tree line must be + = instead of =

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const ll mod=1e9+7ll;
std::vector<int> son[maxn];
ll w[maxn];
ll wt[maxn];
int id[maxn];
int SZ[maxn];
int wson[maxn];
int top[maxn];
int fa[maxn];
int n,m;
int dep[maxn];
int cnt;
void init()
{
    cnt=0;
}
void dfs1(int x,int pre,int step)
{
    fa[x]=pre;
    dep[x]=step;
    SZ[x]=1;
    int maxson=-1;
    for(auto & t:son[x])
    {
        if(t!=pre)
        {
            dfs1(t,x,step+1);
            SZ[x]+=SZ[t];
            if(SZ[t]>maxson)
            {
                maxson=SZ[t];
                wson[x]=t;
            }
        }
    }
}

void dfs2(int x,int topf)
{
    top[x]=topf;
    id[x]=++cnt;
    wt[cnt]=w[x];

    if(wson[x])
        dfs2(wson[x],topf);
    else
        return ;
    for(auto &t :son[x])
    {
        if(t==wson[x]||t==fa[x])
        {
            continue;
        }
        dfs2(t,t);
    }
}
struct node
{
    ll l,r;
    ll sum;
    ll csum;
    ll laze;
}segment_tree[maxn<<2];
void pushup(int rt)
{
    segment_tree[rt].sum=(segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum)%mod;
    segment_tree[rt].csum=(segment_tree[rt<<1].csum+segment_tree[rt<<1|1].csum)%mod;
}
void build(int rt,int l,int r)
{
    segment_tree[rt].l=l;
    segment_tree[rt].r=r;
    segment_tree[rt].laze=0ll;
    if(l==r)
    {
        segment_tree[rt].sum=wt[l];
        segment_tree[rt].csum=wt[l]*wt[l]%mod;
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}
void pushdown(int rt)
{
    if(segment_tree[rt].laze)
    {
        ll val=segment_tree[rt].laze%mod;
        segment_tree[rt].laze=0ll;
        segment_tree[rt<<1].csum+=((segment_tree[rt<<1].r-segment_tree[rt<<1].l+1)*val%mod*val%mod+2ll*val%mod*(segment_tree[rt<<1].sum)%mod)%mod;
        segment_tree[rt<<1].csum%=mod;
        segment_tree[rt<<1].sum+=(segment_tree[rt<<1].r-segment_tree[rt<<1].l+1)*val%mod;
        segment_tree[rt<<1].sum%=mod;
        segment_tree[rt<<1].laze+=val;
        segment_tree[rt<<1].laze%=mod;
        segment_tree[rt<<1|1].csum+=((segment_tree[rt<<1|1].r-segment_tree[rt<<1|1].l+1)*val%mod*val%mod+2ll*val%mod*(segment_tree[rt<<1|1].sum)%mod)%mod;
        segment_tree[rt<<1|1].csum%=mod;
        segment_tree[rt<<1|1].sum+=(segment_tree[rt<<1|1].r-segment_tree[rt<<1|1].l+1)*val%mod;
        segment_tree[rt<<1|1].sum%=mod;
        segment_tree[rt<<1|1].laze+=val;
        segment_tree[rt<<1|1].laze%=mod;
    }
}

void update(int rt,int l,int r,ll val)
{
    val%=mod;
    if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r)
    {
        segment_tree[rt].csum+=((segment_tree[rt].r-segment_tree[rt].l+1)*val%mod*val%mod+2ll*val%mod*segment_tree[rt].sum%mod)%mod;
        segment_tree[rt].csum%=mod;
        segment_tree[rt].laze+=val;
        segment_tree[rt].laze%=mod;
        segment_tree[rt].sum+=(segment_tree[rt].r-segment_tree[rt].l+1)*val%mod;
        segment_tree[rt].sum%=mod;
        return ;
    }
    pushdown(rt);
    int mid=segment_tree[rt].r+segment_tree[rt].l;
    mid>>=1;
    if(mid>=l)
    {
        update(rt<<1,l,r,val);
    }
    if(mid<r)
    {
        update(rt<<1|1,l,r,val);
    }
    pushup(rt);
}

void upson(int x,ll val)
{
    val%=mod;
    update(1,id[x],id[x]+SZ[x]-1,val);
}
void uprange(int x,int y,ll val)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
        {
            swap(x,y);
        }
        update(1,id[top[x]],id[x],val);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y])
    {
        swap(x,y);
    }
    update(1,id[x],id[y],val);
}
ll ask1(int rt,int l,int r)
{
    if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r)
    {
        return segment_tree[rt].sum%mod;
    }
    pushdown(rt);
    int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
    ll res=0ll;
    if(mid>=l)
    {
        res+=ask1(rt<<1,l,r);
        res%=mod;
    }
    if(mid<r)
    {
        res+=ask1(rt<<1|1,l,r);
        res%=mod;
    }
    return res;
}
ll ask2(int rt,int l,int r)
{
    if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r)
    {
        return segment_tree[rt].csum%mod;
    }
    pushdown(rt);
    int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
    ll res=0ll;
    if(mid>=l)
    {
        res+=ask2(rt<<1,l,r);
        res%=mod;
    }
    if(mid<r)
    {
        res+=ask2(rt<<1|1,l,r);
        res%=mod;
    }
    return res;
}

ll qrange(int x,int y)
{
    ll sum1=0ll;
    ll sum2=0ll;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        sum1=(sum1+ask1(1,id[top[x]],id[x]))%mod;
        sum2=(sum2+ask2(1,id[top[x]],id[x]))%mod;
        x=fa[top[x]];
    }
    if(dep[x]>dep[y])
    {
        swap(x,y);
    }
    sum1=(sum1+ask1(1,id[x],id[y]))%mod;
    sum2=(sum2+ask2(1,id[x],id[y]))%mod;
    ll res=(sum1*sum1%mod-sum2+mod)%mod;
    res=(res*powmod(2ll,mod-2ll,mod))%mod;
    return res;
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin>>n>>m;
    repd(i,1,n)
    {
        cin>>w[i];
    }
    int u,v;
    repd(i,2,n)
    {
        cin>>u>>v;
        son[u].pb(v);
        son[v].pb(u);
    }
    init();
    dfs1(1,-1,0);
    dfs2(1,1);
    build(1,1,n);
    int op;
    ll c;
    while(m--)
    {
        cin>>op;
        if(op==1)
        {
            cin>>u>>c;
            upson(u,c);
        }else if(op==2)
        {
            cin>>u>>v>>c;
            uprange(u,v,c);
        }else if(op==3)
        {
            cin>>u>>v;
            cout<<qrange(u,v)<<endl;
        }
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11299530.html