Tunnel Warfare(STL简单使用)

来源:互联网 发布:淘宝女装好店铺推荐 编辑:程序博客网 时间:2024/06/06 03:26

Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9741 Accepted Submission(s): 3808

Problem 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 (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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

//这道题应该使用线段树来做的为了偷懒就使用STL库里面的set容器和stack 堆栈来做了 主要的思路都写在代码中了 就不在做过多的解释了

#include<iostream>#include<stdio.h>#include<string.h>#include<stack>#include<set>using namespace std;int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        set<int> mn;//未摧毁的村庄的下标        set<int> pq;//已经摧毁的村庄的下标        stack<int> nn;//把已摧毁的村庄按照摧毁的顺序放入堆栈        for(int i =1;i<=n;i++)            mn.insert(i);        char t[2];        int x;        while(m--)        {            scanf("%s",t);            if(t[0]=='D')//如果是摧毁 那么就放入堆栈里面 然后将mn里面的删除 将其加入pq            {                scanf("%d",&x);                nn.push(x);                mn.erase(x);                pq.insert(x);            }            else if(t[0]=='Q')//如果查询的话            {                scanf("%d",&x);                if(pq.find(x)!=pq.end())//判断这个点是否已经被摧毁 如果被摧毁 就直接输出0 并continue                {                    printf("0\n");                    continue;                }                int r = n;//假设未摧毁的村庄的左边界                int l = 1;//未摧毁的村庄的右边界                set<int>::iterator it = pq.lower_bound(x);//low_bound(x)函数作用是查找容器里面第一个大于等于x的值                if(it!=pq.end())//在pq里面找到了第一个比x大的元素                {                    r = *it -1;//则直接更新r的值                    if(it!=pq.begin())//如果pq里面的元素不都比x大                        l = *(--it) + 1;//从比x小的里面选出来一个最大的                }                else                {                    if(pq.size())                    l = *(--pq.end()) + 1 ;//pq数组都比x小                }                printf("%d\n",r - l +1);            }            else            {                if(nn.size())                {                    int t = nn.top();                    nn.pop();                    mn.insert(t);                    pq.erase(t);                }            }            //cout<<m<<endl;        }    }    return 0;}
原创粉丝点击