"BZOJ-1455" Roman games can be stacked (left-leaning tree)

Source: Internet
Author: User

1455: Roman Games time limit:5 Sec Memory limit:64 MB
submit:1355 solved:561
[Submit] [Status] [Discuss] Description Roman Emperors liked to play killing games. There are n men in his army, and everyone is a separate regiment. A plane geometry test was recently held and everyone got a score. The emperor liked plane geometry, and he sniffed at those who scored very low. He decided to play such a game. It can send two kinds of commands: 1. Merger (I, J). Merge the regiment where I resides and the group where J is located into a regiment. If I, J has a person who is dead, then ignore the command. 2. Kill (i). Kill the person with the lowest score in the group where I was located. If I were dead, this command would be ignored. The emperor wanted him to publish a kill order, and the following generals reported the scores of the people who had been killed. (If this command is ignored, then 0 points) input the first line an integer n (1<=n<=1000000). n indicates the number of soldiers, and m represents the total number of commands. The second row n integers, where number I indicates the score of the soldier numbered I. (Fractions are integers between [0..10000]) The third line, an integer m (1<=m<=100000), 3+i Line describes the article I command. The command is in the following two forms: 1. M I J 2. K ioutput If the command is kill, the corresponding output is the score of the homicide. (If this person does not exist, output 0) Sample Input5
100 90 66) 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4
Sample Output10
100
0
66
Hintsourcesolution

And pile up naked questions.

Three properties:

[Property 1] The key value of the node is less than or equal to the key value of its left and right child nodes.

[Property 2] The distance of the left child node of the node is not less than the distance of the right child node.

[Property 3] The left child node of the node is also a left-biased tree.

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 1000010intd[maxn],son[maxn][2],a[maxn],n,fa[maxn],m;BOOLDEAD[MAXN];intFindintx) {    if(fa[x]==x)returnXElse returnfa[x]=find (Fa[x]);}intMergeintXinty) {    if(!x)returny; if(!y)returnx; if(a[x]>A[y]) swap (x, y); son[x][1]=merge (son[x][1],y); if(d[son[x][1]]>d[son[x][0]]) Swap (son[x][0],son[x][1]); D[X]=d[son[x][1]]+1; returnx;}intMain () {n=read ();  for(intI=1; i<=n; i++) a[i]=read ();  for(intI=1; i<=n; i++) fa[i]=i; M=read (); d[0]=-1;  for(intI=1; i<=m; i++)        {            Charopt[5];intx, y; scanf"%s", opt); if(opt[0]=='M') {x=read (), Y=read ();intFx=find (x), fy=Find (y), RT; if(dead[x]| | Dead[y])Continue; if(fx!=FY) RT=merge (fx,fy), fa[fx]=fa[fy]=RT; }            if(opt[0]=='K') {x=read ();intfx=find (x); if(Dead[x]) {Puts ("0");Continue;} DEAD[FX]=1; printf"%d\n", A[fx]); FA[FX]=merge (son[fx][0],son[fx][1]); FA[FA[FX]]=FA[FX]; }        }    return 0;}

"BZOJ-1455" Roman games can be stacked (left-leaning 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.