HDU 5372 Segment Game(线段树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5372

题意:n次操作,每次读入a,b。当a = 0,加入线段,线段开头位置为b,结尾位置为b + i ,其中 i 表示当前是第 i 个加入线段的操作。a = 1,删除线段,删除第 b 次加入线段操作时加入的线段。每次加入线段,输出已经加入的线段中有多少个线段能被现在加入的线段完全覆盖。

思路:构造两个线段树,分别记录区间中线段左端点的个数,和右端点的个数。对于每段新加入的线段 [L, R],它所能覆盖的线段数=已加入的线段中左端点位置大于等于L的线段数 - 右端点位置大于R的线段数。其中加入线段时,线段开头b的位置为 |b| < 10^9,所以需要先离散化处理,即找出所有端点的位置 (<= 2 * n),重新编号,再建树。交G++能A,交C++会T。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#define INF 0x7fffffff
#define MOD 1000000007
#include <set>
#include <map>
#define lson l, m, rt * 2
#define rson m + 1, r, rt * 2 + 1
using namespace std;
typedef long long ll;
const int MAXN = 200010;
int op[MAXN], th[MAXN], cntadd, cntdel, cnt, a[MAXN * 2];
int add[MAXN], left[MAXN * 2 * 4], right[MAXN * 2 * 4];
map<int, int> num;

void pushup(int *cur, int rt)
{
    cur[rt] = cur[rt * 2] + cur[rt * 2 + 1];
}

void update(int *cur, int L, int R, int c, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        cur[rt] += c;
        return;
    }
    int m = (l + r) / 2;
    if(L <= m)
        update(cur, L, R, c, lson);
    if(R > m)
        update(cur, L, R, c, rson);
    pushup(cur, rt);
}

int query(int *cur, int L, int R, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        return cur[rt];
    }
    int res = 0, m = (l + r) / 2;
    if(L <= m)
        res += query(cur, L, R, lson);
    if(R > m)
        res += query(cur, L, R, rson);
    return res;
}

int main()
{
    #ifdef LOCAL
    freopen("data.in", "r", stdin);
    #endif

    int n, cas = 0;
    while(scanf("%d", &n) != EOF)
    {
        cas++;
        cntadd = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &op[i], &th[i]);
            if(op[i] == 0)
            {
                op[i] = ++cntadd; //op[]记录当前为第几次加入操作
                add[cntadd] = th[i]; //add[i]记录第i次加入操作线段的开头位置
                a[2 * cntadd - 2] = th[i]; //加入线段的开头位置和结尾位置
                a[2 * cntadd - 1] = th[i] + cntadd;
            }
            else op[i] = -1;
        }
        sort(a, a + 2 * cntadd);
        cnt = 0;
        num.clear();
        num[a[0]] = ++cnt;
        for(int i = 1; i < 2 * cntadd; i++)
        { //离散化,为每个有端点的位置重新编号,去重
            if(a[i] != a[i - 1])
            {
                num[a[i]] = ++cnt;
            }
        }
        //printf("cnt=%d\n", cnt);
        for(int i = 0; i <= 4 * cnt + 2; i++)
        { //建树
            left[i] = 0; right[i] = 0;
        }
        printf("Case #%d:\n", cas);
        for(int i = 1; i <= n; i++)
        {
            if(op[i] > 0)
            {
                int L = num[th[i]], R = num[th[i] + op[i]]; //取离散化后的新编号
                //printf("add:%d %d\n", L, R);

                int ans = 0; //已加入的线段中左端点位置大于等于L的线段数 - 右端点位置大于R的线段数
                ans += query(left, L, cnt, 1, cnt, 1);
                if(R + 1 <= cnt)
                    ans -= query(right, R + 1, cnt, 1, cnt, 1);
                printf("%d\n", ans);
		
		//加入新的左端点和右端点
                update(left, L, L, 1, 1, cnt, 1);
                update(right, R, R, 1, 1, cnt, 1);
            }
            else
            {
                int L = num[add[th[i]]], R = num[add[th[i]] + th[i]];
                //printf("del:%d %d\n", L, R);
                update(left, L, L, -1, 1, cnt, 1);
                update(right, R, R, -1, 1, cnt, 1);
            }
        }
    }

    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值