POJ-2528 (discrete segment tree +)

Mayor's posters

POJ-2528

  • The title is a combination of discrete intervals and update the tree line.
  • Note that the code is here to join a deduplication operation. And I was here when the contribution from the beginning, so the next question to be the subject of attention.
  • Where there is a need to pay attention is, after discretization, there may be a problem according to the meaning of the title, it is [1,10], [1, 4], [6, 10] This situation error. 4-6 herein directly as a continuous, but in fact the middle there is a 5 belong to the first section. So for this situation needs extra point.
  • There is also a data amount of problems that require attention. Try to open a bigger array, or else not reported RE is WA (pro-test).
//离散化+线段树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100014;
int n,m;
int lazy[maxn<<2];
int num[maxn<<1];
int left1[maxn];
int right1[maxn];
bool vis[maxn];
int ans=0;
void build(int id,int l,int r){
    if(l==r)
        return;
    int lc=id<<1;
    int rc=id<<1|1;
    int mid=(l+r)>>1;
    build(lc,l,mid);
    build(rc,mid+1,r);
}
void pushdown(int id,int l,int r){
    if(lazy[id]==0)//这里要注意,不然这些lazy数组都会被赋值为0
        return;
    int lc=id<<1;
    int rc=id<<1|1;
    lazy[lc]=lazy[id];
    lazy[rc]=lazy[id];
    lazy[id]=0;
}
void update(int id,int l,int r,int p,int q,int v){
    if(p<=l&&q>=r){
        lazy[id]=v;
        return;
    }
    pushdown(id,l,r);
    int mid=(l+r)>>1;
    int lc=id<<1;
    int rc=id<<1|1;
    if(p<=mid) update(lc,l,mid,p,q,v);
    if(q>mid) update(rc,mid+1,r,p,q,v);
}
void query(int id,int l,int r){
    if(lazy[id]&&!vis[lazy[id]]){
        ans++;
        vis[lazy[id]]=1;
        return;
    }
    if(l==r)
        return;
    pushdown(id,l,r);
    int lc=id<<1;
    int rc=id<<1|1;
    int mid=(l+r)>>1;
    query(lc,l,mid);
    query(rc,mid+1,r);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        ans=0;
        memset(lazy,0,sizeof(lazy));
        memset(vis,0,sizeof(vis));
        cin>>n;
        int cnt=1;
        for(int i=1;i<=n;i++){
            int a,b;
            cin>>left1[i]>>right1[i];
            num[cnt++]=left1[i];
            num[cnt++]=right1[i];
        }
        sort(num+1,num+cnt);
        m=unique(num+1,num+cnt)-(num+1);
        //cout<<m<<endl;
        //在所有相隔距离大于1的点之间都插入一个单点(让它充当那段被裸露区间)
        int an=m;
        for(int i=2;i<=m;i++){
            if(num[i]-num[i-1]>1){
                num[++an]=num[i-1]+1;
            }
        }
        sort(num+1,num+an+1);
        build(1,1,an);
        // for(int i=1;i<=an;i++)
        //     cout<<num[i]<<endl;
        for(int i=1;i<=n;i++){
            int p=lower_bound(num+1,num+an+1,left1[i])-num;
            int q=lower_bound(num+1,num+an+1,right1[i])-num;
            //cout<<p<<" "<<q<<endl;
            update(1,1,an,p,q,i);
        }
        query(1,1,an);
        cout<<ans<<endl;
    }
    //system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/GarrettWale/p/11448260.html