POJ 2528 Mayor's posters 线段树+离散化

来源:互联网 发布:网上申报软件下载 编辑:程序博客网 时间:2024/06/05 17:27

题意:

几张纸覆盖在一起,求能露出来的纸的个数。(区间覆盖问题)

思路:

区间最大值为1E8,开数组直接做MLE。

注意到此题端点个数仅为20000个,故先离散,再线段树。

代码:

map离散 610ms

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <map>using namespace std;#define ls rt<<1#define rs rt<<1|1const int MAXN=1e6+100;bool flag[MAXN];int tree[MAXN*3];int num[MAXN*3];int v;int st;int en;typedef struct Node{    int x;int y;}Node;int ans;void push_down(int rt){    if(tree[rt]==0) return ;    tree[ls]=tree[rs]=tree[rt];    tree[rt]=0;    return ;}void update(int rt,int l,int r){    if(st<=l&&r<=en){        tree[rt]=v;        return ;    }    int mid=(l+r)>>1;    push_down(rt);    if(st<=mid)        update(ls,l,mid);    if(mid<en)        update(rs,mid+1,r);    //push_up(rt);    return ;}void re(int rt){    if(!flag[tree[rt]]){        ans++;        flag[tree[rt]]=1;    }    return ;}void query(int rt,int l,int r){    if(tree[rt]){        re(rt);        return ;    }    if(l==r) return ;    int mid=(l+r)>>1;    query(ls,l,mid);    query(rs,mid+1,r);    return ;}int main(){    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        Node a[10010];        memset(tree,0,sizeof(tree));        memset(flag,0,sizeof(flag));        //memset(flag2,0,sizeof(flag2));        memset(num,0,sizeof(num));        int m=0;        int ppp=-1;        map <int,bool> flag2;        for(int i=0;i<n;i++){            scanf("%d%d",&a[i].x,&a[i].y);            if(a[i].x>a[i].y)                swap(a[i].x,a[i].y);            if(!flag2[a[i].x]){                num[++ppp]=a[i].x;                flag2[a[i].x]=1;            }            if(!flag2[a[i].y]){                num[++ppp]=a[i].y;                flag2[a[i].y]=1;            }        }        sort(num,num+ppp+1);        int pp=ppp+1;        for(int i=1;i<pp;i++)            if(num[i]-num[i-1]>1)                num[++ppp]=num[i-1]+1;        sort(num,num+ppp+1);        for(int i=0;i<n;i++){            a[i].x=lower_bound(num,num+ppp+1,a[i].x)-num+1;            a[i].y=lower_bound(num,num+ppp+1,a[i].y)-num+1;        }        m=ppp+1;        for(int i=0;i<n;i++){            st=a[i].x;            en=a[i].y;            v=i+1;            update(1,1,m);        }        ans=0;        query(1,1,m);        cout<<ans<<endl;    }    return 0;}
非map 219ms

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define ls rt<<1#define rs rt<<1|1const int MAXN=1e6+100;bool flag[MAXN];int tree[MAXN*3];bool flag2[MAXN];int num[MAXN*3];int v;int st;int en;int temp[MAXN*3];typedef struct Node{    int x;int y;}Node;int ans;void push_down(int rt){    if(tree[rt]==0) return ;    tree[ls]=tree[rs]=tree[rt];    tree[rt]=0;    return ;}void update(int rt,int l,int r){    if(st<=l&&r<=en){        tree[rt]=v;        return ;    }    int mid=(l+r)>>1;    push_down(rt);    if(st<=mid)        update(ls,l,mid);    if(mid<en)        update(rs,mid+1,r);    //push_up(rt);    return ;}void re(int rt){    if(!flag[tree[rt]]){        ans++;        flag[tree[rt]]=1;    }    return ;}void query(int rt,int l,int r){    if(tree[rt]){        re(rt);        return ;    }    if(l==r) return ;    int mid=(l+r)>>1;    query(ls,l,mid);    query(rs,mid+1,r);    return ;}int main(){    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        Node a[10010];        memset(tree,0,sizeof(tree));        memset(flag,0,sizeof(flag));        memset(flag2,0,sizeof(flag2));        memset(num,0,sizeof(num));        int m=0;        int ppp=0;        for(int i=0;i<n;i++){            scanf("%d%d",&a[i].x,&a[i].y);            temp[ppp++]=a[i].x;            temp[ppp++]=a[i].y;        }        sort(temp,temp+ppp);        int p=1;        num[0]=temp[0];        for(int i=1;i<ppp;i++)            if(temp[i]!=num[p-1])                num[p++]=temp[i];        sort(num,num+p);        int pp=p;        for(int i=1;i<pp;i++)            if(num[i]-num[i-1]>1) num[p++]=num[i-1]+1;        sort(num,num+p);        for(int i=0;i<n;i++){            a[i].x=lower_bound(num,num+p,a[i].x)-num+1;            a[i].y=lower_bound(num,num+p,a[i].y)-num+1;        }        m=p;        for(int i=0;i<n;i++){            st=a[i].x;            en=a[i].y;            v=i+1;            update(1,1,m);        }        ans=0;        query(1,1,m);        cout<<ans<<endl;    }}

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 
  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 

Sample Input

151 42 68 103 47 10

Sample Output

4



0 0