HDU - 1540 Tunnel Warfare (线段树 区间合并)

题目链接: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): 9539    Accepted Submission(s): 3727


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
 


区间合并的题 用三个变量分别记录从区间最左端的最长区间个数 最右端的最长区间个数 和 区间内最长区间个数

每次回缩判断即可

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 60010
struct xjy
{
    int left;
    int right;
    int lmax;
    int rmax;
    int mmax;
};
xjy tree[maxn<<2];
int a[maxn];
int ans=0;

void build(int i,int left ,int right)
{

    {
        tree[i].left=left;
        tree[i].right=right;
        tree[i].lmax=tree[i].rmax=tree[i].mmax=right-left+1;
    }
    if(left!=right)
    {
        int mid=(left+right)>>1;
        build(i<<1,left,mid);
        build (i<<1|1,mid+1,right);
    }
}

void updateregion(int i,int left,int right,int val)
{
    if(tree[i].left==left&&tree[i].right==right)
    {
        tree[i].lmax=tree[i].rmax=tree[i].mmax=val;
        return;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        updateregion(i<<1,left,right,val);
    else if(left>mid)
        updateregion(i<<1|1,left,right,val);
    else
    {
        updateregion(i<<1,left,mid,val);
        updateregion(i<<1|1,mid+1,right,val);
    }
    tree[i].lmax=tree[i<<1].lmax;
    tree[i].rmax=tree[i<<1|1].rmax;
    tree[i].mmax=max(max(tree[i<<1].mmax,tree[i<<1|1].mmax),tree[i<<1].rmax+tree[i<<1|1].lmax);
    if(tree[i<<1].lmax==(tree[i<<1].right-tree[i<<1].left+1))
        tree[i].lmax+=tree[i<<1|1].lmax;
    if(tree[i<<1|1].rmax==(tree[i<<1|1].right-tree[i<<1|1].left+1))
        tree[i].rmax+=tree[i<<1].rmax;
}

int query(int i,int t)
{
    if(tree[i].left==tree[i].right||tree[i].mmax==(tree[i].right-tree[i].left+1)||!tree[i].mmax)
    {
        return tree[i].mmax;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(t<=mid)
    {
        if(t>=tree[i<<1].right-tree[i<<1].rmax+1)
            return query(i<<1,t)+query(i<<1|1,mid+1);
        else
            return query(i<<1,t);
    }
    else
    {
        if(t<=tree[i<<1|1].left+tree[i<<1|1].lmax-1)
            return query(i<<1|1,t)+query(i<<1,mid);
        else
            return query(i<<1|1,t);
    }
}

int main()
{
    int n,m;
    int t;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<maxn;i++)
        {
            tree[i].left=0;
            tree[i].lmax=0;
            tree[i].mmax=0;
            tree[i].right=0;
            tree[i].rmax=0;
        }
        build(1,1,n);
        stack<int > ss;
        for(int i=1;i<=m;i++)
        {
            char s;
            scanf(" %c",&s);
            if(s=='D')
            {
                int mid;
                scanf("%d",&mid);
                updateregion(1,mid,mid,0);
                ss.push(mid);
                //show(1,1,n);
                //printf("\n");
            }
            else if(s=='R')
            {
                int mid;
                mid=ss.top();
                ss.pop();
                updateregion(1,mid,mid,1);
                //show(1,1,10);
            }
            else
            {
                int mid;
                scanf("%d",&mid);
                ans=0;
                printf("%d\n",query(1,mid));
            }
        }
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值