hdu1540 Tunnel Warfare(线段树区间合并详解)

来源:互联网 发布:万思软件开发有限公司 编辑:程序博客网 时间:2024/05/22 08:03


http://acm.hdu.edu.cn/showproblem.php?pid=1540


题意:给你一些村庄,D代表毁灭该村,R代表恢复该村,Q代表和x村相连的未被毁灭的村庄。

前两者明显就是单点更新了,查询的时用到了区间合并。


区间合并果然麻烦,连想带懂几乎花了我将近三天,不过好在做掉了。


思路:注释都在代码里了,适合入门。总的来说,增加了三个域ls, rs, ms。然后更新和查询的时候都要对这三个域操作。更新是从下到上依次更新,拆分的区间进行合并。查询注意与待查值比较两次,分别是mid比较判断左右子树,非连续边界判断x值是否在给定区间。差不多就到此为止,再往深感觉核心思想还没领会,真是难以形容。


#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>#include <stack>using namespace std;typedef long long LL;const int N = 50010;const int INF = 1e8;struct line{    int l, r;    int ls, rs, ms;//分别代表左最大连续区间值,右最大连续区间值,整体最大连续区间值}tree[4 * N];void build(int i, int l, int r){    tree[i].l = l;    tree[i].r = r;    tree[i].ls = tree[i].rs = tree[i].ms = r-l+1;//初始化为树满    if(l == r)    {        return;    }    int mid = (l + r) >> 1;    build(i*2, l, mid);    build(i*2+1, mid+1, r);}void update(int i, int x, int flag){    if(tree[i].l == tree[i].r)    {        tree[i].ls = tree[i].rs = tree[i].ms = flag;//flag为0代表破坏,flag为1代表修复        return;    }    int mid = (tree[i].l+tree[i].r) >> 1;    if(mid >= x) update(i*2, x, flag);    else update(i*2+1, x, flag);    //更新左连续区间值    if(tree[i*2].ms == tree[i*2].r-tree[i*2].l+1)//如果左子树树满        tree[i].ls = tree[i*2].ms + tree[i*2+1].ls;//父亲节点也就等于左子树的全部值加上右子树的左(注意)最大连续区间值    else tree[i].ls = tree[i*2].ls;//否则就是左连续区间值    //更新右连续区间值    if(tree[i*2+1].ms == tree[i*2+1].r-tree[i*2+1].l+1)//如果右子树树满        tree[i].rs = tree[i*2+1].ms + tree[i*2].rs;//父亲节点也就等于右子树的全部值加上左子树的右(注意)最大连续区间值    else tree[i].rs = tree[i*2+1].rs//否则就是右连续区间值    //更新最大区间值必须放最后    tree[i].ms = max(max(tree[i*2].ms, tree[i*2+1].ms), tree[i*2].rs+tree[i*2+1].ls);}int query(int i, int x){    if(tree[i].l == tree[i].r || tree[i].ms == 0 || tree[i].ms == tree[i].r-tree[i].l+1)//遍历到最底层、空树满树都应该返回        return tree[i].ms;    int mid = (tree[i].l+tree[i].r) >> 1;    if(mid >= x)//在左子树    {        if(tree[i*2].r-tree[i*2].rs+1 > x)//如果待查找值x在最大不连续左边界(右边界减去最大右连续值,个人理解有点牵强)的左边            return query(i*2, x);//继续查左子树        else return tree[i*2].rs+tree[i*2+1].ls;//否则x的最大连续区间为左子树的右连续区间加右子树的左连续区间    }    else//在右子树。。同理    {        if(tree[i*2+1].l+tree[i*2+1].ls-1 < x)            return query(i*2+1, x);        else return tree[i*2+1].ls+tree[i*2].rs;    }}int main(){  //  freopen("in.txt", "r", stdin);    int n, m, x;    char s[5];    while(~scanf("%d%d", &n, &m))    {        build(1, 1, n);        stack <int> st;//先破坏的先回复,所以用栈表示        if(!st.empty()) st.pop();        for(int i = 0; i < m; i++)        {            scanf("%s", s);            if(s[0] == 'D')            {                scanf("%d", &x);                update(1, x, 0);                st.push(x);            }            else if(s[0] == 'R')            {                x = st.top();                st.pop();                update(1, x, 1);            }            else if(s[0] == 'Q')            {                scanf("%d", &x);                printf("%d\n", query(1, x));            }        }    }    return 0;}


0 0
原创粉丝点击