bzoj_1036 Fenwick tree cover tree line

bzoj_1036

★★★★ input file: bzoj_1036.in Output File: bzoj_1036.out    a simple comparison of
the time limit: 1 s memory limit: 162 MB

[ Title Head Description

Tree has n nodes are numbered 1 to n , each node has a weight W . We will take the following form to ask you to perform some action on this tree: 

I. CHANGE ut: the node u weights to

. II QMAX uv: interrogation from the point u to point v maximum weight value of the node on the path 

III QSUM uv:. Interrogation from the point u to point v weight values of the nodes on the path, and 

Note: From the point u to point v nodes on the path includes u and v themselves

[ Input into the format]

A first act input integer n- , represents the number of nodes.

Next, n - 1 lines of 2 integers a and b , showing a node a and node b has one side connected between.

Next n lines, each line an integer, the i integer row wi represents the node i weights.

Next a line, is an integer Q , the total number of representations of operations.

Next q lines of an operation to "CHANGE ut" or "QMAX uv" or "QSUM uv" given form. 

For 100 % of the data, to ensure . 1 <= n-<= 30,000 , 0 <= Q <= 200000 ; middle operation guaranteed value of each node weights w in -30000 to 30000 between.

[ Outputs the format]

For each "QMAX" or "QSUM" operation, the output of each row represents an integer result output requirements.

[ Comp embodiment input into]

4

1 2

2 3

4 1

4 2 1 3

12

QMAX 3 4

QMAX 3 3

QMAX 3 2

QMAX 2 3

QSUM 3 4

QSUM 2 1

CHANGE 1 5

QMAX 3 4

CHANGE 3 6

QMAX 3 4

QMAX 2 4

QSUM 3 4

  

[ Comp embodiment outputs a]

4

1

2

2

10

6

5

6

5

16

#include<bits/stdc++.h>
#define maxn 30005
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r>>1)
#define lson ls,l,mid
#define rson rs,mid+1,r
using namespace std;
int n,q;
vector<int> v[maxn];int a[maxn];
int size[maxn],son[maxn],fa[maxn],top[maxn],dep[maxn],dfn[maxn],pos[maxn],cnt;
int sum[maxn<<2],mx[maxn<<2];
void Dfs(int rt){
    size[rt]=1;
    for(int i=0;i<v[rt].size();i++){
        int to=v[rt][i];
        if(!size[to]){
            fa[to]=rt;
            dep[to]=dep[rt]+1;
            Dfs(to);
            size[rt]+=size[to];
            if(size[to]>size[son[rt]]) son[rt]=to;
        }
    }
}
void Dfs(int rt,int tp){
    top[rt]=tp;
    dfn[++cnt]=rt;
    pos[rt]=cnt;
    if(son[rt]) Dfs(son[rt],tp);
    for(int i=0;i<v[rt].size();i++)
        if(!top[v[rt][i]]) Dfs(v[rt][i],v[rt][i]);
}
void Build(int rt,int l,int r){
    if(l==r){
        sum[rt]=mx[rt]=a[dfn[l]];
        return;
    }
    Build(lson);Build(rson);
    sum[rt]=sum[ls]+sum[rs];mx[rt]=max(mx[ls],mx[rs]);
}
void Add(int rt,int l,int r,int posx,int qx){
    if(l==r){
        sum[rt]=qx;
        mx[rt]=qx;
        return;
    }
    if(posx<=mid) Add(lson,posx,qx);
    else Add(rson,posx,qx);
    sum[rt]=sum[ls]+sum[rs];mx[rt]=max(mx[ls],mx[rs]);
}
int Sum(int rt,int l,int r,int s,int t){
    if(s>r||t<l) return 0;
    if(s<=l&&r<=t) return sum[rt];
    return Sum(lson,s,t)+Sum(rson,s,t);
}
int Max(int rt,int l,int r,int s,int t){
    if(s>r||t<l) return -maxn;
    if(s<=l&&r<=t) return mx[rt];
    return max(Max(lson,s,t),Max(rson,s,t));
}
int lca(int x,int y,bool opt){
    int res;
    if(opt) res=0;
    else res=-maxn;
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        if(opt) res+=Sum(1,1,n,pos[top[x]],pos[x]);
        else res=max(res,Max(1,1,n,pos[top[x]],pos[x]));
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    if(opt) res+=Sum(1,1,n,pos[x],pos[y]);
        else res=max(res,Max(1,1,n,pos[x],pos[y]));
    return res;
}
int main()
{
    freopen("bzoj_1036.in","r",stdin);
    freopen("bzoj_1036.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y;scanf("%d%d",&x,&y);
        v[x].push_back(y);v[y].push_back(x);
    }    
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    Dfs(1);Dfs(1,1);Build(1,1,n);
    scanf("%d",&q);
    while(q--){
        string s;cin>>s;
        if(s[0]=='C'){
            int x,v;
            scanf("%d%d",&x,&v);
            Add(1,1,n,pos[x],v);
        }
        if(s[1]=='M'){
            int x,y;scanf("%d%d",&x,&y);
            printf("%d\n",lca(x,y,0));
        }
        if(s[1]=='S'){
            int x,y;scanf("%d%d",&x,&y);
            printf("%d\n",lca(x,y,1));
        }
    }
    return 0;
}

Alas programming is not easy ah

Exam when he was wrong on two lines of the 0 burst! ! ! !

 

 

 

 

That dfn pos arrays and arrays

When entering the segment tree from the outside to use pos

Call out from the tree line time to use dfn

Be sure to remember! !

 

There is also a place that is out of bounds if Max function return value must be a case of a negative infinity otherwise would have been but that does not return a 0 to a maximum of bug yet is a negative number and you cross the line still have to be flexible ah!

Come practice your just fine

Isuzu (◍ ° ∇ ° ◍) No゙

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11325407.html