码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2892

时间:2014-08-29 11:10:47      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   ar   for   

Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 6972   Accepted: 2864

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

Source

AC代码:
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<stack>
#include<cstring>
using namespace std;
int n,m;
int c[50005],d[50005];
stack <int> st;
int lowbit(int x){
    return x&-x;
}
int sum(int x){
    int ret=0;
    while(x>=1){
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(int x,int val){
    while(x<=n){
        c[x]+=val;
        x+=lowbit(x);
    }
}
int sol(int x){
    int left,right,mid;
    int ans=n+1;
    left=0; right=n+1;        //注意这里的ans,left和right的初始值
    while(left<=right){
        mid=(left+right)>>1;
        if(sum(mid)>=x){
            ans=mid;
            right=mid-1;
        }
        else
            left=mid+1;
    }
    return ans;
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        while(!st.empty())
            st.pop();
        while(m--){
            char s[2];
            scanf("%s",s);
            if(s[0]=='D'){
                int x; scanf("%d",&x);
                if(d[x]){
                    continue;
                }
                else{
                    st.push(x);
                    d[x]=1;
                    add(x,1);
                }
            }
            else if(s[0]=='Q'){
                int x; scanf("%d",&x);
                if(d[x]){
                    printf("0\n");
                }
                else{
                    int sm=sum(x);
                    int left,right;
                    left=sol(sm);
                    right=sol(sm+1);
                    //printf("%d %d\n",left,right);
                    printf("%d\n",right-left-1);
                }
            }
            else{
                if(st.empty()){
                    continue;
                }
                else{
                    d[st.top()]=0;
                    add(st.top(),-1);
                    st.pop();
                }
            }
        }
    }
    return 0;
}

poj 2892

标签:des   style   http   color   os   io   strong   ar   for   

原文地址:http://blog.csdn.net/my_acm/article/details/38904551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!