18.10.16 POJ 2528 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. 
输入

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.

输出

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.

样例输入

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

样例输出

4

来源

Alberta Collegiate Programming Contest 2003.10.18

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #include <set>
13 
14 using namespace std;
15 const int maxn = 10005;
16 int poster[maxn][2],n,cont,ans;
17 int p[maxn * 2];
18 bool flagc;
19 struct node {
20     int l, r;
21     bool covered;
22 }tree[maxn*8];
23 
24 void build(int root,int l,int r) {
25     tree[root].l = l, tree[root].r = r;
26     tree[root].covered = false;
27     if (l == r)return;
28     int mid = (l + r) / 2;
29     build(root * 2, l, mid);
30     build(root * 2 + 1, mid + 1, r);
31 }
32 
33 void query(int root, int s, int e) {
34     if (tree[root].covered) 
35         return;
36     if (p[tree[root].l] == s && p[tree[root].r] == e) {
37         tree[root].covered = true;
38         flagc = true;
39         return;
40     }
41     int mid = (tree[root].l + tree[root].r) / 2;
42     if (s > p[mid])
43         query(root * 2 + 1, s, e);
44     else if (e <= p[mid])
45         query(root * 2, s, e);
46     else
47     {
48         query(root * 2, s, p[mid]);
49         query(root * 2 + 1, p[mid + 1], e);
50     }
51     tree[root].covered = tree[root * 2].covered&&tree[root * 2 + 1].covered;
52     return;
53 }
54 
55 void init() {
56     scanf("%d", &n);
57     cont = 0,ans=0;
58     for (int i = 1; i <= n; i++) {
59         int s, e;
60         scanf("%d%d", &s, &e);
61         poster[i][0] = s, poster[i][1] = e;
62         p[++cont] = s, p[++cont] = e;
63     }
64     sort(p + 1, p + cont + 1);
65     cont = unique(p + 1, p + cont + 1)-p-1;
66     build(1, 1, cont);
67     for (int i = n; i >= 1; i--) {
68         flagc = false;
69         query(1, poster[i][0], poster[i][1]);
70         if(flagc)
71             ans++;
72     }
73     printf("%d\n", ans);
74 }
75 
76 
77 int main()
78 {
79     int kase;
80     scanf("%d", &kase);
81     while (kase--) {
82         init();
83     }
84     return 0;
85 }
View Code

其实这个程序是错的,只是能过测评而已,到听了题解再说吧

因为当两张海报只隔一个区域时,中间那块其实是空出来的,但这个答案就会直接忽略认为中间那块也是满的

但问题在于,如果将函数 query() 换成:

 1 bool query(int root, int s, int e) {
 2     if (tree[root].covered) 
 3         return false;
 4     if (p[tree[root].l] == s && p[tree[root].r] == e) {
 5         tree[root].covered = true;
 6         return true;
 7     }
 8     int mid = (tree[root].l + tree[root].r) / 2;
 9     bool flag;
10     if (s > p[mid])
11         flag=query(root * 2 + 1, s, e);
12     else if (e <= p[mid])
13         flag=query(root * 2, s, e);
14     else
15     {
16         bool f1=query(root * 2, s, p[mid]);
17         bool f2=query(root * 2 + 1, p[mid + 1], e);
18         flag = f1 || f2;
19     }
20     tree[root].covered = tree[root * 2].covered&&tree[root * 2 + 1].covered;
21     return flag;
22 }
View Code

就会WA……

完全不知道为啥……明明感觉是完全等价的,到之后再看吧……

=====================update========================

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #include <set>
13 
14 using namespace std;
15 const int maxn = 10005;
16 int poster[maxn][2], n, cont, ans;
17 int p[maxn * 2];
18 bool flagc;
19 struct node {
20     int l, r;
21     bool covered;
22 }tree[maxn * 8];
23 
24 void build(int root, int l, int r) {
25     tree[root].l = l, tree[root].r = r;
26     tree[root].covered = false;
27     if (l == r)return;
28     int mid = (l + r) / 2;
29     build(root * 2, l, mid);
30     build(root * 2 + 1, mid + 1, r);
31 }
32 
33 void query(int root, int s, int e) {
34     if (tree[root].covered)
35         return;
36     if (p[tree[root].l] == s && p[tree[root].r+1] == e) {
37         tree[root].covered = true;
38         flagc = true;
39         return;
40     }
41     int mid = (tree[root].l + tree[root].r) / 2;
42     if (s >= p[mid+1])
43         query(root * 2 + 1, s, e);
44     else if (e <= p[mid+1])
45         query(root * 2, s, e);
46     else
47     {
48         query(root * 2, s, p[mid+1]);
49         query(root * 2 + 1, p[mid + 1], e);
50     }
51     tree[root].covered = tree[root * 2].covered&&tree[root * 2 + 1].covered;
52     return;
53 }
54 
55 void init() {
56     scanf("%d", &n);
57     cont = 0, ans = 0;
58     for (int i = 1; i <= n; i++) {
59         int s, e;
60         scanf("%d%d", &s, &e);
61         poster[i][0] = s, poster[i][1] = e+1;
62         p[++cont] = s, p[++cont] = e+1;
63     }
64     sort(p + 1, p + cont + 1);
65     cont = unique(p + 1, p + cont + 1) - p - 1;
66     build(1, 1, cont-1);
67     for (int i = n; i >= 1; i--) {
68         flagc = false;
69         query(1, poster[i][0], poster[i][1]);
70         if (flagc)
71             ans++;
72     }
73     printf("%d\n", ans);
74 }
75 
76 
77 int main()
78 {
79     int kase;
80     scanf("%d", &kase);
81     while (kase--) {
82         init();
83     }
84     return 0;
85 }
View Code

 

好吧,之前我也没有理解对qwq

看错题了qwq

改起来还算不麻烦……吧……

 

转载于:https://www.cnblogs.com/yalphait/p/9796424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值