POJ-2892-Tunnel Warfare(線段樹)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integers n and m (nm ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. R: The village destroyed last was rebuilt.

Output

Output the answer to each of the Army commanders’ request in order on a separate line.

Sample Input

7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024

Hint

An illustration of the sample input:

      OOOOOOOD 3   OOXOOOOD 6   OOXOOXOD 5   OOXOXXOR     OOXOOXOR     OOXOOOO

Source

POJ Monthly--2006.07.30, updog


思路:線段樹維護每個區間的左右兩端分別到中間的最大連續區間的大小,注意左右子樹可能存在最大連續區間大小就等於該節點的區間大小的情況。

#include <stdio.h>struct{int l,r;//分別記錄左右兩邊開始的最大連續區間大小}node[200000];int stk[50000],top,n,m;void build(int idx,int s,int e){    node[idx].l=node[idx].r=e-s+1;    if(s!=e)    {        int mid=(s+e)>>1;        build(idx<<1,s,mid);        build(idx<<1|1,mid+1,e);    }}void update(int idx,int s,int e,int pos,int val){    if(s==e) node[idx].l=node[idx].r=val;//val為0代表摧毀村莊,為1代表修複村莊    else    {        int mid=(s+e)>>1;        if(pos<=mid) update(idx<<1,s,mid,pos,val);        else update(idx<<1|1,mid+1,e,pos,val);        if(node[idx<<1].l==mid-s+1) node[idx].l=mid-s+1+node[idx<<1|1].l;//左子樹滿了        else node[idx].l=node[idx<<1].l;        if(node[idx<<1|1].r==e-mid) node[idx].r=e-mid+node[idx<<1].r;//右子樹滿了        else node[idx].r=node[idx<<1|1].r;    }}int query(int idx,int s,int e,int pos){    if(s==e) return 0;//如果到了葉子節點就肯定為0    int mid=(s+e)>>1;    if(pos<=mid)    {        if(pos>=mid-node[idx<<1].r+1) return node[idx<<1].r+node[idx<<1|1].l;//如果被包含在左子樹的右區間還要加上右子樹的左區間        else return query(idx<<1,s,mid,pos);    }    else    {        if(pos<=mid+node[idx<<1|1].l) return node[idx<<1|1].l+node[idx<<1].r;//如果被包含在右子樹的左區間還要加上左子樹的右區間        else return query(idx<<1|1,mid+1,e,pos);    }}int main(){    int t;    char s[5];    while(~scanf("%d%d",&n,&m))    {        build(1,1,n);        top=0;        while(m--)        {            scanf("%s",s);            if(s[0]=='D')            {                scanf("%d",&t);                stk[top++]=t;                update(1,1,n,t,0);            }            else if(s[0]=='R')            {                update(1,1,n,stk[--top],1);            }            else            {                scanf("%d",&t);                printf("%d\n",query(1,1,n,t));            }        }    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.