Codeforces Contest 1108 problem 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
inputCopy
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
outputCopy
6
2
4 1
inputCopy
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
outputCopy
7
2
3 2
inputCopy
1 0
1000000
outputCopy
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个操作,每次操作都是把这个区间的数-1,让你选择一些操作使得最大值-最小值最大。

题解;

枚举每一个点是最小值的情况,所以包括他的操作一定要加,其他不加,在找到最大值和最小值。本来应该是nmlogn的,但是由于区间是连续的,所以不需要每一次都变回原来的数组,只需要600次update就好了,所以是n*m的时间复杂度(一开始每次都变回去了,一直T在第93个)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],l[N],r[N];
int maxn[N*4],minn[N*4],flag[N*4];
void pushup(int root)
{
    maxn[root]=max(maxn[root<<1],maxn[root<<1|1]);
    minn[root]=min(minn[root<<1],minn[root<<1|1]);
}
void pushdown(int root)
{
    if(!flag[root])
        return ;
    maxn[root<<1]+=flag[root];
    maxn[root<<1|1]+=flag[root];
    minn[root<<1]+=flag[root];
    minn[root<<1|1]+=flag[root];
    flag[root<<1]+=flag[root];
    flag[root<<1|1]+=flag[root];
    flag[root]=0;
}
void build(int l,int r,int root)
{
    if(l==r)
    {
        maxn[root]=minn[root]=a[l];
        return ;
    }
    int mid=l+r>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
    pushup(root);
}
void update(int l,int r,int root,int ql,int qr,int val)
{
    if(l>=ql&&r<=qr)
    {
        maxn[root]+=val;
        minn[root]+=val;
        flag[root]+=val;
        return ;
    }
    pushdown(root);
    int mid=l+r>>1;
    if(mid>=ql)
        update(l,mid,root<<1,ql,qr,val);
    if(mid<qr)
        update(mid+1,r,root<<1|1,ql,qr,val);
    pushup(root);
}
vector<int>ans;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)
        scanf("%d%d",&l[i],&r[i]);
    int mx=-1e7;
    build(1,n,1);
    int vis[305]={0};
    for(int i=1;i<=n;i++)
    {

        for(int j=1;j<=m;j++)
        {
            if(l[j]<=i&&r[j]>=i)
            {
                if(!vis[j])
                    update(1,n,1,l[j],r[j],-1),vis[j]=1;
            }
            else if(vis[j])
                update(1,n,1,l[j],r[j],1),vis[j]=0;
        }
        if(mx<maxn[1]-minn[1])
        {
            ans.clear();
            for(int j=1;j<=m;j++)
                if(vis[j])
                    ans.push_back(j);
            mx=maxn[1]-minn[1];
        }
    }
    printf("%d\n",mx);
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        printf("%d ",ans[i]);
    printf("\n");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值