E2. Array and Segments (Hard version)

E2. Array and Segments (Hard version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The only difference between easy and hard versions is a number of elements in the array.

You are given an array aa consisting of nn integers. The value of the ii-th element of the array is aiai.

You are also given a set of mm segments. The jj-th segment is [lj;rj][lj;rj], where 1≤lj≤rj≤n1≤lj≤rj≤n.

You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array a=[0,0,0,0,0]a=[0,0,0,0,0] and the given segments are [1;3][1;3] and [2;4][2;4] then you can choose both of them and the array will become b=[−1,−2,−2,−1,0]b=[−1,−2,−2,−1,0].

You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array aa and obtain the array bb then the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi will be maximum possible.

Note that you can choose the empty set.

If there are multiple answers, you can print any.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains two integers nn and mm (1≤n≤105,0≤m≤3001≤n≤105,0≤m≤300) — the length of the array aa and the number of segments, respectively.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106), where aiai is the value of the ii-th element of the array aa.

The next mm lines are contain two integers each. The jj-th of them contains two integers ljlj and rjrj (1≤lj≤rj≤n1≤lj≤rj≤n), where ljlj and rjrj are the ends of the jj-th segment.

Output

In the first line of the output print one integer dd — the maximum possible value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi if bb is the array obtained by applying some subset of the given segments to the array aa.

In the second line of the output print one integer qq (0≤q≤m0≤q≤m) — the number of segments you apply.

In the third line print qq distinct integers c1,c2,…,cqc1,c2,…,cq in any order (1≤ck≤m1≤ck≤m) — indices of segments you apply to the array aa in such a way that the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi of the obtained array bb is maximum possible.

If there are multiple answers, you can print any.

Examples

input

Copy

5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3

output

Copy

6
2
4 1 

input

Copy

5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5

output

Copy

7
2
3 2 

input

Copy

1 0
1000000

output

Copy

0
0

Note

In the first example the obtained array bb will be [0,−4,1,1,2][0,−4,1,1,2] so the answer is 66.

In the second example the obtained array bb will be [2,−3,1,−1,4][2,−3,1,−1,4] so the answer is 77.

In the third example you cannot do anything so the answer is 00.

不会,看大佬题解:https://blog.csdn.net/Floraqiu/article/details/86632558

以下来自https://blog.csdn.net/Floraqiu/article/details/86632558

题意
给定一个长度为n的序列,和m个区间。
对一个区间的操作是:对整个区间的数-1
可以选择任意个区间(可以为0个、每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值)。
easy version的范围是(1≤n≤300,0≤m≤300)
hard version的范围是(1≤n≤1e5,0≤m≤300)
思路
操作是对区间内的数减1,因此最大值不会变的更大,要使得极差变大,只能够尽量使最小值变小。
如果对一个区间进行操作,可能会有以下情况:
1.这个区间只包含最小值,那么最小值变小,极差变大。 这是我们需要的。
2.这个区间只包含最大值,那么最大值变大,极差变小 。这显然是要避免的。
3.这个区间既包含最大值,又包含最小值,那么最大值和最小值同时变小,且变小幅度一致,极差不变。 这对我们来说是没有影响的。
要最大化极差,显然要尽量进行1操作,不进行2操作,3操作无所谓。所以在选择区间时,只要是包含最小值的,我们都要选择这个区间进行操作。
但是我们不知道最大值,最小值到底在哪里,他们在操作过程中是变化的。比如在样例1中,最开始最大值是第三位3,最后变成了第五位2。
由于最大值最小值的不确定性,我们需要枚举最小值的位置。记录操作过程中,哪个位置作为最终最小值位置时,能够使得极差最大化。
枚举了最小值的位置后,如果暴力进行区间修改操作,其复杂度是很大的,可以过easy version(我的easy version代码就是暴力的)。
精妙之处
我们可以对操作区间进行整理,使得在枚举过程中,不需要重复进行区间操作。
比如我们现在枚举的最小值位置是p,则我们需要选择包含了p的所有区间。
那么p前面一个状态p-1,进行的区间操作是包含了p-1的所有区间,这些p-1区间中,有些是包含了p的,有些是没包含p的,那么我们需要先删除掉没包含p的区间的影响(即把这些区间减掉的1给加回来)。同时还需要补上包含了p但是不包含p-1的区间(这样选择,就不会和p-1里面保留了的区间重复)。
包含了p-1,但不包含p的区间,显然是以p-1结尾的区间。我们用add[]数组保存以r[i]结尾的区间的编号。
包含了p,但不包含p-1的区间,显然是以p开始的区间。我们用sub[]数组保存以l[i]开始的区间的编号。
区间修改操作,就要用到线段树了。
最后输出答案时,使用最终确定的最小值的位置P,包含P的区间留下,不包含P的舍弃。
然后他的代码我感觉查询最大值那里多余了。

下面代码是我优化后的,少了70行代码吧

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int len[4*N];
int a[N],l[N],r[N],mx[4*N],mi[N*4],lazy[N*4],ans[N];
vector<int>add[N],sub[N]; 
void pushup(int id)
{
	mx[id]=max(mx[id<<1],mx[id<<1|1]);
	mi[id]=min(mi[id<<1],mi[id<<1|1]);
}
void build(int id,int l,int r)
{
	len[id]=r-l+1;
	if(l==r)
	{
		scanf("%d",&a[l]);
		mx[id]=a[l];
		mi[id]=a[l];
		return ;
	}
	int mid=l+r>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid+1,r);
	pushup(id);
}
void pushdown(int id,int l,int r)
{
	if(lazy[id])
	{
		if(l!=r)
		{
			lazy[id<<1]+=lazy[id];
			lazy[id<<1|1]+=lazy[id];
			mx[id<<1]+=lazy[id];
			mx[id<<1|1]+=lazy[id];
			mi[id<<1]+=lazy[id];
			mi[id<<1|1]+=lazy[id];
		}
		lazy[id]=0;
	}
}
void up(int id,int l,int r,int ql,int qr,int val)
{
	pushdown(id,l,r);
	if(ql<=l&&r<=qr)
	{
		mx[id]+=val;
		mi[id]+=val;
		lazy[id]+=val;
		return ;
	}
	int mid=(l+r)>>1;
	if(ql<=mid) up(id<<1,l,mid,ql,qr,val);
	if(qr>mid) up(id<<1|1,mid+1,r,ql,qr,val);
	pushup(id);
}
qu_mx(int id,int l,int r)
{
	pushdown(id,l,r);
	return mx[id];
}
qu_mi(int id,int l,int r)
{
	pushdown(id,l,r);
	return mi[id];
}
int main()
{
	int n,m;
	cin>>n>>m;
	build(1,1,n);
	for(int i=1;i<=m;i++) 
	{
		scanf("%d%d",&l[i],&r[i]);
		sub[l[i]].push_back(i);
		add[r[i]].push_back(i);
	}
	int d=-1,p;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<add[i-1].size();j++)
		{
			int id=add[i-1][j];
			up(1,1,n,l[id],r[id],1);
		}
		for(int j=0;j<sub[i].size();j++)
		{
			int id=sub[i][j];
			up(1,1,n,l[id],r[id],-1);
		}
		int det=qu_mx(1,1,n)-qu_mi(1,1,n);
		if(det>d)
		{
			d=det;
			p=i;
		}
	}
	int j=0;
	for(int i=1;i<=m;i++)
	{
		if(l[i]<=p&&p<=r[i])
		ans[++j]=i;
	}
	printf("%d\n",d);
	printf("%d\n",j);
	for(int i=1;i<=j;i++) printf("%d ",ans[i]);
	puts("");
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/88915986