Mayor's posters 离散化+线段树染色

来源:互联网 发布:java格式化json字符串 编辑:程序博客网 时间:2024/06/12 13:49
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

151 42 68 103 47 10

Sample Output

4


额,,关于离散化网上有很多博客都讲的很清楚,简单说就是一种映射,把大范围的数都映射到小空间上,不然会MLE,不要被“离散化”这个词吓到,其实很容易实现的

#include <iostream>    #include <algorithm>    #include <cstdio>    #include <cstring>    using namespace std;    const int N = 1e4 + 5;    struct tree{    int l, r;    int color;}STree[N << 4];int li[N], ri[N], x[N << 2];int cnt;bool vis[N << 2];void Build(int l, int r, int i){    STree[i].l = l;    STree[i].r = r;    STree[i].color = -1;    if(l == r) return;    int mid = (l + r) >> 1;    Build(l, mid, i << 1);    Build(mid + 1, r, i << 1 | 1);}void Push_Down(int i){    if(STree[i].color != -1)    {        STree[i << 1].color = STree[i << 1 | 1].color = STree[i].color;        STree[i].color = -1;    }}void Update(int i, int l, int r, int color){    if(STree[i].l >= l && STree[i].r <= r)    {        STree[i].color = color;        return;    }    Push_Down(i);    int mid = (STree[i].l + STree[i].r) >> 1;    if(l <= mid)        Update(i << 1, l, r, color);    if(r > mid)        Update(i << 1 | 1, l, r, color);}void Query(int i){    if(STree[i].color != -1)    {        if(!vis[STree[i].color])        {            cnt++;            vis[STree[i].color] = 1;        }        return;    }    if(STree[i].l == STree[i].r) return;    Query(i << 1);    Query(i << 1 | 1);}int main()    {               int t, n;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        int k = 1;        for(int i = 1; i <= n; i++)        {            scanf("%d%d", &li[i], &ri[i]);            x[k++] = li[i];            x[k++] = ri[i];        }        sort(x, x + k);        int m = 1;        for(int i = 1; i < k; i++)            if(x[i] != x[i-1])                x[m++] = x[i];        for(int i = m - 1; i > 1; i--)            if(x[i] != x[i-1] + 1)                x[m++] = x[i-1] + 1;        sort(x, x + m);        Build(1, m - 1, 1);        for(int i = 1; i <= n; i++)        {            int l = lower_bound(x, x + m, li[i]) - x;            int r = lower_bound(x, x + m, ri[i]) - x;            Update(1, l, r, i);        }        memset(vis, 0, sizeof(vis));        cnt = 0;        Query(1);        printf("%d\n", cnt);    }    return 0;    }  


阅读全文
0 0
原创粉丝点击