POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

博客围绕 Mayor's posters 题目展开,题目是贴海报,给出每张海报起点和终点,求最后能看见的海报数量。解题时因海报数据范围大,采用区间离散化缩小数据范围并保留区间性质,离散化后进行线段树区间更新,查询时在 Query() 里判重以发挥线段树本领,最后给出 AC 代码。

任意门:http://poj.org/problem?id=2528

Mayor's posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 77814 Accepted: 22404

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

Source

 

题目大意:

贴海报,给出每张海报的起点和终点,问最后能看见多少张海报(后来的海报会把前面的覆盖);

解题思路:

海报的数据范围太大了,如果按照海报的数据范围开线段树会放不下而且浪费内存。所以考虑离散化,但是这不同于单点离散化,这是区间离散化,意思就是说离散化原来数据的同时还要保留他们的区间的性质。(大神的栗子:原数据“1-10, 1-4,6-10”,常规单点离散化之后“1-4,1-2,3-4”,原本不相邻的点变成邻居了,如果继续按照这种错误的数据建树,那么后果是直接失去了区间的性质) 如果我们想缩小这两点的数据范围,但又想保留不相邻两点表示一个区间的性质,区间离散化要这样搞:缩小数据范围就是按原本数据大小进行排序,他们的先后性保持不变,新下标暂时替代他们,而保留不相邻两点的性质嘛,小脑洞:在他们之间加一个值把他们隔离,这样他们排序之后就不会相邻啦。

离散化之后就还是常规的线段树区间更新,但最后求解的区间查询操作如果暴力单点查询找出有几种不同的海报这样太委屈线段树了,这里的查询只要这张海报类型出现了,那么我们的答案肯定就要加加,然后这张海报我们就可以book一下,下次如果见到有book的海报类型也不需要再更新答案了,这些判重的操作可以直接在Query()里面做,这样才能更好的发挥线段树的本领。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define INF 0x3f3f3f3f
 6 #define LL long long int
 7 #define lson l, mid, root<<1
 8 #define rson mid+1, r, root<<1|1
 9 using namespace std;
10 const int MAXN = 1e4+10;
11 struct date
12 {
13     int L, R;
14 }node[MAXN];
15 int num[MAXN<<3];
16 int lazy_tag[MAXN<<4];
17 bool book[MAXN<<1];
18 int res, cnt;
19 int read()
20 {
21     int f=1, res=0;char s=getchar();
22     while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
23     while(s>='0'&&s<='9'){res=res*10+s-'0';s=getchar();}
24     res*=f;
25     return res;
26 }
27 void PushD(int root)
28 {
29     if(lazy_tag[root]){
30         lazy_tag[root<<1] = lazy_tag[root];
31         lazy_tag[root<<1|1] = lazy_tag[root];
32         lazy_tag[root] = 0;
33     }
34 }
35 
36 void Update(int st, int ed, int l, int r, int root, int val)
37 {
38     if(st <= l && ed >= r){lazy_tag[root] = val; return;}
39     PushD(root);
40     int mid = (l+r)>>1;
41     if(st <= mid) Update(st, ed, lson, val);
42     if(ed > mid)  Update(st, ed, rson, val);
43 }
44 
45 void Query(int l, int r, int root)
46 {
47     if(lazy_tag[root]){
48         if(!book[lazy_tag[root]]) res++;
49         book[lazy_tag[root]] = true;
50         return;
51     }
52     if(l == r) return;
53     int mid = (l+r)>>1;
54     Query(lson); Query(rson);
55 }
56 void init()
57 {
58     cnt = 0; res = 0;
59     memset(lazy_tag, 0, sizeof(lazy_tag));
60     memset(book, false, sizeof(book));
61 }
62 int main()
63 {
64     int T_case, N;
65     T_case = read();
66     while(T_case--)
67     {
68         init();
69         N = read();
70         for(int i = 0; i < N; i++){
71             node[i].L = read();node[i].R = read();
72             num[cnt++] = node[i].L;
73             num[cnt++] = node[i].R;
74         }
75         sort(num, num+cnt);
76         cnt = unique(num, num+cnt)-num;
77         int top = cnt;
78         for(int i = 0; i < top-1; i++){
79             if(num[i+1]!=num[i]+1) num[cnt++] = num[i]+1;
80         }
81 
82         sort(num, num+cnt);
83         /*
84         for(int i = 0; i < cnt; i++)
85             printf("%d ", num[i]);
86         puts("");
87         */
88         for(int i = 0; i < N; i++){
89             int st = lower_bound(num, num+cnt, node[i].L)-num;
90             int ed = lower_bound(num, num+cnt, node[i].R)-num;
91             Update(st+1, ed+1, 1, cnt, 1, i+1);
92         }
93         Query(1, cnt, 1);
94         printf("%d\n", res);
95     }
96     return 0;
97 }
View Code

 

内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值