URAL topic 1553. Caves and tunnels (Link Cut Tree Modify the point right, ask for the maximum between two points)

Source: Internet
Author: User

1553. Caves and Tunnelstime limit:3.0 second
Memory limit:64 MB
After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to the IT using remote controlled robots. It is found out that there exists exactly one route between every pair of caves. But then scientists faced a particular problem. Sometimes in the caves faint explosions happen. They cause emission of radioactive isotopes and increase radiation level in the cave. Unfortunately robots don ' t stand radiation well. But for the purposes they must travel from one cave to another. So scientists placed sensors on every cave to monitor radiation level in the caves. And now every time they move robots they want to know the maximal radiation level the robot'll has to face during its R Elocation. So they asked-write a program, that'll solve their problem. Inputthe first line of the input contains one integer N(1≤ N≤100000)-the number of caves. Next N? 1 lines describe tunnels. Each of these lines contains a pair of integers ai , bI (1≤ ai , bI N) specifying the numbers of the caves connected by corresponding tunnel. The next line have an integer Q( Q≤100000) representing the number of queries. The QQueries follow on a, each. Every query has a form of " C U V", where Cis a single character and can be either ' I ' or ' G ' representing the type of the query (quotes for clarity only). In the case of a ' I ' query radiation level in U-th Cave (1≤ UN) is incremented by V(0≤ V≤10000). In the case of a ' G ' query your program must output the maximal level of radiation on the the-the-the-between Uand V(1≤ U, VNIncreases of radiation (' I ' queries) specified before current query. It is assumed this initially radiation level are 0 in all caves, and it never decreases with time (because Isotopes ' half-l Ife time is much larger than the time of observations). Outputfor every ' G ' query output one line containing the maximal radiation level by itself. Sample
input Output
1013
problem Source:Novosibirsk SU Contest. Petrozavodsk training camp, September 2007

Tags: Data Structures (

)

Instantaneous seconds ~ ~

The main idea: a tree, start each point weights of 0, after Q operation, I a b,a point right plus b,g a B query from a to B to go to the maximum weight value

AC Code


#include <stdio.h> #include <string.h> #include <queue> #include <iostream> #define In  F 0x7fffffff #define MAX (A, B) (A&GT;B?A:B) using namespace std;      int vis[100050];          struct LCT {int bef[100050],pre[100050],next[100050][2],key[100050],val[100050];              void Init () {memset (pre,0,sizeof (pre));        memset (Next,0,sizeof (next)); Memset (Key,0,sizeof (key));     Val[0]=-inf;       } void Pushup (int x) {Val[x]=max (Key[x],max (val[next[x][1]],val[next[x][0]));              } void rotate (int x,int kind) {int y,z;              Y=PRE[X];              Z=pre[y];              Next[y][!kind]=next[x][kind];              Pre[next[x][kind]]=y;              Next[z][next[z][1]==y]=x;              Pre[x]=z;              Next[x][kind]=y;              Pre[y]=x;        Pushup (y);              } void Splay (int x) {int rt; For (Rt=x;pre[rt];rt=pre[rt]);                  if (X!=RT) {BEF[X]=BEF[RT];                  bef[rt]=0;                          while (Pre[x]) {if (next[pre[x]][0]==x) {                      Rotate (x,1);                  } else rotate (x,0);            } pushup (x);              }} void access (int x) {int fa;                  for (Fa=0;x;x=bef[x]) {splay (x);                  pre[next[x][1]]=0;                  Bef[next[x][1]]=x;                  NEXT[X][1]=FA;                  Pre[fa]=x;                  bef[fa]=0;                Fa=x;            Pushup (x);          }} void Change (int x,int y) {key[x]+=y;      Splay (x);          } int query (int x,int y) {access (Y);            for (Y=0;x;x=bef[x]) {  Splay (x);              if (!bef[x]) {return max (Key[x],max (Val[next[x][1]],val[y]));              } pre[next[x][1]]=0;              Bef[next[x][1]]=x;              Next[x][1]=y;              Pre[y]=x;              bef[y]=0;              Y=x;          Pushup (x);      } return 0;  }}LCT;  struct S {int u,v,next;  }edge[200020<<1];  int head[200020],cnt;      void Add (int u,int v) {edge[cnt].u=u;      Edge[cnt].v=v;      Edge[cnt].next=head[u];  head[u]=cnt++;              } void BFs (int u) {queue<int>q;              memset (vis,0,sizeof (VIS));              Vis[u]=1;              Q.push (U);                      while (!q.empty ()) {U=q.front ();                      Q.pop ();                              for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;              if (!vis[v])                {lct.bef[v]=u;                                      Vis[v]=1;                              Q.push (v); }}}} int main () {int n;while (scanf ("%d", &n)!=eof) {int I=1;memset (head,-1,size Of (head)), Cnt=0;for (i=1;i<n;i++) {int u,v;scanf ("%d%d", &u,&v); add (u,v); add (v,u);} int q;scanf ("%d", &q), Lct.init (), BFS (1), while (q--) {char str[2];int u,v;scanf ("%s%d%d", str,&u,&v); if ( str[0]== ' I ') lct.change (u,v) elseprintf ("%d\n", Lct.query (U,v));}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL topic 1553. Caves and tunnels (Link Cut Tree Modify the point right, ask for the maximum between two points)

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.