hdu1540 Tunnel Warfare(线段树)

Tunnel Warfare

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


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

题意:D表示破坏地道,R表示修复上一个被破坏的地道,Q表示查询包括x的最长区间。
分析:用三个变量记录左边连续区间,右边连续区间和最大连续区间;不知道怎么说,看代码吧。如果还看不懂就看这里吧,写的挺详细的 详解
Ps:写代码一定要仔细啊,因为一个符号问题我查了近两天,看的我眼睛那个花啊 抓狂

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
const int MAXN = 50000+10;

struct node
{
    int l,r;
    int ls,rs,ms;//ls为左边最大连续区间,rs为右,ms为区间内最大连续长度
}t[MAXN<<2];
int n,m;
int s[MAXN],top;//模拟栈

void build(int x, int y, int num)
{
    t[num].l = x;
    t[num].r = y;
    t[num].ls = t[num].rs = t[num].ms = y-x+1;
    if(x == y) return ;
    int mid = (x+y)>>1;
    build(x, mid, num<<1);
    build(mid+1, y, num<<1|1);
}

void update(int x, int num, int ok)
{
    if(t[num].l == t[num].r)
    {
        if(ok) //修复
            t[num].ls = t[num].rs = t[num].ms = 1;
        else //破坏
            t[num].ls = t[num].rs = t[num].ms = 0;
        return ;
    }
    int mid = (t[num].l+t[num].r)>>1;
    if(x <= mid)
        update(x, num<<1, ok);
    else
        update(x, num<<1|1, ok);
    t[num].ls = t[num<<1].ls;
    t[num].rs = t[num<<1|1].rs;
    t[num].ms = max(max(t[num<<1].ms, t[num<<1|1].ms), t[num<<1].rs+t[num<<1|1].ls);
    if(t[num<<1].ls == t[num<<1].r-t[num<<1].l+1)//如果左子树满了,父亲左区间要加上右孩子的左区间
        t[num].ls += t[num<<1|1].ls;
    if(t[num<<1|1].rs == t[num<<1|1].r-t[num<<1|1].l+1)//同理
        t[num].rs += t[num<<1].rs;
}

int query(int x, int num)
{
    if(t[num].l==t[num].r || t[num].ms==0 || t[num].ms==t[num].r-t[num].l+1)
        return t[num].ms;
    int mid = (t[num].l+t[num].r)>>1;
    if(x <= mid)
    {
        if(x >= t[num<<1].r-t[num<<1].rs+1)//因为x<=mid,看左子树,t[num<<1].r-t[num<<1].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
            return query(x, num<<1)+query(mid+1, num<<1|1);
        else
            return query(x, num<<1);//如果不在左子树的右边界区间内,则只需要看左子树
    }
    else
    {
        if(x <= t[num<<1|1].l+t[num<<1|1].ls-1)//同理
            return query(x, num<<1|1)+query(mid, num<<1);
        else
            return query(x, num<<1|1);
    }
}

int main()
{
    char ch[2];//其实这里我也不懂为什么用单个的字符就不行,会RE
    int x;
    while(scanf("%d%d",&n,&m)==2)
    {
        top = 0;
        build(1, n, 1);
        while(m--)
        {
            //getchar();
            scanf("%s",ch);
            if(ch[0] == 'D')
            {
                scanf("%d",&x);
                s[top++] = x;
                update(x, 1, 0);
            }
            else if(ch[0] == 'Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(x, 1));
            }
            else
            {
                if(top > 0)
                {
                    x = s[--top];
                    update(x, 1, 1);
                }
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值