zoukankan      html  css  js  c++  java
  • BZOJ1455: 罗马游戏

    可并堆模板??又敲了一次,一遍过,很好。

     1 #include<string.h>
     2 #include<stdlib.h>
     3 #include<stdio.h>
     4 #include<math.h>
     5 //#include<assert.h>
     6 #include<algorithm>
     7 //#include<iostream>
     8 using namespace std;
     9 
    10 int n,m;
    11 #define maxn 1000011
    12 struct leftist
    13 {
    14     struct Node
    15     {
    16         int ls,rs,v,dis;
    17     }a[maxn];
    18     leftist() {a[0].dis=-1;}
    19     int merge(int x,int y)
    20     {
    21         if (!x || !y) return x^y;
    22         if (a[x].v>a[y].v) {int t=x; x=y; y=t;}
    23         a[x].rs=merge(a[x].rs,y);
    24         if (a[a[x].ls].dis<a[a[x].rs].dis) {int t=a[x].ls; a[x].ls=a[x].rs; a[x].rs=t;}
    25         a[x].dis=a[a[x].rs].dis+1;
    26         return x;
    27     }
    28     void push(int id,int &root,int val)
    29     {
    30         a[id].v=val; a[id].ls=a[id].rs=a[id].dis=0;
    31         root=merge(root,id);
    32     }
    33     void pop(int &root) {root=merge(a[root].ls,a[root].rs);}
    34     int top(int root) {return a[root].v;}
    35 }q;
    36 
    37 int root[maxn]; bool die[maxn];
    38 int find(int x) {return x==root[x]?x:(root[x]=find(root[x]));}
    39 int main()
    40 {
    41     scanf("%d",&n);
    42     for (int i=1,x;i<=n;i++) scanf("%d",&x),q.push(i,root[i],x);
    43     scanf("%d",&m);
    44     char c; int x,y;
    45     while (m--)
    46     {
    47         while ((c=getchar())!='K' && c!='M');
    48         if (c=='K')
    49         {
    50             scanf("%d",&x);
    51             if (die[x]) puts("0");
    52             else
    53             {
    54                 x=find(x); die[x]=1; printf("%d
    ",q.top(x));
    55                 q.pop(root[x]); root[root[x]]=root[x];
    56             }
    57         }
    58         else
    59         {
    60             scanf("%d%d",&x,&y);
    61             if (die[x] || die[y]) continue;
    62             x=find(x),y=find(y);
    63             if (x==y) continue;
    64             root[x]=root[y]=q.merge(root[x],root[y]);
    65         }
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    成为优秀程序员的101条建议(3)
    shell字符串的用法
    Centos yum国内源及配置含义
    go自动补全
    shell中空格的使用;空格替换;通配符
    shell自动补全功能:bash和zsh;zsh启动优化
    Mac下的命令行自动补全功能
    mac环境下intellij的自定义配置文件位置
    vim中delete(backspace)键不能向左删除
    一个性能较好的JVM参数配置
  • 原文地址:https://www.cnblogs.com/Blue233333/p/8266745.html
Copyright © 2011-2022 走看看