POJ 2528 Mayors posters(线段树+离散化)

Mayor’s posters

Time Limit:1000MSMemory Limit:65536K

Total Submissions:46857Accepted:13601

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 liand 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

Source

题意:n张海报,贴在一张墙上,现给出这n张海报的初始位置和终点位置,,问在贴完这n张海报之后,能在墙上看到几张海报(因为存在全部覆盖的情况)。

因为数据给的太大,用普通的线段树一定会超时和超内存,所以需要用到离散化,离散化其实现在看来就是把需要用到的数据经过处理对应到线段树的每个区间去。这样可以减少很多的区间浪费。

代码:

<span style="font-size:18px;">#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include<stdlib.h>using namespace std;const int manx = 10100;int v[11000];struct xian{int a,b;int key;} point[manx];struct node{int l,r;int key;bool flag;} q[manx<<4];int ans;int data1[manx<<1];int data2[manx<<2];void Push_down(int rt){if(q[rt].flag){q[rt<<1].flag = q[rt<<1|1].flag = true;q[rt<<1].key = q[rt<<1|1].key = q[rt].key;q[rt].flag = false;}}void build(int l, int r, int rt){q[rt].l = data2[l];q[rt].r = data2[r];q[rt].key = -1;q[rt].flag = false;if(l == r){return;}int mid = (l + r)>>1;build(l , mid , rt<<1);build(mid+1, r, rt<<1|1);}void query(int l, int r, int rt){if(q[rt].l == q[rt].r){if(v[q[rt].key] == 0 && q[rt].key != -1){ans++;v[q[rt].key] = 1;}return;}Push_down(rt);int mid = (l + r)>>1;query(l , mid , rt<<1);query(mid+1, r, rt<<1|1);}void updata(int ll, int rr, int id, int l, int r, int rt){if(rr < q[rt].l || ll > q[rt].r) return;if(ll<=q[rt].l && rr>=q[rt].r){q[rt].key = id;q[rt].flag = true;return;}Push_down(rt);int mid = (l + r) >> 1;updata(ll, rr, id, l , mid , rt<<1);updata(ll, rr, id, mid+1, r, rt<<1|1);}int main(){int t;scanf("%d",&t);while(t–){memset(v,0,sizeof(v));int n;scanf("%d",&n);int tem = 0;for(int i=1; i<=n; i++){scanf("%d%d",&point[i].a,&point[i].b);point[i].key = i;data1[++tem] = point[i].a;data1[++tem] = point[i].b;}sort(data1+1,data1+tem+1);int w = 0;data1[0] = -1;for(int i=1; i<=tem; i++){if(data1[i] != data1[i-1]){data1[++w] = data1[i];}}tem = w;w = 0;data2[++w] = data1[1];for(int i=2; i<=tem; i++){if(data1[i] > data1[i-1] + 1){data2[++w] = data1[i] – 1;data2[++w] = data1[i];}else{data2[++w] = data1[i];}}build(1,w,1);for(int i=1; i<=n; i++){updata(point[i].a, point[i].b, point[i].key, 1, w, 1);}ans = 0;query(1, w ,1);printf("%d\n",ans);}return 0;}</span>

其实只要你愿意,一切都可以变得很容易。

POJ 2528 Mayors posters(线段树+离散化)

相关文章:

你感兴趣的文章:

标签云: