码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

时间:2015-08-10 14:54:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:数据结构   线段树   poj   hdu   合并   

Tunnel Warfare

                                                            Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                                       链接:hdu 1540        POJ 2892

 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

题意:

         给定N个点,点的编号是从1 ~ N,M次‘Q’或‘D或‘R’的操作,“D  x”表示的是破坏这个点x,“R”是修复之前破坏的点,“Q x”表示询问点x所在连续区间的长度。假定最初每个点都是好的。

分析:

         首先对操作进行分析:可以把D操作看成把区间在点x出截断,R操作是把x左右【包括点x】进行合并。每次合并两个区间 [a,b] , [c,d] 的时候,得到的新的区间[a,d] 的最大连续区间长度为:

        Len(a,d)  =  max{ Len(a,b),Len(c,d) ,End(b) + Begin(c) };  <==有一点分治的思想

         End(b)表示以b结尾的最大连续区间长度,Begin(c)表示以c开头的最大连续区间长度,这里我们可以知道,合并两个区间,并不是简单的区间加减,而是还要保持一个以区间第一个元素开始的最大连续区间长度以及 以区间最后一个元素结尾的最大连续区间长度。

         那么,现在问题就比较好办了,线段树每个节点包含三个信息,分别是以区间第一个元素开始的最大连续区间长度ln,以区间最后一个元素结尾的最大连续区间长度rn以及区间最大连续长度mn

接下来,便是Q操作了,询问点x 所在连续区间的长度。在递归左儿子节点的时候,注意一下,x是否在以左儿子区间最后一个元素结尾长度为左儿子rn的范围里面,如果在,那么x所在的连续区间便有可能包含右儿子的一部分,递归右儿子的时候,同理!

/****************************>>>>HEADFILES<<<<****************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
#define fst             first
#define snd             second
#define root            1,N,1
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
#define PB(a)           push_back(a)
#define MP(a,b)         make_pair(a,b)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef __int64         LL;
const int INF = 0x3f3f3f3f;
/****************************>>>>SEPARATOR<<<<****************************/
const int maxn = 50000 + 5;
int N, M;
struct Node
{
    int ln, rn, mn;
} segtree[maxn << 4];
inline void PushUp(const int& rt, const int& l, const int& r)
{
    segtree[rt].ln = segtree[rt << 1].ln;
    segtree[rt].rn = segtree[rt << 1 | 1].rn;
    segtree[rt].mn = max(segtree[rt << 1].rn + segtree[rt << 1 | 1].ln, 
                         max(segtree[rt << 1].mn, segtree[rt << 1 | 1].mn));
    int mid = (l + r) >> 1;
    if(segtree[rt << 1].mn == mid - l + 1) segtree[rt].ln += segtree[rt << 1 | 1].ln;
    if(segtree[rt << 1 | 1].mn == r - (mid + 1) + 1) segtree[rt].rn += segtree[rt << 1].rn;
}
void Build(int l, int r, int rt)
{
    segtree[rt].ln = segtree[rt].rn = segtree[rt].mn = r - l + 1;
    if(l == r)
    {
        return ;
    }
    int mid = (l + r) >> 1;
    Build(lson);
    Build(rson);
}
void Update(int pos, int val, int l, int r, int rt)
{
    if(l == r)
    {
        segtree[rt].ln = segtree[rt].rn = segtree[rt].mn = val;
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid)
        Update(pos, val, lson);
    else
        Update(pos, val, rson);
    PushUp(rt, l, r);
}
int Query(int pos, int l, int r, int rt)
{
    if(r - l + 1 == segtree[rt].mn || r  == l || segtree[rt].mn == 0)
    {
        return segtree[rt].mn;
    }
    int mid = (l + r) >> 1, ret = 0;
    if(pos <= mid)
    {
        if(pos >= mid - segtree[rt << 1].rn + 1)
            ret = Query(pos, lson) + Query(mid + 1, mid + 1, r, rt << 1 | 1);
        else ret = Query(pos, lson);
    }
    else
    {
        if(pos <= (mid + 1) + segtree[rt << 1 | 1].ln - 1)
            ret = Query(pos, rson) + Query(mid, l, mid, rt << 1);
        else ret = Query(pos, rson);
    }
    return ret;
}
int destroy[maxn];
int main()
{
    //FIN;
    while(~scanf("%d %d", &N, &M))
    {
        Build(root);
        char Op[5];
        int x, cnt = 0;
        for(int i = 0; i < M; i++)
        {
            scanf("%s", Op);
            if(Op[0] == 'Q')
            {
                scanf("%d", &x);
                printf("%d\n", Query(x, root));
            }
            else if(Op[0] == 'D')
            {
                scanf("%d", &x);
                Update(x, 0, root);
                destroy[cnt++] = x;
            }
            else
            {
                x = destroy[--cnt];
                Update(x, 1, root);
            }
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

标签:数据结构   线段树   poj   hdu   合并   

原文地址:http://blog.csdn.net/acmore_xiong/article/details/47397127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!