poj 2528 线段树区间修改+离散化

Mayor's posters POJ 2528

传送门

线段树区间修改加离散化

#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define P pair<int,int>
const ll INF=1e18;
const int N=1e6+10;
int ans[N];
int l[N],r[N];
struct SegmentTree{
    int l,r;
    ll dat;
}t[N*4];
void build(int p,int l,int r)
{
    t[p].l = l;
    t[p].r = r;
    t[p].dat = 0;
    if(t[p].l == t[p].r){t[p].dat = 0;return ;}
    int mid = (l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
}
void spread(int p)
{
    if(t[p].dat)
    {
        t[p*2].dat = t[p].dat;
        t[p*2+1].dat = t[p].dat;
        t[p].dat = 0;
    }
}
void change(int p,int l,int r,ll v)
{
    if(l <= t[p].l && t[p].r <= r)
    {
        t[p].dat = v;
        return ;
    }
    int mid = (t[p].l+t[p].r)/2;
    spread(p);
    if(l<=mid) change(p*2,l,r,v);
    if(r>mid) change(p*2+1,l,r,v);
}
void ask(int p,int l,int r)
{
    if(t[p].dat)
    {
        ans[t[p].dat] = 1;
        return ;
    }
    if(t[p].l == t[p].r)
        return;
    int mid = (t[p].l+t[p].r)/2;
    spread(p);
    ask(p*2,l,r);
    ask(p*2+1,l,r);
}
vector<int>num;
int n;
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        num.clear();
        for(int i=1;i<=n;i++)
        {
            cin >> l[i] >> r[i];
            num.push_back(l[i]);
            num.push_back(r[i]);
        }
        sort(num.begin(),num.end());
        num.erase(unique(num.begin(),num.end()),num.end());
        int m = num.size();
        for(int i=1;i<m;i++)
        {
            if(num[i] - num[i-1] > 1)
                num.push_back(num[i-1]+1);
        }
        sort(num.begin(),num.end());
        m = num.size();
        build(1,0,m-1);
        for(int i=1;i<=n;i++)
        {
            ans[i] = 0;
            int x = lower_bound(num.begin(),num.end(),l[i])-num.begin();
            int y = lower_bound(num.begin(),num.end(),r[i])-num.begin();
            change(1,x,y,i);
        }
        int res = 0;
        ask(1,0,m-1);
        for(int i=1;i<=n;i++)
        {
            if(ans[i])
                res++;
        }
        cout << res << "\n";
    }
    return 0;
}
posted @ 2020-02-28 10:59  hh13579  阅读(94)  评论(0编辑  收藏  举报