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

 

全部评论

相关推荐

原来已经一年了,因为没有加任何实验室没有学长学姐带,再一次偶然的机会下刷到我们学校的牛肉哥,和他聊天之后发现他也没加实验室能进大厂,我就燃起了希望,去年大概&nbsp;4&nbsp;月份找好路线&nbsp;零基础&nbsp;开始学&nbsp;5&nbsp;月背八股和开始刷算法很难受&nbsp;7-8&nbsp;月焦虑躯体化害怕找不到实习&nbsp;9&nbsp;月找到一家像样的小厂去实习了&nbsp;4&nbsp;个月大三上期末考试结束之后&nbsp;1&nbsp;月份回来边实习边准备工作压力很大&nbsp;当时只有字节、百度、商汤的面试,字节三面挂了,百度&nbsp;oc,商汤&nbsp;二面挂(差评&nbsp;无效面试),之后来深圳百度实习之后还是觉得不甘心一直没把算法和八股扔下一直在准备,百度实习的时候&nbsp;mt&nbsp;交给我一个特别重要的工作数据库迁移(特别感谢&nbsp;mt&nbsp;,这个需求学到了很多东西处理了一堆线上问题),本来看着暑期他们面试都很困难,然后听说百度要涨实习薪资(然而&nbsp;5&nbsp;月并没有涨),就想着留在百度吧也懒得面试了,4&nbsp;月&nbsp;20&nbsp;多的时候字节&nbsp;hr&nbsp;打电话约面问我要不要尝试一下询问了&nbsp;1&nbsp;月份三面为啥会挂有没有学习&nbsp;ai&nbsp;知识(因为字节这边后端岗位偏&nbsp;ai),我来到百度之后全面拥抱&nbsp;AI&nbsp;也认识了我的好兄弟&nbsp;X&nbsp;哥,他在百度&nbsp;XX&nbsp;部门&nbsp;Agent&nbsp;实习,他属于是我&nbsp;Agent&nbsp;的启蒙老师,来百度之后一直在了解&nbsp;AI&nbsp;这一块,我就接受了字节的面试,一面的时候&nbsp;20&nbsp;分钟实习拷打然后突然说&nbsp;30&nbsp;分钟代码考核我心就凉了以为是&nbsp;kpi,算法题是手撕高并发安全下的令牌桶限流器,我写了整整&nbsp;80&nbsp;多行代码最后也写出来了,但是从来没看到过出这种题能&nbsp;oc&nbsp;的我也就不管了,后边面试也是很顺利但是流程有点长可能一直在横向吧总结结果是好的!!!感谢这一年努力的自己和遇到的各位互联网大佬分享的知识!!!ps&nbsp;图二纯感慨&nbsp;(觉得🍬请不要喷我)欢迎大家一起交流学习呀!!!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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