poj 2892 Tunnel Warfare(树状数组+二分)

来源:互联网 发布:科比0506赛季数据 编辑:程序博客网 时间:2024/04/30 18:59

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 7707 Accepted: 3181

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
题意:

三个操作:
D pos 将pos位置摧毁,让它和周围不相连。
Q pos 问和pos 相连的有多少个村庄。
R 修复最近摧毁的村庄。

思路:D和R用树状数组很好维护,关键在于Q如何查找?

我们可以做两次二分,一次从1~pos,一次从pos到n。  求得最远的满足sum(r)-sum(l)==r-l+1的点,然后两段距离加起来减一即可。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 50050int n,m;int a[N],c[N];int lowbit(int x){    return x&-x;}void add(int x,int v){    for(int i=x;i<=n;i+=lowbit(i))        c[i]+=v;}int sum(int x){    int ans=0;    for(int i=x;i;i-=lowbit(i))        ans+=c[i];    return ans;}int main(){    char op[5];   while(~scanf("%d %d",&n,&m))   {       memset(c,0,sizeof(c));       for(int i=1;i<=n;i++)          add(i,1);       int cnt=0,t;       while(m--)       {           scanf("%s",op);           if(op[0]=='D')           {               scanf("%d",&t);               add(t,-1);               a[cnt++]=t;           }           else if(op[0]=='R')               add(a[--cnt],1);           else           {               scanf("%d",&t);               if(sum(t)==sum(t-1))               {                   printf("0\n");                   continue;               }               int l=1,r=t,ans=0;               while(l<=r)               {                   int mid=(l+r)>>1;                   if(sum(t)-sum(mid-1)==t-mid+1)                       {                           ans=t-mid+1;                           r=mid-1;                       }                   else l=mid+1;               }               l=t,r=n;               int ans1=0;               while(l<=r)               {                   int mid=(l+r)>>1;                   if(sum(mid)-sum(t-1)==mid-t+1)                       {                           ans1=mid-t+1;                           l=mid+1;                       }                   else r=mid-1;               }               printf("%d\n",ans1+ans-1);           }       }   }   return 0;}


0 0
原创粉丝点击