JDOJ 2197: 校门外的树

JDOJ 2197: 校门外的树

题目传送门

Description

某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。

我们可以把马路看成一个数轴,马路的一端在数轴1的位置,另一端在L的位置;

数轴上的每个整数点,即1,2,...L的位置,都种有一棵树。

由于马路上的N个区域[L1,R1],[L2,R2]...[LN,RN]要用来建地铁,区域之间可能有重合的部分。

现在要把这些区域的树(包括区域端点处的两棵树)移走。

你的任务是计算每次移走这些树后,马路上还有多少棵树。

Input

两个整数N和L。

2~N+1行,两个整数Li,Ri。

Output

N行,每行一个整数,表示移走后树的数量。

Sample Input

2 4 2 3 3 4

Sample Output

2 1

HINT

对于50%的数据, N,L,Li,Ri <= 103。

对于100%的数据, N,L,Li,Ri <= 105。

题解:

数据加强,使用模拟的O(N^2)方法会TLE。

所以我们采取了线段树。

这是一道线段树板子题,关于线段树的基础知识请参考本蒟蒻的另一篇博客(例题也是这道)

基础知识传送门

AC 代码:

#include<bits/stdc++.h>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
int n,L;
int tree[100001<<2];
void build(int pos,int l,int r)
{
    int mid=(l+r)>>1;
    tree[pos]=r-l+1;
    if(l==r)
        return;
    build(lson,l,mid);
    build(rson,mid+1,r);
}
void fix(int pos,int l,int r,int x,int y)
{
    int mid=(l+r)>>1;
    if(tree[pos]==0)
        return;
    if(x<=l && r<=y)
    {
        tree[pos]=0;
        return;
    }
    if(y<=mid)
        fix(lson,l,mid,x,y);
    else if(x>mid)
        fix(rson,mid+1,r,x,y);
    else
    {
        fix(lson,l,mid,x,y);
        fix(rson,mid+1,r,x,y);
    }
    tree[pos]=tree[lson]+tree[rson];
}
int main()
{
    scanf("%d%d",&n,&L);
    build(1,1,L);
    for(int i=1;i<=n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        fix(1,1,L,a,b);
        printf("%d\n",tree[1]);
    }
    return 0;
}
posted @ 2019-08-05 11:30  Seaway-Fu  阅读(207)  评论(0编辑  收藏  举报