hdu 1540 Tunnel Warfare

线段树-区间最大字串(正解)

https://blog.csdn.net/u013480600/article/details/22509467

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14427    Accepted Submission(s): 5713


 

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

 

 

Source

POJ Monthly

 

 

Recommend

LL

 

 

Statistic | Submit | Discuss | Note

正解:暴力模拟

题意:

D代表破坏村庄,

R代表修复最后被破坏的那个村庄,

Q代表询问包括x在内的最大连续区间是多少

直接上模拟,见代码

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
int main()
{
    //freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int per;
        set<int > S;
        S.insert(0);//查找时使用
        S.insert(n+1);
        stack<int> ST;
        for(int i=0; i<m; i++)
        {
            int x;
            char str;
            scanf(" %c",&str);
            if(str=='D')
            {
                scanf("%d",&x);
                ST.push(x);
                S.insert(x);
            }
            else if(str=='Q')
            {
                scanf("%d",&x);
                if(S.find(x)!=S.end())//表示被破坏
                    puts("0");
                else
                {
                    set<int>::iterator it1= S.upper_bound(x);//第一个大于的,当前村庄右边那个被破坏的
                    set<int>::iterator it2=it1;
                    it2--;//小于的位置,因为当前村庄没有被破坏,所以减一就是被破坏的左边的那个
                    cout<<(*it1-*it2-1)<<endl;
                }
            }
            else if(!ST.empty())
            {
                S.erase(ST.top());//删除最后一个被破坏的
                ST.pop();
            }
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值