Codeforces 1108 E2 Array and Segments (Hard version)

http://codeforces.com/problemset/problem/1108/E2

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 .

 

 

 题意:有一长度为n的序列和m个区间,要选择若干个区间,对于每个区间,其所有元素减小1,最后要求序列最大值与最小值之差最大。

 

设最终答案的最小值位置在a,最大值位置在b,对于任意一个区间,有4中可能:含a不含b,含b不含a,不含a不含b,含a含b。

含a不含b对答案有贡献,含b不含a对答案有副作用,其余两种无所谓。

对于简单版,O(n^2)足矣,可以枚举最大与最小值的位置,也可以光枚举一个,比如光枚举最大值位置,然后把不覆盖这个位置的所有区间都减1,最后判断。

对于困难版,n很大,要用线段树+类似差分的思想。

记录下以每个位置为起始点的区间集合和以每个位置为结束点下一个位置的区间集合。

初始将所有的区间都减1,然后枚举最大值位置i,将以i开始的区间全部+1,将以i-1结束的区间全部-1。这个思想非常类似差分。

然后求该点的值-序列最小值,复杂度O(nlogn)。

求序列最大值-序列最小值的话,复杂度就是官方题解的O(n+mlogn)。

因为区间最大值就存在maxv[o]中,而该点的值还需要递归logn层。

#include<bits/stdc++.h>
using namespace std;
#define maxn (100000+100)

int n,m,a[maxn],ans,ansi;
vector<pair<int,int> > segment;
vector<pair<int,int> > s[maxn],t[maxn];

int minv[maxn*4],addv[maxn*4];
int build(int o,int l,int r)
{
	int m=(l+r)/2;
	if(l==r)return minv[o]=addv[o]=a[l];
	return minv[o]=min(build(o*2,l,m),build(o*2+1,m+1,r));
}
void maintain(int o,int l,int r)
{
	int lc=o*2,rc=o*2+1;
	minv[o]=0;
	if(r>l)minv[o]=min(minv[lc],minv[rc]);
	minv[o]+=addv[o];
}
int y,y2,v;
void update(int o,int l,int r)
{
	int lc=o*2,rc=o*2+1;
	if(y<=l&&r<=y2)addv[o]+=v;
	else
	{
		int m=(l+r)/2;
		if(y<=m)update(lc,l,m);
		if(y2>m)update(rc,m+1,r);
	}
	maintain(o,l,r);
}
int _min;
void query(int o,int l,int r,int add)
{
	if(y<=l&&r<=y2)_min=min(_min,minv[o]+add);
	else
	{
		int m=(l+r)/2;
		if(y<=m)query(o*2,l,m,add+addv[o]);
		if(y2>m)query(o*2+1,m+1,r,add+addv[o]);
	}
}

int main()
{
//	freopen("input.in","r",stdin);
	int a1,b1;
	cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a1,&b1);
		y=a1;y2=b1;v=-1;update(1,1,n);
		segment.push_back(make_pair(a1,b1));
		s[a1].push_back(make_pair(a1,b1));
		t[b1+1].push_back(make_pair(a1,b1));
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<s[i].size();j++)y=s[i][j].first,y2=s[i][j].second,v=1,update(1,1,n);
		for(int j=0;j<t[i].size();j++)y=t[i][j].first,y2=t[i][j].second,v=-1,update(1,1,n);
		y=i;y2=i;_min=(1<<30);
		query(1,1,n,0);
		int maxx=_min;
		y=1;y2=n;_min=(1<<30);
		query(1,1,n,0);
		int minn=_min;
		if(maxx-minn>ans)
		{
			ans=maxx-minn;
			ansi=i;
		}
	}
	cout<<ans<<endl;
	int num=0;
	for(int i=0;i<segment.size();i++)if(segment[i].first>ansi||segment[i].second<ansi)num++;
	cout<<num<<endl;
	for(int i=0;i<segment.size();i++)if(segment[i].first>ansi||segment[i].second<ansi)cout<<i+1<<" ";
	cout<<endl;
	return 0;
}

 

全部评论

相关推荐

投了十几个无人回应
花环鞣: 佬,我隔壁的
点赞 评论 收藏
分享
04-02 10:09
门头沟学院 Java
用微笑面对困难:这里面问题还是很多的,我也不清楚为啥大家会感觉没啥问题。首先就是全栈开发实习9个月的内容都没有java实习生的内容多,1整个技术栈没看出太核心和难点的内容,感觉好像被拉过去打杂了,而且全栈基本上很容易被毙。里面能问的bug是在太多了比如L:继承 BaseMapper 可直接使用内置方法’。请问你的 BaseMapper 是如何扫描实体类注解如果瞬时产生 100 个上传任务,MySQL 的索引设计是否会有瓶颈?你做过分库分表或者索引优化吗?全栈的内容可以针对动态难点去搞,技能特长写在下面吧,你写了这么多技能,项目和实习体现了多少?你可以在项目里多做文章然后把这个放下去,从大致来看实习不算太水,有含金量你也要写上内容针对哨兵里面的节点变化能问出一万个问题,这个很容易就爆了。
提前批简历挂麻了怎么办
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务