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

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total 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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     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 50050
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值