HDOJ 1540 && POJ 2892 —— 线段树

84 篇文章 0 订阅
Language:
Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 6445 Accepted: 2648

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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

Source

比较基础的一道线段树
题意是给你若干个顺序相连的村庄,你有3种命令:D x :把x村庄干掉,R:重建上一个被干掉的村庄,Q x:输出与x村庄直接或间接相连的村庄数。

显然我们用线段树去维护3个域即可,suml该节点向左的最大长度,sumr该节点向右的最大长度,sum该段的长度。

村庄有3种状态,用cover记录: 1:全部村庄存在; 0:全部村庄不存在; -1:有的村庄存在,有的不存在。

区间合并时有两种情况:

1:mid - l <= 左儿子的sumr  2:l - mid + 1 <= 右儿子的suml

两种情况皆为左儿子的sumr + 右儿子的suml 。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid, r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 50000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
//#pragma comment(linker, "/STACK:102400000,102400000")
struct t
{
    int l , r , suml , sumr , cover , sum;
}t[MAXN << 2];
stack <int>s;
void init()
{
    clr(t , 0);
}
void build(int l , int r , int rt)
{
    t[rt].l = l ,t[rt].r = r;
    t[rt].suml = t[rt].sumr = t[rt].sum = r - l;
    t[rt].cover = 1;
    if(l == r - 1)return ;
    int mid = (l + r) >> 1;
    build(lson) ,build(rson);
}
void PushUp(int rt)
{
    if(t[rt << 1].cover == 0 && t[rt << 1 | 1].cover == 0)t[rt].cover = 0;
    else if(t[rt << 1].cover == 1 && t[rt << 1 | 1].cover == 1)t[rt].cover = 1;
    else
    {
        t[rt].cover = -1;
    }
    t[rt].suml = t[rt << 1].suml + (t[rt << 1].cover == 1 ? t[rt << 1 | 1].suml : 0);
    t[rt].sumr = t[rt << 1 | 1].sumr + (t[rt << 1 | 1].cover == 1 ? t[rt << 1].sumr : 0);
    t[rt].sum = max(t[rt].suml , max(t[rt].sumr  , t[rt << 1].sumr + t[rt << 1 | 1].suml));
}
void update(int L , int R ,  int add , int rt)
{
    if(t[rt].l >= L && t[rt].r <= R)
    {
        t[rt].cover = t[rt].sum = t[rt].suml = t[rt].sumr = add;
        return ;
    }
    if(t[rt].l == t[rt].r - 1)return ;
    int mid = (t[rt].l + t[rt].r) >> 1;
    if(L <= mid)update(L , R , add , rt << 1);
    if(mid < R)update(L , R , add , rt << 1 | 1);
    PushUp(rt);
}
int query(int rt , int l)
{
    if(t[rt].cover == 1)return t[rt].sum;
    if(t[rt].cover == 0)return 0;
    int mid = (t[rt].l + t[rt].r) >> 1;
    if(l < mid)
    {
        if(mid - l <= t[rt << 1].sumr)
        {
            return t[rt << 1].sumr + t[rt << 1 | 1].suml;
        }
        return query(rt  << 1 , l);
    }
    else
    {
        if(l - mid + 1 <= t[rt << 1 | 1].suml)
        {
            return t[rt << 1 | 1].suml + t[rt << 1].sumr;
        }
        return query(rt << 1 | 1 , l);
    }
}
char str[5];
int n , m;
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // Online_Judge
    while(~scanf("%d%d" ,  &n , &m))
    {
        init();
//        outstars
        build(0 , n , 1);
//        outstars
        while(!s.empty())s.pop();
        while(m--)
        {
            scanf("%s"  ,str);
            int x;
            if(str[0] == 'D')
            {
                scanf("%d" , &x);
                update(x - 1 , x , 0 , 1);
                s.push(x);
            }
            else if(str[0] == 'R')
            {
                if(s.empty())break;
                x = s.top();
                s.pop();
                update(x - 1 , x , 1, 1);
            }
            else
            {
                scanf("%d" , &x);
                int ans = query(1 , x - 1);
                printf("%d\n" , ans);
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值