Tunnel Warfare HDU

来源:互联网 发布:剑灵男灵剑士捏脸数据 编辑:程序博客网 时间:2024/06/06 09:51
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 (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.
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



题意  n个村庄,m次操作,n个村庄相连  。D 代表破坏此村庄   Q 为询问 此村庄相连的村庄(包括本身)有几个  R为修复上一个被破坏的村庄。。


思路 :  遇到Q时,一定是从这个村庄开始往左到哪里,往右到哪里,计算这个长度。      

             可以用set 先把0与n+1让入set中,遇到D时,把这个村庄放入set中,R时把上一个村庄从set中删除。

            Q时在set中查找到大于这个村庄的值l,找到前一个小于这个村庄的值r,l-r-1为此区间的值,若正好在set中找到此村庄,则证明这个村庄被破坏,输出0;


#include<iostream>#include<cstring>#include<algorithm>#include<stack>#include<set>using namespace std;int n,m;int main(){    char s;    int l;    int t;    while(cin>>n>>m)    {        l=0;        stack<int> ss;        set<int> aa;        aa.insert(0);        aa.insert(n+1);        for(int i=0;i<m;i++)        {            cin>>s;            if(s=='D')            {                cin>>t;                ss.push(t);                aa.insert(t);            }            if(s=='R')            {                int a=ss.top();                ss.pop();                aa.erase(a);            }            if(s=='Q')            {                cin>>t;                int f=0;                int l,r;                set<int>::iterator ite;                for(ite=aa.begin();ite!=aa.end();ite++)                {                    if(*ite==t)                    {                        f=1;                        cout<<"0"<<endl;                        break;                    }                    if(*ite>t)                    {                        l=*ite;                        ite--;                        r=*ite;                        break;                    }                }                if(f==0)                {                    cout<<l-r-1<<endl;                }            }        }    }    return 0;}


原创粉丝点击