E - Delete a Segment

CodeForces E题解析
本文详细解析CodeForces竞赛E题,探讨如何通过离散化和前缀和技巧求解最大段数问题,实现高效算法设计。

E - Delete a Segment

https://codeforces.com/contest/1285/problem/E
There are n segments on a Ox axis [l1,r1], [l2,r2], …, [ln,rn]. Segment [l,r] covers all points from l to r inclusive, so all x such that l≤x≤r.

Segments can be placed arbitrarily — be inside each other, coincide and so on. Segments can degenerate into points, that is li=ri is possible.

Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example:

if n=3 and there are segments [3,6], [100,100], [5,8] then their union is 2 segments: [3,8] and [100,100];
if n=5 and there are segments [1,2], [2,3], [4,5], [4,6], [6,6] then their union is 2 segments: [1,3] and [4,6].
Obviously, a union is a set of pairwise non-intersecting segments.

You are asked to erase exactly one segment of the given n so that the number of segments in the union of the rest n−1 segments is maximum possible.

For example, if n=4 and there are segments [1,4], [2,3], [3,6], [5,7], then:

erasing the first segment will lead to [2,3], [3,6], [5,7] remaining, which have 1 segment in their union;
erasing the second segment will lead to [1,4], [3,6], [5,7] remaining, which have 1 segment in their union;
erasing the third segment will lead to [1,4], [2,3], [5,7] remaining, which have 2 segments in their union;
erasing the fourth segment will lead to [1,4], [2,3], [3,6] remaining, which have 1 segment in their union.
Thus, you are required to erase the third segment to get answer 2.

Write a program that will find the maximum number of segments in the union of n−1 segments if you erase any of the given n segments.

Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly n−1 segments.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases in the test. Then the descriptions of t test cases follow.

The first of each test case contains a single integer n (2≤n≤2⋅105) — the number of segments in the given set. Then n lines follow, each contains a description of a segment — a pair of integers li, ri (−109≤li≤ri≤109), where li and ri are the coordinates of the left and right borders of the i-th segment, respectively.

The segments are given in an arbitrary order.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
Print t integers — the answers to the t given test cases in the order of input. The answer is the maximum number of segments in the union of n−1 segments if you erase any of the given n segments.

题意:有n段闭区间,先要删除其中的一段,将其余的段并起来得到x个不相交的段,问x的最大值
思路:首先,我们很容易求得不删任何一段时不相交的段数,现考虑删除一段时会增加的答案数,先将所有的段离散化(注意因为前一段的结尾和后一段的开始部分如果相邻会有影响,所以得在中间插入一个点),用a数组记录下每个位置现有的段数,很明显当a[i]>1&&a[i-1]<=1时,位置i为一个新的段的开始处且被另一段连接起来的,只要删除那一段答案数就可以加一,将这个结果的前缀和用sum数组记录下来,但是要注意我们只记录了删除一段会有多少个新段开始处被暴露,但还存在一种情况就是删除的那一段左端点处会暴露出一个新段,但没有记录在sum数组中(因为并不是新段开始位置),即a[nod[i].l] > 1 && a[nod[i].l-1] > 1的情况,得加上1,然后因为删除了一段最终结果要减1,这样即可得到答案

#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
int n,cnt,b[MAXN*6],in[MAXN*6],out[MAXN * 6],a[MAXN*6],sum[MAXN * 6];
struct node
{
	int l,r;
}nod[MAXN];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		cnt = 0;
		for(int i = 1;i <= n;++i)
		{
			scanf("%d%d",&nod[i].l,&nod[i].r);
			nod[i].l *= 2,nod[i].r *= 2;
			b[++cnt] = nod[i].l,b[++cnt] = nod[i].r;
			b[++cnt] = nod[i].l-1,b[++cnt] = nod[i].l + 1;
			b[++cnt] = nod[i].r-1;b[++cnt] = nod[i].r+1;
		}
		sort(b+1,b+cnt+1);
		cnt = unique(b+1,b+cnt+1)-b;
		memset(in,0,sizeof(int)*(cnt+1));
		memset(out,0,sizeof(int)*(cnt+1));
		for(int i = 1;i <= n;++i)
		{
			nod[i].l = lower_bound(b+1,b+cnt+1,nod[i].l)-b;
			nod[i].r = lower_bound(b+1,b+cnt+1,nod[i].r)-b;
			++in[nod[i].l],++out[nod[i].r];
		}
		a[0] = 0;
		int tmp = 0;
		for(int i = 1;i <= cnt;++i)
		{
			tmp += in[i];
			a[i] = tmp;
			tmp -= out[i];
		}
		int ans = 0;
		sum[0] = 0;
		for(int i = 1;i <= cnt;++i)
		{
			sum[i] = sum[i-1] + (a[i] > 1 && a[i-1] <= 1);
			ans += (a[i] > 0 && a[i-1] == 0);
		}
		int maxn = 0;
		for(int i = 1;i <= n;++i)
		{
			int add = sum[nod[i].r]-sum[nod[i].l-1];
			if(a[nod[i].l] > 1 && a[nod[i].l-1] > 1)
				++add;
			maxn = max(maxn,add);
		}
		printf("%d\n",ans+maxn-1);
	}
	return 0;
}
Example Packet Analyzer Commands This section is applicable for the following families of devices: a Aldrin a Aldrin2 a Gen6 devices Packet Analyzer commands require switching to Packet Analyzer context (within Debug context) using the packet-analyzer command. Use the exit command to exit any of the Packet Analyzer sub-contexts. To use the Packet Analyzer commands, the debugged device iDebug XML file must be located in the appropriate location. XML files are under cpss/tools/admin/iDebug. Copy them to either of the following locations, depending on the desired platform: For Linux target devices (black mode), to /usr/bin For Linux simulation, to ~/embeddedFs/CHIP_SIMULATION_03_394/ For Windows simulation, to C:\Users\<user-name>\AppData\Local\Temp\embeddedFs\CHIP_SIMULATION_03_394\ Packet Analyzer configuration and operation is preserved even when outside the Packet Analyzer context, enabling re-entrance for sampled data viewing. See Lua CLI Support section in CPSS User Guide for more details. clear Use the clear command to reset the sampling counter which counts the number of samplings in each Stage. Using this command ensures accurate count results between separate samplings. Syntax clear Parameters None Command Context Packet-Analyzer context Example Console(debug.0-pa-1)# clear configure pipeline Use the configure pipeline command to configure a Packet Analyzer to operate in pipeline mode. Using this command transitions to a sub-context for configuring pipeline mode parameters. Syntax configure pipeline Parameters None Command Context Packet-Analyzer context Example To configure Pipeline mode: Console(debug)# packet-analyzer manager 1 device 0 Console(debug.pa-1)# configure pipeline Console(debug.0-pa-1-pipeline)# configure pipeline packet-trace Use the configure pipeline packet-trace command to configure a Packet Analyzer to operate in pipeline mode and enable packet-trace option. Using this command transitions to a sub-context for configuring pipeline mode parameters. See explanation on packet trace usage in Packet Analyzer section CPSS User Guide. This command is not available for Aldrin and Aldrin2. Syntax configure pipeline packet-trace Parameters None Command Context Packet-Analyzer context Example To configure Pipeline mode: Console(debug)# packet-analyzer manager 1 device 0 Console(debug.pa-1)# configure pipeline packet-trace Console(debug.0-pa-1-pipeline-packet-trace)# configure stage Use the configure stage command to set the Stage to use in stage mode. Using this command transitions to a sub-context for configuring stage mode parameters of the specified stage, as demonstrated by the prompt containing the stage name. When trying to configure an inactive stage muxed with a currently active one, an error message is issued naming the currently active stage. Following deactivation of the latter, reuse this command to activate the desired stage. Use the no form of the configure stage command to stop sampling the specified stage, as well as deactivating a stage prior to activating any of its muxed stages. Syntax configure stage <stageName> no configure stage <stageName> Parameters stageName - Stage name; replacing stageName with a ‘?’ yields a list of stage names available for configuration. Stage name may be either a pre-defined or a user-defined Stage Command Context Packet-Analyzer context Example To obtain a list of Stage names to configure: Console(debug.0-pa-1)# configure stage ? Packet-Analyzer stage bridge Pre Bridge ingress stage e-filter Pre Filter egress stage e-oam Pre OAM egress stage e-pcl Pre PCL egress stage e-policer Pre Policer egress stage equeue Pre e-Queue ingress stage header-alt Pre Header Alteration egress stage i-oam Pre OAM ingress stage i-pcl Pre PCL0,1,2 ingress tages i-policer Pre Policer ingress stage mac Pre MAC egress stage mll Pre MLL ingress stage pha Pre programmable Header Alteration egress stage (falcon only) replication Pre replication egress stage (falcon only) router Pre Router ingress stage timestamp Pre Timestamp egress stage tti Pre TTI0,1 ingress stages tx-queue Pre tx-Queue egress stage <CR> Select stage Console(debug.0-pa-1)# To configure the Stage named i-pcl: Console(debug.pa-1)# configure stage i-pcl Console(debug.0-pa-1-i-pcl)# disable Use the disable command to cease sampling before viewing the sampling results. Syntax disable Parameters None Command Context Packet Analyzer context Example Sample the Pipeline for descriptors that came for port 5: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# match local_dev_src_port 5 Console(debug-pa-1-pipeline)# exit Console(debug-pa-1)# enable pipeline Run traffic, and then: Console(debug-pa-1)# disable enable pipeline Use the enable pipeline command to apply the configuration set by the configure pipeline command to the device hardware, and begin sampling in pipeline mode. Syntax enable pipeline Parameters None Command Context Packet Analyzer context Example Sample the Pipeline for descriptors Ingressing through port 5: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# match local_dev_src_port 5 Console(debug-pa-1-pipeline)# exit Console(debug-pa-1)# enable pipeline enable stage Use the enable stage command to apply the configuration set by the configure stage command(s) to the device hardware, and begin sampling in stage mode. Syntax enable stage Parameters None Command Context Packet Analyzer context Example Sample Stage i-pcl for descriptors with source port 5 and e-pcl for descriptors with egress port 6: Console(debug.0-pa-1)# configure stage i-pcl Console(debug-pa-1-i-pcl)# match local_dev_src_port 5 Console(debug-pa-1-i-pcl)# exit Console(debug.0-pa-1)# configure stage e-pcl Console(debug-pa-1-i-pcl)# match trg_phy_port 6 Console(debug-pa-1-i-pcl)# exit Console(debug-pa-1)# enable stage Stages must be configured in order to be enabled in Stage mode inverse Use the inverse command to sample descriptors not matching all configured filters in Pipeline or Stage mode. Due to this command requiring none of the filters is matched (AND between all NOTs), use it sparingly and carefully. This command affects sampling at all Stages when in Pipeline mode, and at the currently configured Stage when in Stage mode. Syntax inverse Parameters None Command Context Pipeline Configuration and Stage Configuration contexts Example To match in Pipeline mode on ingress port number other than 5: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# match local_dev_src_port 5 Console(debug-pa-1-pipeline)# inverse Console(debug-pa-1-pipeline)# match Use the match command to filter descriptors with a specific field value when configuring Pipeline or a Stage for sampling. This allows limiting sampling to descriptors with specific values in specific fields. A mask can be used in conjunction with the value, when only a field segment is of interest, such as domain within and IP address. In Pipeline configuration context, the field must exist in at least one of the stages within the Pipeline. In Stage configuration context, the field must exist in the configured Stage. The filtered fields are marked for display in the show pipeline or show stage commands Syntax match <fieldName> <fieldValue> [mask <maskValue>] Parameters fieldName - Name of field within one of the Pipeline stages or in the configured Stage fieldValue - Value of field to match maskValue - Mask limiting matching to a field value segment; for example, in an IPV4 field, a 255.255.255.0 mask limits matching to the domain; 0 disable filtering according to field value, meaning this field is of interest regardless of its value User Guidelines Field and mask values are specified in terms of the field type: Numeric values - A decimal number Hex values - Hex number preceded by a 0x prefix IP address - IP address formatted as xxx.xxx.xxx.xxx or xxx:xxx:xxx:xxx:xxx MAC Address - MAC address formatted as xx:xx:xx:xx:xx:xx Replacing field value with ?, yields a list of possible field values. Setting a 0 (0x0) mask merely marks the field for display in the show command. Command Context Pipeline configuration and Stage configuration contexts Examples To sample ingress port number 5 in Pipeline mode: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# match local_dev_src_port 5 Console(debug-pa-1-pipeline)# To sample a domain value of 192.168.1 in an IPv4 destination address while in the ‘router’ Stage: Console(debug.0-pa-1)# configure stage router Console(debug-pa-1-router)# match ipv4_dip 192.168.1.1 mask 255.255.255.0 Console(debug-pa-1-router)# To mark local_dev_src_port field for the show command only; no match: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# match local_dev_src_port 0 mask 0 Console(debug-pa-1-pipeline)# packet-analyzer Use the packet-analyzer Debug context command to create a Packet Analyzer Manager for the specified device, and enter Packet Analyzer context. If the specified Manager ID is already in use, switches to this existing manager. It is possible to create several concurrent managers with varying configurations, and use them at separate times. Syntax packet-analyzer [manager <managerId>] device <devId> Parameters managerId - Packet Analyzer manager ID; when omitted, a manager with ID = 1 is created, or if already exists, switches to its working context devId – Device ID Command Context Debug context Example Console(debug)# packet-analyzer manager 1 device 0 Console(debug.pa-1)# sampling-mode Use the sampling-mode command to configure Packet Analyzer to sample the first or last of the descriptors matching the sampling criteria. Regardless of the sampling mode, the hit counter continues counting until sampling is disabled. If this command is not used, last-match sampling mode is used Syntax sampling-mode {first-match | last-match} Parameters first-match - Sample first descriptor matching sampling criteria last-match - Continue sampling and matching descriptor for sampling criteria Command Context Pipeline Configuration and Stage Configuration contexts Example To sample the first descriptor in all Pipeline Stages: Console(debug.0-pa-1)# configure pipeleine Console(debug-pa-1-pipeline)# sampling-mode first-match Console(debug-pa-1-pipeline)# To sample the first descriptor in i-pcl Stage, and the last descriptor in the router Stage: Console(debug.0-pa-1)# configure stage i-pcl Console(debug-pa-1-i-pcl)# sampling-mode first-match Console(debug-pa-1-i-pcl)# exit Console(debug.0-pa-1)# configure stage router Console(debug-pa-1-router)# sampling-mode last-match Console(debug-pa-1-router)# exit Console(debug-pa-1)# show pipeline Use the show pipeline command to display the sampled pipeline-mode data. The displayed data includes, in this order: List of Pipeline Stages, and number of matches found in each List of fields used in filters (configured using the match command) Sampling mode – First/last, or inverse List of all stages and sampled values, if any (no value signifies no found match) Note: Field list depends on command parameter Syntax show pipeline [field {<fieldName> | all | dump}] Parameters fieldName - Field to display all - Display values of all defined fields (both pre and user-defined) dump - Display values of all fields - predefined, user-defined, and unnamed - in all Stages Command Context Packet Analyzer context Example Console(debug.pa-1)# configure pipeline Console(debug.0-pa-1-pipeline)# match local_dev_src_port 1 Console(debug.0-pa-1-pipeline)# exit Console(debug.pa-1)# enable pipeline Console(debug.pa-1)# disable Console(debug.pa-1)# show pipeline Hits: tti : 165719975 i-pcl : 182160528 bridge : 181962972 router : 179923992 i-oam : 179307633 i-policer : 178133064 equeue : 174874642 e-filter : 173543688 header-alt : 0 e-pcl : 0 e-policer : 0 replication : 0 mac : 666079946 Match : local_dev_src_port = 1 mask = 0x3FF Sampled data (first-match): Stage: tti local_dev_src_port = 1 Stage: i-pcl local_dev_src_port = 1 Stage: bridge local_dev_src_port = 1 Stage: router local_dev_src_port = 1 Stage: i-oam local_dev_src_port = 1 Stage: i-policer local_dev_src_port = 1 Stage: equeue local_dev_src_port = 1 Stage: e-filter local_dev_src_port = 1 Stage: header-alt NONE Stage: e-pcl NONE Stage: e-policer NONE Stage: replication NONE Stage: mac Console(debug.pa-1)# show stage Use the show stage command to display the sampled stage-mode data. The displayed data per stage includes, in this order: Stage name, and number of matches found in it List of fields used in Stage filters (configured using the match command) Sampling mode – first/last, inverse List of fields and sampled values, if any (no value signifies no found match) Syntax show stage <stageName> [field {<fieldName> | all | dump}] Parameters stageName - Name of Stage to display field - Field to display. When omitted, all matched fields are displayed fieldName - Field to display all - Display values of all defined fields (both pre and user-defined) dump - Display values of all fields - predefined, user-defined, and unnamed - in the specified Stage Command Context Packet Analyzer context Example Console(debug.pa-1)# configure stage bridge Console(debug.0-pa-1-bridge)# Console(debug.0-pa-1-bridge)# match mac_da 00:00:00:00:00:00 Console(debug.0-pa-1-bridge)# exit Console(debug.pa-1)# enable stage Console(debug.pa-1)# disable Console(debug.pa-1)# show stage bridge field mac_da Stage: bridge Hits: 0 Match : mac_da = 00:00:00:00:00:00 mask = 0xFF:FF:FF:FF:FF:FF Sampled data (last-match): NONE Console(debug.pa-1)# show user-defined stage Use the show user-defined stage Exec context command to display the list of user defined Stages. Syntax show user-defined stage [all] Parameters all - Display information, including Stage index, on all Stages, including pre-defined ones. When omitted, display only user-defined Stages Command Context Packet Analyzer context Example Console(debug.pa-1)# show user-defined stage Stage Name | Interface | Instance | Leg | Status | Index ---------------- --------------- ------------- ------- -------- -------- stage1 i22 s32 1 invalid 0 stage2 i10 s123 2 valid 1 Status may be one of: invalid – Stage is inactive valid – Stage is active To activate a Stage, use the configure stage command. user-defined field Use the user-defined field Packet Analyzer context command to add a field to the list of defined fields. Use the no form of this command to delete the user-defined field definition. Number of UDFs is limited to 64 Syntax user-defined field <udfName> field-id <udfId> no user-defined field <udfName> Parameters udfName - Name to assign to new user-defined field udfId - Field ID in system, as supplied by a Marvell engineer Command Context Packet Analyzer context Example Console(debug-pa-1-router)#user-defied field myField field-id f50 Console(debug.0-pa-1)# no user-defined field myField user-defined stage Use the user-defined stage command to add a Stage to the list of defined Stages. Use the no form of this command to delete the user-defined Stage definition. Number of UDSes is limited to 20 New stages require activation before their usage, and when muxed with another stage, require that the currently muxed stage is deactivated, and only then can they be activated: Utilize the configure pipeline command to activate a new stage, or if muxed, to obtain the name of the currently active muxed stage Use the no form of the configure pipeline command to deactivate the muxed stage, and then re-use configure pipeline to activate the new stage Syntax user-defined stage <udsName> interface <interfaceId> instance <instanceId> leg <instanceLeg> no user-defined stage <udsName> Parameters udsName - Name to assign to new user-defined Stage interfaceId - i-Debug interface ID, as supplied by a Marvell engineer instanceId - ID of i-Debug instance connected the Interface, as supplied by a Marvell engineer instanceLeg - Leg of Interface in Instance, as supplied by a Marvell engineer Command Context Packet Analyzer context Example Console(debug-pa-1-router)#user-defined stage MyStage interface i22 instance s32 leg 0 Console(debug.0-pa-1)# no user-defined stage <udsName> PHY Commands phy 10G register read Use the phy 10G register read Debug context commands to read the specified SMI register and 10G PHY device. Syntax phy 10g register read device {all | <devNum>} port <PortNum> register <reg> phyDev <phyDev_Num> phyID <phyID_Num> Parameters all |devNum – All devices or a specific device PortNum – Port number reg – Register to read, specified as an 16-bit unsigned integer phyDev_Num – PHY device number; range: 0-31 phyID_Num – PHY ID 翻译并解释
最新发布
10-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值