poj 2892Tunnel Warfare

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 6965   Accepted: 2863

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

//树桩数组二分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int MAX=0xfffffff;
const int mx=50050;
int c[mx*4];
int n,m;
stack<int>S;
int lowbit(int x)
{
    return x&(-x);
}
void update(int pos,int val)
{
    while(pos<=n)
    {
        c[pos]+=val;
        pos+=lowbit(pos);
    }
}
int  sum(int end)
{
    int sum=0;
    while(end>0)
    {
        sum+=c[end];
        end-=lowbit(end);
    }
    return sum;
}
int binl(int l,int r,int cur)
{
    int ans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(sum(cur)-sum(mid-1)==cur-mid+1)   //sum(cur)-sum(mid)不包括a[mid];
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    return ans;
}
int binr(int l,int r,int cur)
{
    int ans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(sum(mid)-sum(cur-1)==mid-cur+1)   //sum(cur)-sum(mid)不包括a[mid];
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    return ans;
}
int main( )
{
   // freopen("1.txt","r",stdin);
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            update(i,1);
        while(!S.empty())  S.pop();
        while(m--)
        {
            char s[2];
            scanf("%s",s);
            if(s[0]=='D')
            {
                int x;
                scanf("%d",&x);
                update(x,-1);
                S.push(x);
            }
            else if(s[0]=='R')
            {
                update(S.top(),1);
                S.pop();
            }
            else if(s[0]=='Q')
            {
                int x;
                scanf("%d",&x);
                if(sum(x)-sum(x-1)==0)   printf("0\n");
                else
                {
                    int cur=x;
                    int l=binl(1,x,cur);
                    int r=binr(x,n,cur);
                    printf("%d\n",r-l+1);
                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(树状数组)