WIKIOI 3243 區間翻轉

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   資料   

3243 區間翻轉 題目描述 Description

給出N個數,要求做M次區間翻轉(如1 2 3 4變成4 3 2 1),求出最後的序列

輸入描述 Input Description

第一行一個數N,下一行N個數表示原始序列,在下一行一個數M表示M次翻轉,之後的M行每行兩個數L,R表示將區間[L,R]翻轉。

輸出描述 Output Description

一行N個數 , 表示最終序列。

範例輸入 Sample Input

4

1 2 3 4

2

1 2

3 4

範例輸出 Sample Output

2 1 4 3

資料範圍及提示 Data Size & Hint

對於30%的資料滿足n<=100 , m <= 10000

對於100%的資料滿足n <= 150000 , m <= 150000

對於100%的資料滿足n為2的冪,且L = i * 2^j + 1 , R = (i + 1) * 2^j

題解:

要不要這麼卡時啊。。。不加inline就不能過?

代碼:

  1 {$inline on}  2 const maxn=150000+100;  3 var s,id,fa,a:array[0..maxn] of longint;  4     rev:array[0..maxn] of boolean;  5     c:array[0..maxn,0..1] of longint;  6     i,n,m,rt,x,y:longint;  7     procedure swap(var x,y:longint);inline;  8      var t:longint;  9      begin 10      t:=x;x:=y;y:=t; 11      end; 12  13 procedure pushup(x:longint);inline; 14  begin 15    s[x]:=s[c[x,0]]+s[c[x,1]]+1; 16  end; 17 procedure pushdown(x:longint);inline; 18  var l,r:longint; 19  begin 20    l:=c[x,0];r:=c[x,1]; 21    if rev[x] then 22     begin 23       swap(c[x,0],c[x,1]); 24       rev[l]:=not(rev[l]); 25       rev[r]:=not(rev[r]); 26       rev[x]:=false; 27     end; 28  end; 29 procedure rotate(x:longint;var k:Longint);inline; 30  var l,r,y,z:longint; 31  begin 32    y:=fa[x];z:=fa[y]; 33    if c[y,0]=x then l:=0 else l:=1;r:=l xor 1; 34    if y=k then k:=x else c[z,ord(c[z,1]=y)]:=x; 35    fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y; 36    c[y,l]:=c[x,r];c[x,r]:=y; 37    pushup(y);pushup(x); 38  end; 39 procedure splay(x:longint;var k:longint);inline; 40  var y,z:longint; 41  begin 42    while x<>k do 43     begin 44       y:=fa[x];z:=fa[y]; 45       if y<>k then 46         begin 47           if (c[z,0]=y) xor (c[y,0]=x) then rotate(x,k) 48           else rotate(y,k); 49         end; 50       rotate(x,k); 51     end; 52  end; 53 function find(x,rank:longint):longint;inline; 54  var l,r:longint; 55  begin 56  pushdown(x);l:=c[x,0];r:=c[x,1]; 57  if s[l]+1=rank then exit(x) 58  else if s[l]>=rank then exit(find(l,rank)) 59  else exit(find(r,rank-s[l]-1)); 60  end; 61 procedure rever(l,r:longint);inline; 62  var x,y:longint; 63  begin 64  x:=find(rt,l);y:=find(rt,r+2); 65  splay(x,rt);splay(y,c[x,1]); 66  rev[c[y,0]]:=not(rev[c[y,0]]); 67  end; 68 procedure build(l,r,f:longint);inline; 69  var mid,now,last:longint; 70  begin 71  if l>r then exit; 72  now:=id[l];last:=id[f]; 73  if l=r then 74     begin 75       fa[now]:=last;s[now]:=1; 76       c[last,ord(l>f)]:=now; 77       exit; 78     end; 79  mid:=(l+r)>>1; 80  build(l,mid-1,mid);build(mid+1,r,mid); 81  now:=id[mid];pushup(mid); 82  fa[now]:=last; 83  c[last,ord(mid>f)]:=now; 84  end; 85 procedure init; 86  begin 87    readln(n); 88    for i:=1 to n do read(a[i]);readln; 89    readln(m); 90    for i:=1 to n+2 do id[i]:=i; 91    build(1,n+2,0);rt:=(n+3)>>1; 92  end; 93 procedure main; 94  begin 95    for i:=1 to m do 96     begin 97       readln(x,y); 98       rever(x,y); 99     end;100    for i:=2 to n+1 do write(a[find(rt,i)-1],‘ ‘);101  end;102 begin103   assign(input,‘input.txt‘);assign(output,‘output.txt‘);104   reset(input);rewrite(output);105   init;106   main;107   close(input);close(output);108 end.109                                  
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.