BZOJ1230: [Usaco2008 Nov]lites 開關燈

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   for   

1230: [Usaco2008 Nov]lites 開關燈Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1074  Solved: 544
[Submit][Status]Description

Farmer John嘗試通過和奶牛們玩益智玩具來保持他的奶牛們思維敏捷. 其中一個大型玩具是牛欄中的燈. N (2 <= N <= 100,000) 頭奶牛中的每一頭被連續的編號為1..N, 站在一個彩色的燈下面.剛到傍晚的時候, 所有的燈都是關閉的. 奶牛們通過N個按鈕來控制燈的開關; 按第i個按鈕可以改變第i個燈的狀態.奶牛們執行M (1 <= M <= 100,000)條指令, 每個指令都是兩個整數中的一個(0 <= 指令號 <= 1). 第1種指令(用0表示)包含兩個數字S_i和E_i (1 <= S_i <= E_i <= N), 它們表示起始開關和終止開關. 奶牛們只需要把從S_i到E_i之間的按鈕都按一次, 就可以完成這個指令. 第2種指令(用1表示)同樣包含兩個數字S_i和E_i (1 <= S_i <= E_i <= N), 不過這種指令是詢問從S_i到E_i之間的燈有多少是亮著的. 協助FJ確保他的奶牛們可以得到正確的答案.

Input

* 第 1 行: 用空格隔開的兩個整數N和M

 * 第 2..M+1 行: 每行表示一個操作, 有三個用空格分開的整數: 指令號, S_i 和 E_i

Output

第 1..詢問的次數 行: 對於每一次詢問, 輸出詢問的結果.

Sample Input4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

輸入解釋:
一共有4盞燈; 5個指令. 下面是執行的情況:
燈
1 2 3 4
Init: O O O O O = 關 * = 開
0 1 2 -> * * O O 改變燈 1 和 2 的狀態
0 2 4 -> * O * *
1 2 3 -> 1 輸出在2..3的範圍內有多少燈是亮的
0 2 4 -> * * O O 改變燈 2 ,3 和 4 的狀態
1 1 4 -> 2 輸出在1..4的範圍內有多少燈是亮的




Sample Output1
2
HINT Source

Gold

題解:簡單的線段樹,區間打標記、求和而已。。。真是有點水的嚴重啊。。。代碼:
 1 uses math; 2 const maxn=100000+1000; 3 type node=record 4      l,r,mid,sum0,sum1:longint; 5      mark:boolean; 6      end; 7 var t:array[0..4*maxn] of node; 8     i,n,m,x,y,ch:longint; 9     procedure swap(var x,y:longint);10      var t:longint;11          begin12            t:=x;x:=y;y:=t;13          end;14 15 procedure build(k,x,y:longint);16  begin17    with t[k] do18     begin19       l:=x;r:=y;mid:=(l+r)>>1;sum0:=r-l+1;sum1:=0;mark:=false;20       if l=r then exit;21       build(k<<1,l,mid);build(k<<1+1,mid+1,r);22     end;23  end;24 procedure rever(k:longint);25  begin26    with t[k] do27     begin28       mark:=not(mark);29       swap(sum0,sum1);30     end;31  end;32 33 procedure pushdown(k:longint);34  begin35    with t[k] do36     begin37       if not(mark) then exit;38       rever(k<<1);rever(k<<1+1);39       mark:=false;40     end;41  end;42 procedure pushup(k:longint);43  begin44    t[k].sum0:=t[k<<1].sum0+t[k<<1+1].sum0;45    t[k].sum1:=t[k<<1].sum1+t[k<<1+1].sum1;46  end;47 48 procedure change(k,x,y:longint);49  begin50    with t[k] do51     begin52       if (l=x) and (r=y) then53        begin54          rever(k);exit;55        end;56       pushdown(k);57       if y<=mid then change(k<<1,x,y)58       else if x>mid then change(k<<1+1,x,y)59       else60        begin61          change(k<<1,x,mid);change(k<<1+1,mid+1,y);62        end;63       pushup(k);64     end;65  end;66 function query(k,x,y:longint):longint;67   begin68     with t[k] do69      begin70        if (l=x) and (r=y) then exit(sum1);71        pushdown(k);72        if y<=mid then exit(query(k<<1,x,y))73        else if x>mid then exit(query(k<<1+1,x,y))74        else exit(query(k<<1,x,mid)+query(k<<1+1,mid+1,y));75      end;76   end;77 78 begin79   assign(input,‘input.txt‘);assign(output,‘output.txt‘);80   reset(input);rewrite(output);81   readln(n,m);build(1,1,n);82   for i:=1 to m do83    begin84      readln(ch,x,y);85      case ch of86      0:change(1,x,y);87      1:writeln(query(1,x,y));88      end;89    end;90   close(input);close(output);91 end.                         
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.