hdu 1540 线段树

来源:互联网 发布:九月份非农数据 编辑:程序博客网 时间:2024/06/05 11:31
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540
题意:有n个点依次链接,先给出三种操作:
  1. 删除某个点
  2. 修复某个点
  3. 查询与某个点相连的点的个数
解法:主要是query函数的写法。利用回溯,分情况判断左/右是否联通。注意自定义的结构一定要赋初值。
 
 
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define PAUSE system("pause")
#define Pr printf
#define Sc scanf
#define pb push_back
#define Sort(v) sort(v.begin(), v.end())
#define Unique(v) (unique(v.begin(), v.end())-v.begin())
#define MM(x, y) memset(x, y,sizeof(x))
#define For(i, s, t) for(int i=s; i<(t); i++)
#define eps (1e-9)
#define pi 3.1415926
#define N 50005
struct Statue
{
    bool l, r;
    int len;
    Statue(int len=0,bool l=false,bool r=false): len(len), l(l), r(r){}
};
struct SegmentTreeNode
{
    bool ok;
}tree[N<<2];
int used[N];
void build(int l,int r,int p)
{
    tree[p].ok = true;
    if(l == r)
        return;
    int m = (l+r)>>1;
    build(l, m, p<<1);
    build(m+1, r, p<<1|1);
}
void updata(int l,int r,int p, int k,bool ok)
{
    if(l == r)
    {
        tree[p].ok = ok;
        return ;
    }
    int m = (l+r)>>1;
    if(k <= m)
        updata(l, m, p<<1, k, ok);
    else
        updata(m+1, r, p<<1|1, k, ok);
    tree[p].ok = tree[p<<1].ok&& tree[p<<1|1].ok;
}
Statue query(int l, int r, int p,int k)
{
    if(tree[p].ok == true)
    {
        return Statue(r-l+1,true,true);
    }
    if(l == r)
    {
        return Statue(0,false,false);
    }
    int m = (l+r)>>1;
    Statue s1, s2;//忘记给s1, s2赋初值,蛋疼了好久。
    if(k <= m)
    {
        s1 = query(l, m, p<<1, k);
        if(s1.r == true)
            s2 = query(m+1, r, p<<1|1, m+1);
        return Statue(s1.len+s2.len, s1.l, s2.r);
    }
    else
    {
        s2 = query(m+1, r, p<<1|1, k);
        if(s2.l == true)
            s1 = query(l, m, p<<1, m);
        return Statue(s1.len+s2.len, s1.l, s2.r);
    }
}
int n, m;
int main()
{
    while(Sc("%d%d",&n,&m) != EOF)
    {
        stack<int> vec;
        MM(used, 0);
        build(1, n, 1);
        For(i, 0, m)
        {
            static char ch;
            static int x;
            Sc(" %c", &ch);
            if(ch == 'R')
            {
                while( !used[vec.top()] )
                    vec.pop();
                updata(1, n, 1, vec.top(), true);
                used[vec.top()] = 0;
                vec.pop();
            }
            else
            {
                Sc("%d", &x);
                if(ch == 'D')
                {
                    vec.push(x);
                    used[x]++;
                    updata(1, n, 1, vec.top(), false);
                }
                else
                {
                    Statue s = query(1, n,1, x);
                    Pr("%d\n", s.len);
                }
            }
        }
    }
    return 0;
}