UVA10587 Mayor‘s posters 题解 线段树 离散化

UVA10587 Mayor’s posters

传送门(有亿点慢,需要耐心)

PDF
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 e l e c t o r a l electoral electoral w a l l wall 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 ( b y t e byte 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 c c giving the number of cases that follow. The first line of data for a single case contains number 1 ≤ n ≤ 10000 1\le n\le 10000 1n10000. The subsequent n lines describe the posters in the order in which they were placed. The i i i-th line among the n lines contains two integer numbers l i l_i li and r i r_i 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\le i\le n 1in, 1 ≤ l i ≤ r i ≤ 10000000 1\le l_i\le r_i\le 10000000 1liri10000000. After the i i i-th poster is placed, it entirely covers all wall segments numbered l i , l i + 1 , . . . , r i l_i, l_i+1 ,... , r_i li,li+1,...,ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

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

4

题目翻译

AB 省 B y t e Byte Byte 镇的市民们无法忍受市长竞选活动中的候选人随心所欲地在各个地方张贴选举海报。市议会最终决定建造一面选举墙,用于张贴海报,并出台了以下规则:

  • 每位候选人只能在墙上张贴一张海报。
  • 所有海报的高度相同,等于墙的高度;海报的宽度可以是任意整数字节(字节是 B y t e Byte Byte 镇的长度单位)。
  • 墙面被分成若干段,每段的宽度为一个字节。
  • 每张海报必须完全覆盖连续的墙段。

他们建造了一堵长达 10000000 b y t e byte byte 的墙(这样所有候选人都有足够的位置)。竞选活动重新开始后,候选人开始在墙上张贴海报,他们的海报宽度相差很大。此外,候选人开始将自己的海报张贴在已被其他海报占据的墙面上。 B y t e Byte Byte 镇的每个人都很好奇,在选举前的最后一天,谁的海报还能被看到(全部或部分)。
你们的任务是根据海报的尺寸、在选举墙上的位置和摆放顺序等信息,找出当所有海报都摆放完毕时,能看到的海报数量。

输入格式

第一行输入包含一个数字 c c c,表示后面的案例数。单个案例的第一行数据包含数字 1 ≤ n ≤ 10000 1\le n\le 10000 1n10000。随后的 n n n 行按顺序描述海报。 n n n 行中的第 i i i 行包含两个整数 l i l_i li r i r_i ri,分别是第 i i i 张海报的左端和右端所占墙段的编号。我们知道,每 1 ≤ i ≤ n 1\le i\le n 1in 1 ≤ l i ≤ r i ≤ 10000000 1\le l_i\le r_i\le 10000000 1liri10000000。第 i i i 张海报放置后,它将完全覆盖编号为 l i , l i + 1 , … , r i l_i, l_i+1 ,\dots, r_i li,li+1,,ri 的所有墙段。

输出格式

为每个输入数据集打印所有海报张贴后可见海报的数量。

提示: 下图展示了样例输入的情况。
在这里插入图片描述
以上来自 U V A ,翻译: D e e p L 和本人。 以上来自UVA,翻译: DeepL和本人。 以上来自UVA,翻译:DeepL和本人。

解题思路

这是一道很经典的线段树+离散化。 线段树其实没什么特别的,就是模版。如果还不懂线段树,那就看这里。本题主要需要注意的是离散化

浅浅地讲一下离散化:离散化主要是优化的是空间。这道题有 1 0 7 10^7 107 的数组,再开 4 4 4 倍就要爆空间 了。离散化可以大大减少所需的空间大小。
思想:用简短的数组来表示一个极大的数组。就以这道题来说,假设有一个这样的区间:11111111111111111111,如果用朴素的存放方法需要用 20 20 20 个变量来存,但是,你可以这样表示 L _   A n d _   R 1 = 1 {L\_\ And\_\ R}_1=1 L_ And_ R1=1, C o l o r 1 = 1 Color_1=1 Color1=1, L _   A n d _   R 2 = 20 {L\_\ And\_\ R}_2=20 L_ And_ R2=20, C o l o r 2 = 1 Color_2=1 Color2=1 4 4 4 个变量就存下了。

AC Code

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#define int long long
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define RT return
const int Maxn = 200000 + 5;
int n, L[Maxn], R[Maxn];
int Color[Maxn << 2], L_And_R[Maxn];
bool HasVisit[Maxn];
int Answer = 0;
inline void Push_Down(int rt);
inline void Update(int L, int R, int C, int l, int r, int rt);
inline void Query(int l, int r, int rt);
inline bool Check(int k, int mid);
inline int Binary_Search(int k, int w);
inline void init();
inline void solve();
inline void Work() {
	int T;
	cin >> T;
	while (T--)init(), solve();
}
signed main() {
	fast_io
	Work();
	RT 0;
}
inline void Push_Down(int rt) {
	if (Color[rt] != -1)Color[rt << 1] = Color[rt << 1 | 1] = Color[rt], Color[rt] = -1;
}
inline void Update(int L, int R, int C, int l, int r, int rt) {
	if (L <= l && r <= R) {
		Color[rt] = C;
		RT;
	}
	int mid = (l + r) >> 1;
	Push_Down(rt);
	if (mid >= L)Update(L, R, C, l, mid, rt << 1);
	if (mid < R)Update(L, R, C, mid + 1, r, rt << 1 | 1);
}
inline void Query(int l, int r, int rt) {
	if (Color[rt] != -1) {
		if (!HasVisit[Color[rt]]) Answer++;
		HasVisit[Color[rt]] = 1;
		RT;
	}
	if (l == r) RT;
	int mid = (l + r) >> 1;
	Query(l, mid, rt << 1), Query(mid + 1, r, rt << 1 | 1);
}
inline bool Check(int k, int mid) {
	RT k == L_And_R[mid];
}
inline int Binary_Search(int k, int w) {
	int l = 0, r = w - 1, mid;
	while (l <= r) {
		mid = (l + r) >> 1;
		if (Check(k, mid)) RT mid;
		if (k > L_And_R[mid]) l = mid + 1;
		else r = mid - 1;
	}
	RT - 1;
}
inline void init() {
	Answer = 0;
	memset(HasVisit, 0, sizeof(HasVisit));
	memset(Color, -1, sizeof(Color));
	cin >> n;
	for (int i = 0; i < n; i++)cin >> L[i] >> R[i];
}
inline void solve() {
	int Pre_Length = 0, Real_Length = 1;
	for (int i = 0; i < n; i++)L_And_R[Pre_Length++] = L[i], L_And_R[Pre_Length++] = R[i];
	sort(L_And_R, L_And_R + Pre_Length);
	for (int i = 1; i < Pre_Length; i++)if (L_And_R[i] != L_And_R[i - 1])L_And_R[Real_Length++] = L_And_R[i];
	for (int i = Real_Length - 1; i > 0; i--)if (L_And_R[i] != L_And_R[i - 1] + 1)L_And_R[Real_Length++] = L_And_R[i - 1] + 1;
	sort(L_And_R, L_And_R + Real_Length);
	for (int i = 0; i < n; i++) {
		int l = Binary_Search(L[i], Real_Length), r = Binary_Search(R[i], Real_Length);
		Update(l, r, i, 0, Real_Length, 1);
	}
	Query(0, Real_Length, 1);
	cout << Answer << endl;
}

抱怨此题无正规翻译,damn,bro。(写题解时本人与题面反应生成了大量氧化钙

  • 27
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值