HDU 5372 Segment Game(线段树+离散化)

题意:

有两种操作:

1. 插入一个线段

2. 删除一个已存在的线段

每次插入后输出当前插入的线段能完整覆盖存在的几条线段。

解析:

线段树上面维护的是两个值,左端点的和,右端点的和

每次插入一条区间[L, R]就,

先询问 [0, R] 的右端点个数 lsum

再询问[L, INF]的左端点的个数 rsum

tot表示:当前线段还有几条

那么题目要求的是整条线段的个数就是:lsum+rsum?tot

(这里用到了容斥的思想)

再用线段树(或者树状数组)单点增加左节点的个数,和右节点的个数

删除的时候就先获取区间,然后单点修改两个端点,

由于b比较大,有1e9,所以还需要先离散化一下。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define lson ls, L, M
#define rson rs, M+1, R
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = (int)3e5 + 10;

struct Node {
    int lsum, rsum;
    Node() { lsum = rsum = 0; }
} node[MAXN << 2];

inline void pushUp(int o) {
    node[o].lsum = node[ls].lsum + node[rs].lsum;
    node[o].rsum = node[ls].rsum + node[rs].rsum;
}

void build(int o, int L, int R) {
    node[o] = Node();
    if(L == R) return ;
    int M = (L + R)/2;
    build(lson);
    build(rson);
}

void modify(int o, int L, int R, int pos, int val, int type) {
    if(L == R) {
        if(type == -1)
            node[o].lsum += val;
        else
            node[o].rsum += val;
        return ;
    }
    int M = (L + R)/2;
    if(pos <= M) modify(lson, pos, val, type);
    else modify(rson, pos, val, type);
    pushUp(o);
}

int query(int o, int L, int R, int ql, int qr, int type) {
    if(ql <= L && R <= qr) {
        if(type == -1)
            return node[o].lsum;
        else
            return node[o].rsum;
    }
    int M = (L + R)/2, ret = 0;
    if(ql <= M) ret += query(lson, ql, qr, type);
    if(qr > M) ret += query(rson, ql, qr, type);
    return ret;
}

struct Line {
    int L, R;
    int lid, rid;
    Line() {}
    Line(int L, int R) : L(L), R(R) {}
} line[MAXN];
pii oper[MAXN];
int ln, n;
int arr[MAXN*2], an;

void descrete() {
    sort(arr, arr+an);
    an = unique(arr, arr+an) - arr;
    for(int i = 1; i < ln; i++) {
        line[i].lid = lower_bound(arr, arr+an, line[i].L) - arr;
        line[i].rid = lower_bound(arr, arr+an, line[i].R) - arr;
    }
}

void solve() {
    build(1, 0, MAXN);
    int tot = 0, idx = 1;
    int ql, qr;
    for(int i = 1; i <= n; i++) {
        if(oper[i].first == 0) {
            ql = line[idx].lid;
            qr = line[idx].rid;
            int lsum = query(1, 0, MAXN, 0, qr, 1);
            int rsum = query(1, 0, MAXN, ql, MAXN, -1);
            printf("%d\n", lsum + rsum - tot);
            modify(1, 0, MAXN, ql, 1, -1);
            modify(1, 0, MAXN, qr, 1, 1);
            idx++, tot++;
        }else {
            ql = line[oper[i].second].lid;
            qr = line[oper[i].second].rid;
            modify(1, 0, MAXN, ql, -1, -1);
            modify(1, 0, MAXN, qr, -1, 1);
            tot--;
        }
    }
}

int main() {
    int cas = 1;
    while(scanf("%d", &n) != EOF) {
        ln = 1, an = 0;
        int a, b;
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a, &b);
            oper[i] = mp(a, b);
            if(a == 0) {
                int L = b, R = b+ln;
                line[ln++] = Line(L, R);
                arr[an++] = L, arr[an++] = R;
            }
        }
        printf("Case #%d:\n", cas++);
        descrete();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 16:13:02

HDU 5372 Segment Game(线段树+离散化)的相关文章

hdu 5372 Segment Game(树状数组)

题目链接:hdu 5372 Segment Game 因为线段长度是递增的,不会出现后面的线段被前面的线段完全覆盖,所以只要分别计算[1,l-1]之间有多少个左端点,[1,r]之间有多少个右端点,想减即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 2 * 1e5 + 5; #define lowbit(x) ((x)&a

hdu 5124 lines (线段树+离散化)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 620    Accepted Submission(s): 288 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

HDU - 1542 扫描线入门+线段树离散化

扫描线算法+线段树维护简介: 像这种求面积的并集的题目,就适合用扫描线算法解决,具体来说就是这样 类似这种给出点的矩形的对角的点的坐标,然后求出所有矩形面积的交集的问题,可以采用扫描线算法解决.图如下,我们要求红色部分的面积: 我们可以通过一条叫扫描线的东西解决问题.具体来说: 我们首先给自己一条线,这条可以我称之为标准线(棕色线表示) 从上往下(从下往上也行)我们把每个矩形用一个四元组表示了l,r,h,f  也就是说,把一个矩形用上下两条边表示,l,r分别是x1,x2,而h则是y坐标,f代表这

【HDU】5249-KPI(线段树+离散化)

好久没写线段树都不知道怎么写了... 很easy的线段树二分问题 #include<cstdio> #include<set> #include<queue> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; #define lson (pos<<1) #define rson (pos<<1|1) const

hdu 5372 Segment Game 【 树状数组 】

给出一些操作, 0是将第i次增加的线段放在b位置,第i次放的线段的长度为i 1是将第b次增加操作放的线段删除 每次增加操作完之后,询问这条线段上面的完整的线段的条数 每次询问统计比这条线段左端点大的线段的条数 L,比这条线段右端点大的线段的条数 R,两个相减就是完整的线段的条数 另外因为给的b很大,所以需要离散化一下,而且b可能会相同,所以相同大小的应该占据一个编号 然后就像求逆序对那样的算 1 #include<iostream> 2 #include<cstdio> 3 #in

HDU 1199 &amp;&amp; ZOJ 2301 线段树离散化

一段长度未知的线段,一种操作:a b c ,表示区间[a,b]涂为颜色C,w代表白色,b代表黑色,问最终的最长连续白色段,输出起始位置和终止位置 离散化处理,和平常的离散化不同,需要把点化成线段,左闭右开,即对于一段区间[a,b],转化成区间[a,b+1) #include "stdio.h" #include "string.h" #include "algorithm" using namespace std; struct node { i

线段树+离散化 IP地址段检查 SEGMENT TREE

Problem: Give a series of IP segments, for example, [0.0.0.1-0.0.0.3], [123.234.232.21-123.245.21.1]... Now there is a new IP, find which IP segment it's in ? Solution: First, we could map the ends of IP segments into some intervals, since the IP add

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

HDU 4107 Gangster Segment Tree线段树

这道题也有点新意,就是需要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提高速度的目的. 本题过的人很少,因为大部分都超时了,我严格按照线段树的方法去写,一开始居然也超时. 然后修补了两个地方就过了,具体修改的地方请参看程序. 知道最大值段和最小值段,然后修补一下就能过了.不是特别难的题目. #include <stdio.h> #include <string> #include <algorithm> using namespace std; c

HDU 3743 Frosh Week (线段树+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 Frosh Week Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: