Mayor's posters (线段树+离散化)

Mayor's posters (线段树+离散化)

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

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

Sample Output

4

 

题意:在一个10000000长 的墙上贴海报,每张海报高度都等于墙高,宽度为第l块墙到第r块墙(如图),后贴的会覆盖先前贴的,问最后能看到几张海报。

思路:长度1e7的,整段维护肯定会超时,所以首先离散化,将给出的所有点离散化到1~n上再进行维护。其中lazy保存的是[l,r]区间应该贴几号海报,插入时后面的会覆盖前面的lazy,最后查询时不用将lazy都down到尾节点(不知为何,会RE),直接把当前[l,r]的lazy记录一下,就不用访问他的子节点了。

 

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#define mid (left+right)/2
#define lson node*2,left,mid
#define rson node*2+1,mid+1,right

using namespace std;

const int maxn = 2e4+10;
int tree[maxn*8];
int n;
int x[maxn*3];
int lazy[maxn*8];
int hah[10000005];
int cnt = 0;
int via[maxn*4];

struct node {
    int l,r;
} a[maxn];

void up( int node )
{
    tree[node] = tree[node*2] + tree[node*2+1];
}

void down( int node )
{
    if ( lazy[node] ) {
        lazy[node*2] = lazy[node];
        lazy[node*2+1] = lazy[node];
        tree[node*2] = lazy[node];
        tree[node*2+1] = lazy[node];
        lazy[node] = 0;
    }
}

void built_tree( int node, int left, int right )
{
    if ( left==right ) {
        tree[node] = 0;
        return ;
    }
    built_tree(lson);
    built_tree(rson);
    up(node);
}

void update( int node, int left, int right, int L, int R, int date )
{
    if ( left>=L && right<=R ) {
        tree[node] = date;
        lazy[node] = date;
        return ;
    }
    down(node);

    if ( L<=mid ) {
        update(lson,L,R,date);
    }
    if ( R>mid ) {
        update(rson,L,R,date);
    }
    //up(node);
}

void query( int node, int left, int right )
{ // 这里查询的是,整棵树有多少不同的海报。
    if ( left==right && tree[node]==0 ) {
        return ;
    }
    if ( lazy[node] ) { // 不用再down了,因为他的所以儿子的值都等于父亲的lazy
        via[lazy[node]] = 1;
        return ;
    }
    // down(node)  就因为这里down了一下,runtime error
    query(lson);
    query(rson);
}

int main()
{
    ios::sync_with_stdio(0);
    int listt,i,j;
    cin >> listt;
    while ( listt-- ) {
        memset(via,0,sizeof(via));
        memset(lazy,0,sizeof(lazy));
        cin >> n;
        for ( i=1; i<=n; i++ ) {
            cin >> a[i].l >> a[i].r;
            x[cnt++] = a[i].l;
            x[cnt++] = a[i].r;
        }
        // 离散化
        sort(x,x+cnt);
        cnt = unique(x,x+cnt)-x;
        for ( i=0; i<cnt; i++ ) {
            hah[x[i]] = i+1;
        }
        // 建树
        built_tree(1,1,cnt);
        // 逐个跟新区间
        for ( i=1; i<=n; i++ ) {
            a[i].l = hah[a[i].l];
            a[i].r = hah[a[i].r];
            update(1,1,cnt,a[i].l,a[i].r,i);
        }
        query(1,1,cnt); // 只进行一次查询,这里不是查询根节点的值,所以和普通查询有区别
        int ans = 0;
        for ( i=1; i<=cnt; i++ ) {
            if ( via[i] ) {
                ans ++;
            }
        }
        cout << ans << endl;
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值