HDOJ 1540 && POJ 2892 —— 线段树

来源:互联网 发布:英语软件哪个比较好 编辑:程序博客网 时间:2024/06/06 16:51
Language:
Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072KTotal 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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024

Hint

An illustration of the sample input:

      OOOOOOOD 3   OOXOOOOD 6   OOXOOXOD 5   OOXOXXOR     OOXOOXOR     OOXOOOO

Source

POJ Monthly--2006.07.30, updog

比较基础的一道线段树
题意是给你若干个顺序相连的村庄,你有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 1000000007typedef 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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 农村无证宅基地怎么办 身份证没磁怎么办护照 买安置房怎么办产权 安置房房东违约怎么办 安置房产权到期怎么办 安置房怎么办房产证吗 美甲后指甲长了怎么办 在菲律宾怎么办持枪证? 绝地求生打不准怎么办 身份证号码变更后社保怎么办 社保与身份不符怎么办 年龄改了学籍怎么办 结婚证信息错误怎么办 六级身份证过期怎么办 身份号泄露了怎么办 身体证信息泄露怎么办 手机号被泄漏了怎么办 姓名和电话泄露怎么办 个人身份证信息泄露怎么办 身份号码泄露了怎么办 我身份证泄露了怎么办 身份证信息泄漏了怎么办 无锡身份证丢了怎么办 人在外地怎么办身份证 欠空放公司不还怎么办 兼职要身份证照片怎么办 身份证刷不了磁怎么办 身份证不能刷了怎么办 身份证指纹错了怎么办 指纹手机丢了怎么办 异地办理临时身份证怎么办 杭州办理外地身份证怎么办 办理身份证没有户口本怎么办 2018身份证掉了怎么办 双户口注销社保怎么办 常用户口被注销怎么办 刚到厦门怎么办身份证 新疆身份证丢了怎么办 技能证书丢了怎么办 16岁拍身份证怎么办 16岁以下怎么办身份证