【線段樹區間合併】HDU1540-Tunnel Warfare

1、題目

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!php

Input

The first line of the input contains two positive integers n and m (n, m ≤ 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:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.html

Output

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

Sample Input

7 9ide

D 3ui

D 6spa

D 5rest

Q 4code

Q 5orm

Rhtm

Q 4

R

Q 4

Sample Output

1

0

2

4

順便附上原題連接→_→Problem-1540

2、題目分析

和普通的線段樹沒什麼太大差異,只是每一個區間加了兩個變量——lsum和rsum,分別表示這個區間左起向右連續「1」串的長度和右起向左連續「1」串的長度。在維護樹回溯時,一個區間的lsum等於其左區間的lsum,rsum等於其右區間的rsum。特別的,當一個區間左區間的lsum等於其左區間長度,即該區間左區間是一個連續的「1」串時,該區間lsum需額外加上其右區間的lsum;對於其rsum同理。

3、代碼實現

這題有個天坑,題目徹底沒提到,但這題有多組數據_(:з」∠)_

  1 #include<cstdio>
  2 #include<cstring>
  3 const int MAXN=5e4+10;
  4 int n,m;
  5 struct node
  6 {
  7     int l,r;
  8     int lsum,rsum;
  9 }tr[MAXN<<2];
 10 int stack[MAXN],top;
 11 void build(int x,int y,int i)
 12 {
 13     tr[i].l=x,tr[i].r=y;
 14     if(x==y)
 15     {
 16         tr[i].lsum=1;
 17         tr[i].rsum=1;
 18         return;
 19     }
 20     int mid=(tr[i].l+tr[i].r)>>1;
 21     build(x,mid,i<<1);
 22     build(mid+1,y,i<<1|1);
 23     tr[i].lsum=y-x+1,tr[i].rsum=y-x+1;
 24     return;
 25 }
 26 void update(int x,int i,int val)
 27 {
 28     if(tr[i].l==x&&tr[i].r==x)
 29     {
 30         tr[i].lsum=val;
 31         tr[i].rsum=val;
 32         return;
 33     }
 34     int mid=(tr[i].l+tr[i].r)>>1;
 35     if(x<=mid)
 36     {
 37         update(x,i<<1,val);
 38 
 39     }
 40     else 
 41     {
 42         update(x,i<<1|1,val);
 43     }
 44     tr[i].lsum=tr[i<<1].lsum;
 45     tr[i].rsum=tr[i<<1|1].rsum;
 46     if(tr[i<<1].lsum==tr[i<<1].r-tr[i<<1].l+1)tr[i].lsum+=tr[i<<1|1].lsum;
 47     if(tr[i<<1|1].rsum==tr[i<<1|1].r-tr[i<<1|1].l+1)tr[i].rsum+=tr[i<<1].rsum;
 48 }
 49 int query_left(int x,int i)
 50 {
 51     if(tr[i].r==x)
 52     {
 53         if(tr[i].rsum==tr[i].r-tr[i].l+1&&tr[i].l!=1)
 54         {
 55             return tr[i].rsum+query_left(tr[i].l-1,1);
 56         }
 57         return tr[i].rsum;
 58     }
 59     int mid=(tr[i].l+tr[i].r)>>1;
 60     if(x<=mid)
 61     {
 62         return query_left(x,i<<1);
 63     }
 64     else 
 65     {
 66         return query_left(x,i<<1|1);
 67     }
 68 }
 69 int query_right(int x,int i)
 70 {
 71 
 72     if(tr[i].l==x)
 73     {
 74         if(tr[i].lsum==tr[i].r-tr[i].l+1&&tr[i].r!=n)
 75         {
 76             return tr[i].lsum+query_right(tr[i].r+1,1);
 77         }
 78         return tr[i].lsum;
 79     }
 80     int mid=(tr[i].l+tr[i].r)>>1;
 81     if(x<=mid)
 82     {
 83         return query_right(x,i<<1);
 84     }
 85     else 
 86     {
 87         return query_right(x,i<<1|1);
 88     }
 89 }
 90 int main()
 91 {
 92     while(scanf("%d%d",&n,&m)!=EOF)
 93     {
 94         memset(tr,0,sizeof(tr));
 95         memset(stack,0,sizeof(stack));
 96         top=0;
 97         build(1,n,1);
 98         for(int i=1;i<=m;++i)
 99         {
100             char c;
101             int x;
102             scanf("\n%c",&c);
103             if(c=='D')
104             {
105                 scanf("%d",&x);
106                 stack[++top]=x;
107                 update(x,1,0);
108             }
109             else if(c=='R')
110             {
111                 update(stack[top--],1,1);
112             }
113             else
114             {
115                 scanf("%d",&x);
116                 int ans=query_left(x,1)+query_right(x,1)-1;
117                 if(ans<=0)printf("0\n");
118                 else printf("%d\n",ans);
119             }
120         }
121     }
122     return 0;
123 }
HDU1540-Tunnel Warfare

弱弱地說一句,本蒟蒻碼字也不容易,轉載請註明出處http://www.cnblogs.com/Maki-Nishikino/p/6230606.html

相關文章
相關標籤/搜索