zoukankan      html  css  js  c++  java
  • 3223: Tyvj 1729 文艺平衡树

    Description

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

    Input

    第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
    接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

    Output

    输出一行n个数字,表示原始序列经过m次变换后的结果

    Sample Input

    5 3

    1 3

    1 3

    1 4

    Sample Output

    4 3 2 1 5

    HINT

    N,M<=100000

    好久没写,水一发splay。。。。。。

      1 const
      2     maxn=100100;
      3 type
      4     node=record
      5       son:array[0..1]of longint;
      6       a,fa,size:longint;
      7       sw:boolean;
      8     end;
      9 var
     10     f:array[0..maxn]of node;
     11     n,m,root:longint;
     12 
     13 function build(l,r,ff:longint):longint;
     14 var
     15     mid:longint;
     16 begin
     17     mid:=(l+r)>>1;
     18     f[mid].fa:=ff;
     19     f[mid].size:=r-l+1;
     20     if mid>l then f[mid].son[0]:=build(l,mid-1,mid);
     21     if (mid>1) and (mid<n+2) then f[mid].a:=mid-1;
     22     if mid<r then f[mid].son[1]:=build(mid+1,r,mid);
     23     exit(mid);
     24 end;
     25 
     26 procedure swap(var x,y:longint);
     27 var
     28     t:longint;
     29 begin
     30     t:=x;x:=y;y:=t;
     31 end;
     32 
     33 procedure new(x:longint);
     34 begin
     35     with f[x] do
     36       begin
     37         size:=f[son[0]].size+f[son[1]].size+1;
     38         if sw then
     39         begin
     40           swap(son[0],son[1]);
     41           f[son[0]].sw:=not f[son[0]].sw;
     42           f[son[1]].sw:=not f[son[1]].sw;
     43           sw:=not sw;
     44         end;
     45       end;
     46 end;
     47 
     48 function wh(x:longint):longint;
     49 begin
     50     if f[f[x].fa].son[0]=x then exit(0);
     51     exit(1);
     52 end;
     53 
     54 procedure rotate(x,w:longint);
     55 var
     56     y:longint;
     57 begin
     58     y:=f[x].fa;
     59     f[y].son[w]:=f[x].son[w xor 1];
     60     if f[x].son[w xor 1]<>0 then f[f[x].son[w xor 1]].fa:=y;
     61     f[x].son[w xor 1]:=y;
     62     if root=y then root:=x
     63     else f[f[y].fa].son[wh(y)]:=x;
     64     f[x].fa:=f[y].fa;
     65     f[y].fa:=x;
     66     new(y);
     67 end;
     68 
     69 procedure splay(x,z:longint);
     70 var
     71     y:longint;
     72 begin
     73     while f[x].fa<>z do
     74       begin
     75         y:=f[x].fa;
     76         if f[y].fa<>z then
     77           begin
     78             if wh(x)=wh(y) then rotate(y,wh(y))
     79             else rotate(x,wh(x));
     80           end;
     81         rotate(x,wh(x));
     82       end;
     83     new(x);
     84 end;
     85 
     86 function find(k:longint):longint;
     87 begin
     88     find:=root;
     89     while true do
     90       begin
     91         new(find);
     92         if k=f[f[find].son[0]].size+1 then exit(find);
     93         if k<=f[f[find].son[0]].size then find:=f[find].son[0]
     94         else
     95           begin
     96             dec(k,f[f[find].son[0]].size+1);
     97             find:=f[find].son[1];
     98           end;
     99       end;
    100 end;
    101 
    102 var
    103     aa:array[0..maxn]of longint;
    104     tot:longint;
    105 
    106 procedure dfs(x:longint);
    107 begin
    108     new(x);
    109     with f[x] do
    110       begin
    111         if son[0]<>0 then dfs(son[0]);
    112         inc(tot);
    113         aa[tot]:=a;
    114         if son[1]<>0 then dfs(son[1]);
    115       end;
    116 end;
    117 
    118 procedure main;
    119 var
    120     i,l,r:longint;
    121 begin
    122     read(n,m);
    123     root:=build(1,n+2,0);
    124     for i:=1 to m do
    125       begin
    126         read(l,r);
    127         splay(find(l),0);
    128         splay(find(r+2),root);
    129         f[f[f[root].son[1]].son[0]].sw:=not f[f[f[root].son[1]].son[0]].sw;
    130       end;
    131     splay(find(1),0);
    132     splay(find(n+2),root);
    133     dfs(f[f[root].son[1]].son[0]);
    134     for i:=1 to n-1 do
    135       write(aa[i],' ');
    136     write(aa[n]);
    137 end;
    138 
    139 begin
    140     main;
    141 end.
    View Code
  • 相关阅读:
    集群技术
    Docker Swarm(一)
    服务器集群
    生产环境swarm集群规划和管理
    集群的分类
    Arcengine C#开发源码
    BIOS设置中开启cpu睿频功能
    aida64怎么用?aida64最详细的使用教程
    SQL Server2019最大并行度
    IIS 之 在IIS7、IIS7.5中应用程序池最优配置方案
  • 原文地址:https://www.cnblogs.com/Randolph87/p/3741327.html
Copyright © 2011-2022 走看看