codeforces535_E2. Array and Segments (Hard version)

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

You are given an array a consisting of n integers. The value of the i-th element of the array is ai.

You are also given a set of m segments. The j-th segment is [lj;rj], where 1≤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] and the given segments are [1;3] and [2;4] then you can choose both of them and the array will become 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 a and obtain the array b then the value maxi=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 n and m (1≤n≤105,0≤m≤300) — the length of the array a and the number of segments, respectively.

The second line of the input contains n integers a1,a2,…,an (−106≤ai≤106), where ai is the value of the i-th element of the array a.

The next m lines are contain two integers each. The j-th of them contains two integers lj and rj (1≤lj≤rj≤n), where lj and rj are the ends of the j-th segment.

Output

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

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

In the third line print q distinct integers c1,c2,…,cq in any order (1≤ck≤m) — indices of segments you apply to the array a in such a way that the value maxi=1nbi−mini=1nbi of the obtained array b is maximum possible.
If there are multiple answers, you can print any.

Examples
Input

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

Output

6
2
4 1 

Input

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

Output

7
2
3 2 

Input

1 0
1000000

Output

0
0

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

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

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


首先给出一个长度为 n 的数组,以及 m 个区间。对于给出的这 m 个区间,可以对区间中每个元素都 -1 。求这个数组所有可能最大值与最小值的差的最大值(极差的最大值)。(也可以不同区间,即原始极差作为答案)
若综合考虑,情况较为复杂,因此可以退而求其次,枚举每一个元素作为最大值时的答案(所有可能的极差的取值),进而求得答案。

那么现在考虑求当最大值下标为 i 时的极差。
易知任何包含位置 i 的区间都不会使极差增大,因为对这个区间内的元素执行自减操作后,无论此时的最小值在这个区间内还是在这个区间外,都不会使极差增大。所以,又可以将所给的区间划分为两类:在位置 i 左侧 与 在位置 i 右侧。

现在考虑在左侧的情况:
由于存在关系:
在位置 i 左侧的所有区间 = 在位置 i-1 左侧的所有区间 + 以 i 为右端点的所有区间
而对于位置 i 左侧元素的最终值为 初始值 + 自减操作数。
因此,可以从最左端开始依次积累到所求的位置 i ,即 先更新 i 左侧元素中最小值,后加入所有以 i 为右端点的区间。
Ps:注意当没有以 i 为右端点的区间时,所需要执行的操作


#include <stdio.h>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define re return
#define Pair pair<int,int>
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

int n,m;
int store[100024];
vector<int> ans,add;
vector<Pair> segment;
vector<vector<int> > lf,rg;
int main(){
    ios::sync_with_stdio(false);

    cin>>n>>m;
    rep(i,0,n)
        cin>>store[i];
    segment.resize(m);
    lf.resize(n+1),rg.resize(n+1);
    rep(i,0,m){
        cin>>segment[i].first>>segment[i].second;
        segment[i].first--,segment[i].second--;
        lf[segment[i].second].Push(segment[i].first);
        rg[segment[i].first].Push(segment[i].second);
    }

    ans.resize(n,-1);
    add.resize(n,0);
    int least=store[0];
    //from left
    rep(i,0,n){
        //update
        ans[i]=max(ans[i],store[i]-least);
        //add new segment
        for(register int j=0;j<lf[i].size();j++){
            for(register int k=lf[i][j];k<=i;k++){
                add[k]--;
                least=min(least,store[k]+add[k]);
            }
        }
        //there are not add new segment
        least=min(least,store[i]+add[i]);

    }
    //from right
    add=vector<int>(n,0);
    least=store[n-1];
    drep(i,n-1,0){
        //update
        ans[i]=max(ans[i],store[i]-least);
        //add new segment
        for(register int j=0;j<rg[i].size();j++){
            for(register int k=i;k<=rg[i][j];k++){
                add[k]--;
                least=min(least,store[k]+add[k]);
            }
        }
        //there are not add new segmen
        least=min(least,store[i]+add[i]);
    }
    //find ans(max value) position
    int ansPos=0;
    for(register int i=0;i<n;i++){
        if(ans[i]>ans[ansPos])
            ansPos=i;
    }
    //get segment used
    vector<int> used;
    for(register int i=0;i<m;i++){
        if(segment[i].first>ansPos || segment[i].second<ansPos)
            used.Push(i+1);
    }
    cout<<ans[ansPos]<<endl;
    cout<<used.size()<<endl;
    for(register int i=0;i<used.size();i++)
        cout<<used[i]<<" ";
    cout<<endl;
    re 0;
}

猜你喜欢

转载自blog.csdn.net/white_156/article/details/86764124