"COGS1672" "SPOJ375" Qtree

Source: Internet
Author: User

This is my first edge right profile.
COGS and SPOJ a little bit different is that there are no more sets of data, the nature is still the same
I wrote the cogs that actually changed it to spoj AC = =
(But I'm not going to Spoj (╯‵-′) ╯︵┻━┻)
"Title description"

One day the night of the computer room, countless people in the MC struggle ...

We all know how important the mineral is to MC, but as the mines dig less, the Warriors have to go farther to dig the mines, but this will take a considerable amount of time on the way, leading to low mining efficiency.

CJJ proposed to fix a railway, and everyone agreed.

We were all assigned some tasks by CH:

ZJMFRANK2012 is responsible for drawing a map of the mine, this map includes home (of course, this is also a mine, after all, not the home hollowed out we will not go), and countless mines, so we should be able to think of this is a non-circular diagram, that is, a tree.

Digital_t and Cstdio are responsible for paving the railway. So there is nothing for them, and the two can work.

At this time song526210932 and RMB suddenly found that some of the mines will brush strange, and the number of strange will change. As the main mining force, they want to know which section of the road from one mine to another will be most difficult ... (The difficulty value is indicated by the number of ZJM deaths).
"Input Format"

The first line of the input file has an integer n, which represents the number of mines. The number of mines is 1 to N.

Next N-1 line has three integer a,b,c, which represents a road between the mine of No. I and J, and the difficult value of this road at the initial time is C.

Next, there are several lines, each line is "Change I ti" or "QUERY a B", the former represents the first road (the road according to the order from 1 to M numbered) the difficulty value of TI, which represents the query A to B through the path of the maximum difficulty value.

The input data ends with a line of "done".
"Output Format"

For each "QUERY" operation, output a single positive integer, which is the maximum difficulty value.
"Sample Input"

3

1 2 1

2 3 2

QUERY 1 2

Change 1 3

QUERY 1 2

Done
"Sample Output"

1

3
Prompted

For 60% of data, 1 ≤ N ≤ 50

For 100% of data, 1 ≤ N ≤ 10000,1 ≤ C ≤ 1000000,1 ≤ Number of operations ≤ 100000
Source

Adapted from Gdfrwmy from Spoj 375 Qtree

Data by Cstdio

Benquan chain cut slightly egg ache
I use the Benquan of the greater depth of the two endpoints on each side to inherit this side of the edge with a point.
Finally, the Query_max function is changed slightly based on the statistic count of the tree.
(direct violence with LCA would be a stupid mistake I didn't even notice at first. Thanks to Zky Seniors reminded Quq)

//ac Code by Creationaugust#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define MAXINT 0x7fffffff#define MAXN 100010#define Lchild Rt<<1,l,mid#define Rchild Rt<<1|1,mid+1,r#define LN rt<<1#define RN Rt<<1|1using namespace STD;intWintVAL[MAXN];intsize[maxn],deep[maxn],chain[maxn],num[maxn],fa[maxn][ -];BOOLVIS[MAXN];intTOP,TP;intb;intU,v;intN,t;intX[MAXN],Y[MAXN],Z[MAXN];Charch[6];structedge{Edge *next;intValintto;} *prev[maxn],e[maxn*2];structseg{intMAXN;intLintR;} tree[maxn*4];inline voidInsertintUintVintW) {e[++top].to=v;    E[top].val=w;    E[top].next=prev[u]; Prev[u]=&e[top];}inline voidDFS1 (intx) {size[x]=1; vis[x]=1; for(intI=1; i<= -; i++) {if(deep[x]< (1<<i)) Break; fa[x][i]=fa[fa[x][i-1]][i-1]; } for(Edge *i=prev[x];i;i=i->next) {intt=i->to;if(Vis[t])Continue; deep[t]=deep[x]+1; fa[t][0]=x;        DFS1 (t);    SIZE[X]+=SIZE[T]; }}inline voidDFS2 (intXintlast) {chain[x]=last; NUM[X]=++TP;intt=0; for(Edge *i=prev[x];i;i=i->next)if(Deep[i->to]>deep[x]&&size[t]<size[i->to]) t=i->to;if(!t)return; DFS2 (T,last); for(Edge *i=prev[x];i;i=i->next)if(deep[i->to]>deep[x]&&i->to!=t) DFS2 (i->to,i->to);}inline voidPUSH_UP (intRT) {Tree[rt].maxn=max (TREE[LN].MAXN,TREE[RN].MAXN);}inline voidBuildintrt=1,intL=1,intr=n) {tree[rt].l=l; Tree[rt].r=r;if(L==R)return;intMid= (l+r) >>1;    Build (Lchild); Build (Rchild);}inline intLcaintAintb) {if(Deep[a]<deep[b]) swap (A, b);intT=DEEP[A]-DEEP[B]; for(intI=0; i<= -; i++)if(t& (1<<i)) A=fa[a][i]; for(intI= -; i>=0; i--)if(Fa[a][i]!=fa[b][i])            {A=fa[a][i];        B=fa[b][i]; }if(a==b)returnAElse returnfa[a][0];}voidModifyintRtintLintRintW) {intL=tree[rt].l,r=tree[rt].r,mid= (l+r) >>1;if(L==L&AMP;&AMP;R==R) {tree[rt].maxn=w;return; }if(l>mid) modify (RN,L,R,W);Else    if(r<=mid) modify (LN,L,R,W);Else{Modify (LCHILD,W);    Modify (RCHILD,W); } push_up (RT);}voidModify (intAintBintW) { while(Chain[a]!=chain[b]) {Modify (1, num[chain[a]],num[a],w); a=fa[chain[a]][0]; } Modify (1, num[b],num[a],w);}inline intQuery_max (intRtintLintR) {intL=tree[rt].l,r=tree[rt].r,mid= (l+r) >>1;if(L==L&AMP;&AMP;R==R) {returnTREE[RT].MAXN; }if(L>mid)returnQuery_max (RN,L,R);Else    if(R<=mid)returnQuery_max (LN,L,R);Else        returnMax (Query_max (lchild), Query_max (Rchild));}inline intQuery_max (intAintb) {intRet=-maxint; while(Chain[a]!=chain[b]) {Ret=max (Ret,query_max (1, Num[chain[a]],num[a])); a=fa[chain[a]][0]; }if(num[b]+1<=num[a]) Ret=max (Ret,query_max (1, num[b]+1, Num[a]));returnRET;}intMain () {Freopen ("Qtree.in","R", stdin); Freopen ("Qtree.out","W", stdout);scanf("%d", &n); for(intI=1; i<n;i++) {scanf("%d%d%d", &u,&v,&w);            Insert (U,V,W);            Insert (V,U,W);            X[i]=u;            Y[i]=v;        Z[i]=w; } DFS1 (1); DFS2 (1,1); Build (); for(intI=1; i<n;i++) {if(Deep[x[i]]<deep[y[i]]) val[y[i]]=z[i];ElseVal[x[i]]=z[i]; } for(intI=1; i<=n;i++) Modify (1, Num[i],num[i],val[i]); while(1)        {scanf('%s ', ch);if(ch[1]==' H ')            {scanf("%d%d", &a,&b);if(Deep[x[a]]<deep[y[a]]) modify (1, num[y[a]],num[y[a]],b);ElseModify1, num[x[a]],num[x[a]],b); }Else            if(ch[1]==' U ')            {scanf("%d%d", &a,&b);intT=lca (A, b);printf("%d\n", Max (Query_max (a,t), Query_max (b,t))); }Else            if(ch[1]==' O ') Break; }}

"COGS1672" "SPOJ375" Qtree

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.