POJ 2528 线段树区间查找区间询问

Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 53839 Accepted: 15635

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

Source

Alberta Collegiate Programming Contest 2003.10.18

题意

有多张海报贴到墙上,问最终能一眼看到几张不同的海报。

题解

线段树求解,区间查找区间询问

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)

using namespace std;

struct Po{
    int x,y;
    int R,L;
};

Po q[30000+50];
int len;
int T[300000];
int lazy[300000];
int rMax;
int ma[300000];
set<int>Se;

void init(){
    m0(T);
    m0(lazy);
}

void Push_down(int rt){
   T[rt<<1] = lazy[rt];lazy[rt<<1] =  lazy[rt];
   T[rt<<1|1] = lazy[rt];lazy[rt<<1|1] = lazy[rt];
   lazy[rt] = 0;
}

void update(int L,int R,int C,int rt,int l,int r){
    if(L<=l&&r<=R){
        T[rt] = C;
        lazy[rt] = C;
        return;
    }

    if(lreturn;
    if(r>R&&l>R) return;

    if(lazy[rt]) Push_down(rt);
    int m = (l + r)>>1;

    if(L<=m) update(L,R,C,rt<<1,l,m);
    if(R>m) update(L,R,C,rt<<1|1,m+1,r);

}

void query(int L,int R,int rt,int l,int r){
    if(lazy[rt]){
        Se.insert(T[rt]);
        return;
    }
    if(l==r) return;
    int m = (l+r)>>1;
    if(L<=m) query(L,R,rt<<1,l,m);
    if(R>m) query(L,R,rt<<1|1,m+1,r);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int N;
    cin>>N;
    while(N--){
        int n;
        cin>>n;
        int i,j = 1,k;
        f(i,1,n){
            cin>>q[i].x>>q[i].y;
            ma[j++] = q[i].x;
            ma[j++] = q[i].y;
        }
        j--;
        sort(ma+1,ma+j+1);
        k = 1;

        f(i,2,j){
            if(ma[i]!=ma[k]){
                ma[++k] = ma[i];
            }
        }

        len = k;
        j = k+1;
        f(i,2,k){
            if(ma[i]-ma[i-1]>1){
                ma[j++] = (ma[i]+ma[i-1])/2;
            }
        }
        j--;
        sort(ma+1,ma+j+1);

        len = j;
        init();
        f(i,1,n){
            q[i].L = lower_bound(ma+1,ma+len+1,q[i].x) - ma;
            q[i].R = lower_bound(ma+1,ma+len+1,q[i].y) - ma;
            update(q[i].L,q[i].R,i,1,1,len);
        }
        Se.clear();
        Se.insert(0);
        query(1,len,1,1,len);
        cout<1<return 0;
}

你可能感兴趣的:(线段树)