BNUOJ 2528 Mayor's posters

Mayor‘s posters

Time Limit: 3000ms

Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 10587
64-bit integer IO format: %lld      Java class name: Main

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.

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 lili+1 ,... , ri.

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

Output for sample input

4

解题:线段树+离散化。挂了几次,居然还有贴在10-10这样位置的数据,简直太疯狂了。。这能贴么,一个点啊!好吧,改正后,终于Ac 了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #include <map>
14 #define LL long long
15 #define pii pair<int,int>
16 #define INF 0x3f3f3f3f
17 using namespace std;
18 const int maxn = 100100;
19 set<int>st;
20 int a[maxn],b[maxn];
21 struct node{
22     int lt,rt,flag;
23 };
24 node tree[maxn<<2];
25 int lisan[maxn<<2];
26 void build(int lt,int rt,int v){
27     tree[v].lt = lt;
28     tree[v].rt = rt;
29     tree[v].flag = 0;
30     if(lt + 1 == rt) return;
31     int mid = (lt+rt)>>1;
32     build(lt,mid,v<<1);
33     build(mid,rt,v<<1|1);
34 }
35 void update(int lt,int rt,int v,int val){
36     if(lisan[tree[v].lt] == lt && lisan[tree[v].rt] == rt){
37         tree[v].flag = val;
38         return;
39     }
40     if(tree[v].flag){
41         tree[v<<1].flag = tree[v<<1|1].flag = tree[v].flag;
42         tree[v].flag = 0;
43     }
44     int mid = (tree[v].lt+tree[v].rt)>>1;
45     if(rt <= lisan[mid]){
46         update(lt,rt,v<<1,val);
47     }else if(lt >= lisan[mid]){
48         update(lt,rt,v<<1|1,val);
49     }else{
50         update(lt,lisan[mid],v<<1,val);
51         update(lisan[mid],rt,v<<1|1,val);
52     }
53 }
54 void query(int v){
55     if(tree[v].flag){
56         if(!st.count(tree[v].flag)) st.insert(tree[v].flag);
57         return;
58     }
59     if(tree[v].lt+1 == tree[v].rt) return;
60     query(v<<1);
61     query(v<<1|1);
62 }
63 int main() {
64     int t,i,j,n,cnt,tot;
65     scanf("%d",&t);
66     while(t--){
67         tot = 1;
68         scanf("%d",&n);
69         for(i = 1; i <= n; i++){
70             scanf("%d %d",a+i,b+i);
71             if(a[i] > b[i]) swap(a[i],b[i]);
72             lisan[tot++] = a[i];
73             lisan[tot++] = ++b[i];
74         }
75         sort(lisan+1,lisan+tot);
76         cnt = 1;
77         for(i = 2; i < tot; i++){
78             if(lisan[i] == lisan[cnt]) continue;
79             lisan[++cnt] = lisan[i];
80         }
81         build(1,cnt,1);
82         for(i = 1; i <= n; i++) update(a[i],b[i],1,i);
83         st.clear();
84         query(1);
85         printf("%d\n",st.size());
86     }
87     return 0;
88 }

BNUOJ 2528 Mayor's posters

时间: 2024-08-12 04:45:25

BNUOJ 2528 Mayor's posters的相关文章

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

POJ 2528——Mayor&#39;s posters——————【线段树区间替换、找存在的不同区间】

Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been plac

(中等) POJ 2528 Mayor&#39;s posters , 离散+线段树。

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

PKU 2528 Mayor&#39;s posters

题意: 一个公告板上面贴海报,宽度都是一样的,长度可能不一样.后面的海报可能把前面的覆盖掉.问最后能看见多少张不同的海报. 思路: 这题原来做过,是用线段树的区间染色写的.记录每个区间是纯色还是杂色.最后统计所有颜色. 今天发现可以用一种类似扫描线的想法来做.想象一条扫描线从左往右走.用set来维护当前位置对应的海报集合.然后记录当前位置最新(能被看到)的海报是哪一张. 最后统计一下能被看见的海报数量. 代码: 1 #include <iostream> 2 #include <cstd

POJ 2528 Mayor&#39;s posters(线段树区间染色+离散化或倒序更新)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

线段树+离散化 POJ 2528 Mayor&#39;s posters

题目传送门 题意:在一面墙上贴海报,有先后顺序,问最后有多少张不同的海报(指的是没被覆盖或者只是部分覆盖的海报) 分析:这题数据范围很大,直接搞超时+超内存,需要离散化:离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

POJ 2528 Mayor&#39;s posters (hash+线段树成段更新)

题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW.后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报.现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到.) 思路:简单的成段更新,但是数据量是1千万,会MT,所以要区间压缩(离散化),保证覆盖的关系不变,离散化的时候有个易错的细节,poj数据水了,这个易错点引用h