线段树-离散化处理点

博客 :http://www.cnblogs.com/wuyiqi/archive/2012/03/19/2405885.html

 

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

题目大意 :
  向一面墙上贴东西, 后贴的会覆盖前面的,问最终可以看到的有几种?

这题快要WA 哭我了, 一顿runtime error , 自己上网上搜的AC 代码,开始用g++的, 一直runtime error,后来试了下, C++ 网上的代码就 A 了 , 然后后来也找到了我的代码的问题。

线段树的节点大小一定要 开 到 最下面叶子节点 的 4 倍 啊 !!

这题还有一点 就是数据很大, 直接线段树操作 铁定是超时 超内存 , 因此要对输入的点进行离散化 。
具体离散化 我在专门写一个博客

下面是我的AC 代码 , C++ G++ 都能过

#include <iostream>
#include <cstdio>

#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int eps = 1e4+5;

struct point
{
    int x, y;
}po[eps];

int pre[eps<<2];
int a, b, ans;
int lazy[eps<<4];
bool pt[eps<<2];

void down(int k){
    lazy[k<<1] = lazy[k<<1|1] = lazy[k];
    lazy[k] = 0;
}

void change(int l, int r, int pt, int k){
    if (a <= l && r <= b){
        lazy[k] = pt;
        return;
    }
    if (lazy[k]) down(k);
    int m = (l + r) >> 1;
    if (a <= m) change(l, m, pt, k<<1);
    if (b > m) change(m+1, r, pt, k<<1|1);
}

void query(int l, int r, int k){
    if (lazy[k]){
        if (pt[lazy[k]]) ans++;
        pt[lazy[k]] = false;
        return;
    }
    if (l == r) return;
    int m = (l + r) >> 1;
    query(l, m, k<<1);
    query(m+1, r, k<<1|1);
}

int fun(int l, int r, int key){
    
    while (l <= r){
        int mid = (l + r) >> 1;
        if (pre[mid] == key) return mid;
        else if (pre[mid] < key) l = mid + 1;
        else r = mid - 1;
    }
}

int main() {
    int t, n;
    
    cin >> t;
    while (t--){
        cin >> n;
        memset(lazy, 0, sizeof(lazy));
        int k = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d%d", &po[i].x, &po[i].y);
            pre[k++] = po[i].x, pre[k++] = po[i].y;
        }
        sort(pre, pre+k);
        int m = 1;
        for(int i = 1; i < k; i++){
            if (pre[i] != pre[i-1]) pre[m++] = pre[i];
        }
        for(int i = m - 1; i >= 1; i--){
            if (pre[i] != pre[i-1] + 1) pre[m++] = pre[i-1] + 1;
        }
        sort(pre, pre+m);
        for(int i = 1; i <= n; i++){
            a = fun(0, m - 1, po[i].x) + 1;
            b = fun(0, m - 1, po[i].y) + 1;
            change(1, m, i, 1);        
        }
        ans = 0; 
        memset(pt, 1, sizeof(pt));
        query(1, m, 1);
        printf("%d\n", ans);
    }
    
    return 0;
}

 


posted @ 2017-10-09 23:08  楼主好菜啊  阅读(457)  评论(0编辑  收藏  举报