HDU 5692 Segment Tree

Source: Internet
Author: User

Click to open link

Test instructions: Chinese title

Idea: This is the first time to write such a line segment tree, personal feeling is a routine has not done before, but thought what ingenious, will a tree to transform into a line tree really powerful, first will each node below all the number of DFS serial number, and will their left and right number down, that is, my Code L and R, The Val record is the root node 0 to the current location of the cost, after the achievement is very good operation, the update will find the current node's son and their own representative of the interval, and then update the interval, note that the topic Update a node value into Y, why we update a section, because the node of our segment tree is saved to the root node 0 , then as a Father node, its value changed, will naturally affect its son to the root of the cost of the node, and change the same, so with the interval update to complete, the query is the same, must go to the X node, then to the X node is not there will be a greater cost, and this cost of the point must go through the X node, So we directly query the X node and all of its son's maximum value can PS: tree-shaped structure and line segment tree perfect combination, to practice well

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <vector> #include <stdio.h> #include < string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using namespace Std;typedef Long Long ll;const ll Inf=0x3f3f3f3f3f3f3f3f;const int Maxn=100010;vector<int>g[maxn];ll MAX1[MAXN*4],LAZY[MAXN    *4],val[maxn];int l[maxn],r[maxn],cost[maxn];int idx;void dfs (int u,int f,ll vv) {l[u]=++idx;        for (unsigned int i=0;i<g[u].size (); i++) {int v=g[u][i];        if (v==f) continue;    DFS (V,u, (ll) COST[V]+VV);    } R[u]=idx; VAL[L[U]]=VV;} void pushup (int node) {Max1[node]=max (max1[node<<1],max1[node<<1|1]);}        void pushdown (int node) {if (Lazy[node]) {lazy[node<<1]+=lazy[node];        lazy[node<<1|1]+=lazy[node];        max1[node<<1]+=lazy[node];        max1[node<<1|1]+=lazy[node];    lazy[node]=0; }}void buildtree (int le,int ri,int node) {if (Le==ri) {Max1[node]=vAl[le];    return;    } int t= (LE+RI) >>1;    Buildtree (le,t,node<<1);    Buildtree (t+1,ri,node<<1|1); Pushup (node);}        void update (int l,int r,int x,int le,int ri,int node) {if (l<=le&&ri<=r) {lazy[node]+= (ll) x;        max1[node]+= (ll) x;    return;    } pushdown (node);    int t= (LE+RI) >>1;    if (l<=t) update (L,R,X,LE,T,NODE&LT;&LT;1);    if (r>t) update (L,R,X,T+1,RI,NODE&LT;&LT;1|1); Pushup (node);}    ll query (int l,int r,int le,int ri,int node) {if (l<=le&&ri<=r) return Max1[node];    Pushdown (node);    ll Ans=-inf;    int t= (LE+RI) >>1;    if (l<=t) Ans=max (Ans,query (l,r,le,t,node<<1));    if (r>t) Ans=max (Ans,query (l,r,t+1,ri,node<<1|1)); return ans;}    int main () {int t,cas=1,n,m,a,b,c;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (int i=0;i<maxn;i++) g[i].clear (); for (int i=0;i<n-1;i++) {scanf ("%d%d", &a,&b);            a++;b++;            G[a].push_back (b);        G[b].push_back (a);        } for (int i=1;i<=n;i++) scanf ("%d", &cost[i]);        Idx=0;dfs (1,0, (LL) cost[1]);        Buildtree (1,n,1);        memset (lazy,0,sizeof (lazy));        printf ("Case #%d:\n", cas++);            while (m--) {scanf ("%d", &a);                if (a==0) {scanf ("%d%d", &b,&c); b++;                Update (l[b],r[b],c-cost[b],1,n,1);            Cost[b]=c;                }else{scanf ("%d", &b); b++;                ll Answe=query (l[b],r[b],1,n,1);            printf ("%i64d\n", answe); }}} return 0;}

HDU 5692 Segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.