bzoj1984 month under "Mao Jingxiu"

Source: Internet
Author: User

Description Caterpillar after a timely deformation, eventually escaped a robbery, left the vegetable mother's vegetable garden. Caterpillar through the mountains and rivers, through hardships, and finally came to a small Shaoxing campus. Climb, crawl, climb, crawl ~ ~ Caterpillar climbed to a small "Mao Jingxiu" below, found that the tree has his favorite Mao Maogo ~ ~ ~ "Mao Jingxiu" on there are N nodes and N-1 branches, but there is no Mao Maogo on the node, Mao Maogo are long on the branches. But this "Mao Jingxiu" has magical magic, he can change the number of hairs on the branches of the tree:? Change K W: Changes the number of hairs on section K branches to W.? Cover u v w: Change the number of hairs on the branch between node U and node v to W.? Add U v w: increases the number of hairs on the branch between node U and Node v by W. Since the caterpillar is greedy, he will have the following questions:? Max U V: asks the maximum number of hairs on the branch between the node U and Node v. Input first line a positive integer n. Next N-1 line, three positive integers per line ui,vi and WI, section i+1 describes article I branches. Indicates that the branch I is connected to the node UI and Node VI, the branches have wi a fluffy fruit. Next is the action and the inquiry, ending with "Stop". Output for each of the Caterpillar's inquiry operations, outputs an answer. Sample Input4
1 2 8
1 3 7
3 4 9
Max 2 4
Cover 2 4 5
ADD 1 4 10
Change 1 16
Max 2 4
Stop
Sample Output9
16

"Data Range"
1<=n<=100,000, the number of operations + queries does not exceed 100,000.
Ensure that the number of hairs on all branches will not exceed 10^9 at any time. Tree chain split second questionI'm not going to love you anymore. Line Tree write I'm going to kneel this question is Benquan also to turn into a point right processing
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime> #define LL long long#define INF 0x7ffffff#define pa pair<int,int> #define Pi 3.1415926535897932384626433832795028841971#define N 200010using namespace Std;inline ll read () {ll X=0,f=1;char ch=getc    Har (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,cnt,cnt2,tt,lca,x,y,z;struct edge{int to,next,v,rnk;} e[2*n];struct segtree{int l,r,mx,tag,cover;} Tree[4*n];int query[n],v[n];int head[n];bool mrk[n];int depth[n],son[n],fa[n][21];int Place[N],pplace[N],belong[N];    char ch[100];inline void ins (int u,int v,int w,int rnk) {e[++cnt].to=v;    E[cnt].v=w;    E[cnt].next=head[u];    E[CNT].RNK=RNK; head[u]=cnt;} inline void Insert (int u,int V,int w,int rnk) {ins (u,v,w,rnk); Ins (V,U,W,RNK);}    inline void dfs1 (int x,int dep) {if (mrk[x]) return;    Mrk[x]=1;depth[x]=dep;son[x]=1;    for (int i=1;i<=20;i++) fa[x][i]=fa[fa[x][i-1]][i-1];        for (int i=head[x];i;i=e[i].next) if (!mrk[e[i].to]) {fa[e[i].to][0]=x;        query[e[i].rnk]=e[i].to;        V[E[I].TO]=E[I].V;        DFS1 (e[i].to,dep+1);      SON[X]+=SON[E[I].TO];    }}inline void dfs2 (int x,int chain) {place[x]=++tt;pplace[tt]=x;    Belong[x]=chain;    int mx=-inf,res=-1; for (int i=head[x];i;i=e[i].next) if (E[i].to!=fa[x][0]) {if (son[e[i].to]>mx) {mx=            SON[E[I].TO];        res=e[i].to;    }} if (Res==-1) return;    DFS2 (Res,chain); for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa[x][0]&&e[i].to!=res) DFS2 (e[i].to,e[i].to);}    inline int LCA (int a,int b) {if (Depth[a]<depth[b]) swap (A, b);    int res=depth[a]-depth[b]; for (int i=0;i<=20;i++) IF (Res & (1<<i)) a=fa[a][i];        for (int i=20;i>=0;i--) if (Fa[a][i]!=fa[b][i]) {a=fa[a][i];     B=fa[b][i];     } if (a==b) return A; return fa[a][0];} inline void pushdown (int k) {int l=tree[k].l,r=tree[k].r,tag=tree[k].tag,cov=tree[k].cover;tree[k].tag=0;tree[k]. Cover=-1;if (l==r) return;if (cov!=-1) {tree[k<<1].mx=tree[k<<1|1].mx=cov;tree[k<<1].tag=tree[k <<1|1].tag=0;tree[k<<1].cover=tree[k<<1|1].cover=cov;} if (tag) {tree[k<<1].mx+=tag;if (tree[k<<1].cover!=-1) Tree[k<<1].cover+=tag;else tree[k<<1 ].tag+=tag;tree[k<<1|1].mx+=tag;if (tree[k<<1|1].cover!=-1) Tree[k<<1|1].cover+=tag;else tree[k <<1|1].tag+=tag;}} inline void update (int k) {Tree[k].mx=max (tree[k<<1].mx,tree[k<<1|1].mx);}    inline void buildtree (int now,int l,int r) {tree[now].l=l;tree[now].r=r;    Tree[now].cover=-1;        if (l==r) {tree[now].mx=v[pplace[l]];    Return } intMid= (l+r) >>1;    Buildtree (Now<<1,l,mid);    Buildtree (NOW&LT;&LT;1|1,MID+1,R); Update (now);}    inline int ask_in_tree (int now,int x,int y) {pushdown (now);    int L=TREE[NOW].L,R=TREE[NOW].R;    if (l==x&&r==y) return tree[now].mx;    int mid= (L+R) >>1;    if (Y<=mid) return Ask_in_tree (now<<1,x,y);    else if (X>mid) return Ask_in_tree (now<<1|1,x,y); Return Max (Ask_in_tree (Now<<1,x,mid), Ask_in_tree (Now<<1|1,mid+1,y));}    inline void change_in_tree (int now,int x,int y,int dat) {pushdown (now);    int L=TREE[NOW].L,R=TREE[NOW].R;        if (l==x&&r==y) {tree[now].mx=dat;        Tree[now].cover=dat;        tree[now].tag=0;    Return    } int mid= (L+R) >>1;    if (y<=mid) Change_in_tree (Now<<1,x,y,dat);    else if (x>mid) Change_in_tree (Now<<1|1,x,y,dat);        else {change_in_tree (now<<1,x,mid,dat);    Change_in_tree (Now<<1|1,mid+1,y,dat); } update (now);} inline void ADd_in_tree (int now,int x,int y,int dat) {pushdown (now);    int L=TREE[NOW].L,R=TREE[NOW].R;    if (l==x&&r==y) {tree[now].tag+=dat;        Tree[now].mx+=dat;    Return    } int mid= (L+R) >>1;    if (y<=mid) Add_in_tree (Now<<1,x,y,dat);    else if (x>mid) Add_in_tree (Now<<1|1,x,y,dat);        else {add_in_tree (now<<1,x,mid,dat);    Add_in_tree (Now<<1|1,mid+1,y,dat); } update (now);}    inline int Ask (int from,int to) {int l,r,mx=0;        while (Belong[from]!=belong[to]) {L=place[belong[from]];        R=place[from];        Mx=max (Mx,ask_in_tree (1,l,r));    FROM=FA[BELONG[FROM]][0];    } L=place[to]+1;r=place[from];    if (l>r) return MX;    Mx=max (Mx,ask_in_tree (1,l,r)); return MX;}    inline void Add (int from,int to,int dat) {int l,r;        while (Belong[from]!=belong[to]) {L=place[belong[from]];        R=place[from];        Add_in_tree (1,l,r,dat);    FROM=FA[BELONG[FROM]][0]; } L=placE[to]+1;r=place[from];    if (l>r) return; Add_in_tree (1,l,r,dat);}    inline void change (int from,int to,int dat) {int l,r;        while (Belong[from]!=belong[to]) {L=place[belong[from]];        R=place[from];        Change_in_tree (1,l,r,dat);    FROM=FA[BELONG[FROM]][0];    } L=place[to]+1;r=place[from];    if (l>r) return; Change_in_tree (1,l,r,dat);}    int main () {n=read ();        for (int i=1;i<n;i++) {x=read ();        Y=read ();        Z=read ();    Insert (X,Y,Z,++CNT2);    } DFS1 (a);    DFS2 (a);    Buildtree (1,1,n);        while (1) {scanf ("%s", ch);            if (ch[1]== ' t ') break;//stop if (ch[1]== ' a ')//max {x=read (); Y=read ();            Lca=lca (x, y);        printf ("%d\n", Max (Ask (X,LCA), ask (Y,LCA)));            } if (ch[1]== ' o ')//cover {x=read (); Y=read (); Z=read ();            Lca=lca (x, y);        Change (x,lca,z), change (y,lca,z);        } if (ch[1]== ' d ')//add {    X=read (); Y=read (); Z=read ();            Lca=lca (x, y);        Add (x,lca,z); add (y,lca,z);            } if (ch[1]== ' h ')//change {x=read (); Y=read ();        Change_in_tree (1,place[query[x]],place[query[x]],y); }} return 0;}

  

bzoj1984 month under "Mao Jingxiu"

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.