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

### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值