离散化

离散化

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~

这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,但是贴来贴去,有些海报就会被其他社团的海报所遮挡住。看到这个场景,小Hi便产生了这样的一个疑问——最后到底能有几张海报还能被看见呢?

于是小Ho肩负起了解决这个问题的责任:因为宣传栏和海报的高度都是一样的,所以宣传栏可以被视作长度为L的一段区间,且有N张海报按照顺序依次贴在了宣传栏上,其中第i张海报贴住的范围可以用一段区间[a_i, b_i]表示,其中a_i, b_i均为属于[0, L]的整数,而一张海报能被看到当且仅当存在长度大于0的一部分没有被后来贴的海报所遮挡住。那么问题就来了:究竟有几张海报能被看到呢?

提示一:正确的认识信息量

提示二:小Hi大讲堂之线段树的节点意义

输入

每个测试点(输入文件)有且仅有一组测试数据。

每组测试数据的第1行为两个整数N和L,分别表示总共贴上的海报数量和宣传栏的宽度。

每组测试数据的第2-N+1行,按照贴上去的先后顺序,每行描述一张海报,其中第i+1行为两个整数a_i, b_i,表示第i张海报所贴的区间为[a_i, b_i]。

对于100%的数据,满足N<=10^5,L<=10^9,0<=a_i<b_i<=L。

输出

对于每组测试数据,输出一个整数Ans,表示总共有多少张海报能被看到。

样例输入
5 10
4 10
0 2
1 6
5 9
3 4
样例输出
   5
分析:线段树+坐标离散化;
   注意(1,2)(2,3)(3,4)是三张海报;
   右端点减1后存在有的点无海报的情况;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <bitset>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e6+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,a[maxn],b[maxn],c[maxn],num;
set<int>ans;
struct Node
{
    ll sum, Min, Max, lazy;
} T[maxn<<2];
void PushUp(int rt)
{
    T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
    T[rt].Min = min(T[rt<<1].Min, T[rt<<1|1].Min);
    T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max);
}

void PushDown(int L, int R, int rt)
{
    int mid = (L + R) >> 1;
    ll t = T[rt].lazy;
    T[rt<<1].sum = t * (mid - L + 1);
    T[rt<<1|1].sum = t * (R - mid);
    T[rt<<1].Min =t;
    T[rt<<1|1].Min = t;
    T[rt<<1].Max = t;
    T[rt<<1|1].Max = t;
    T[rt<<1].lazy =t;
    T[rt<<1|1].lazy = t;
    T[rt].lazy = 0;
}

void Build(int L, int R, int rt)
{
    if(L == R)
    {
        T[rt].Min = T[rt].Max = T[rt].sum =T[rt].lazy= 0;
        return ;
    }
    int mid = (L + R) >> 1;
    Build(Lson);
    Build(Rson);
    PushUp(rt);
}

void Update(int l, int r, int v, int L, int R, int rt)
{
    if(l==L && r==R)
    {
        T[rt].lazy = v;
        T[rt].sum = 1LL*v * (R - L + 1);
        T[rt].Min = v;
        T[rt].Max = v;
        return ;
    }
    int mid = (L + R) >> 1;
    if(T[rt].lazy) PushDown(L, R, rt);
    if(r <= mid) Update(l, r, v, Lson);
    else if(l > mid) Update(l, r, v, Rson);
    else
    {
        Update(l, mid, v, Lson);
        Update(mid+1, r, v, Rson);
    }
    PushUp(rt);
}
void work(int L,int R,int rt)
{
    if(L==R)
    {
        //printf("%d %d %d
",L,R,T[rt].lazy);
        ans.insert(T[rt].Min);
        return;
    }
    if(T[rt].lazy)PushDown(L,R,rt);
    int mid=L+R>>1;
    work(Lson);
    work(Rson);
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    j=0;
    rep(i,0,n-1)scanf("%d%d",&a[i],&b[i]),c[j++]=a[i],c[j++]=b[i];
    sort(c,c+j);
    num=unique(c,c+j)-c;
    rep(i,0,n-1)
    {
        a[i]=lower_bound(c,c+num,a[i])-c+1;
        b[i]=lower_bound(c,c+num,b[i])-c+1;
    }
    Build(1,num-1,1);
    rep(i,0,n-1)Update(a[i],b[i]-1,i+1,1,num-1,1);
    work(1,num-1,1);
    printf("%d
",ans.size()-(ans.find(0)!=ans.end()));
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/5749574.html