题意,给定一个数组,要求在每个区间减一的操作中选出使得数组中最小与最大值差距最大的操作组合,并求出最大差值
思路:对于最大最小值,我们要求的是两者之间的最大差值,因此有一个经典的特性,对于最大最小值,我们同时将它们减1是没有影响的,除非这样使得最大值改变位置,因此我们可以得出,对于每一个最小值来说,最大最小值之间得差值总是在最小值被减的次数最多时(充分但不必要)得出的,因为如果最大值最后在操作组合的范围外,那无所谓,如果在范围内,由于最大最小同时减是无影响的,因此也无所谓。
那就直接来吧,线段树区间维护最大最小,枚举最小值位置
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>
//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 1000000007;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int a[N];
PII op[310];
struct Node {
int l, r, mx, mn;
int add;
}tr[N * 4];
vector<int> st[N], ed[N];
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline void init() {}
void push_down(int u)
{
if (tr[u].add) {
tr[u << 1].add += tr[u].add;
tr[u << 1].mx += tr[u].add;
tr[u << 1].mn += tr[u].add;
tr[u << 1 | 1].add += tr[u].add;
tr[u << 1 | 1].mx += tr[u].add;
tr[u << 1 | 1].mn += tr[u].add;
tr[u].add = 0;
}
}
void push_up(int u)
{
tr[u].mx = max(tr[u << 1 | 1].mx, tr[u << 1].mx);
tr[u].mn = min(tr[u << 1 | 1].mn, tr[u << 1].mn);
}
void build(int l, int r, int u = 1)
{
tr[u] = { l,r };
if (l == r) {
tr[u].mx = tr[u].mn = a[l]; return;
}
int mid = l + r >> 1;
build(l, mid, u << 1), build(mid + 1, r, u << 1 | 1);
push_up(u);
}
void modify(int l, int r, int c, int u = 1)
{
if (l <= tr[u].l && r >= tr[u].r) {
tr[u].add += c;
tr[u].mx += c;
tr[u].mn += c;
}
else {
push_down(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(l, r, c, u << 1);
if (r > mid)modify(l, r, c, u << 1 | 1);
push_up(u);
}
}
void slove()
{
cin >> n >> m;
pre(i, 1, n) cin >> a[i];
build(1, n);
int l, r;
pre(i, 1, m) {
cin >> op[i].x >> op[i].y;
st[op[i].x].pb(i);
ed[op[i].y].pb(i);
}
int mx = 0;
set<int> ans, tmp;
pre(i, 1, n)
{
for (int id : ed[i - 1]) modify(op[id].x, op[id].y, 1), tmp.erase(id);
for (int id : st[i]) modify(op[id].x, op[id].y, -1),tmp.insert(id);
if (tr[1].mx - tr[1].mn > mx) {
mx = tr[1].mx - tr[1].mn;
ans = tmp;
}
}
cout << mx << endl;
cout << ans.size() << endl;
for (int i : ans) cout << i << ' ';
return;
}
signed main()
{
//IOS;
int _;
//si(_);
_ = 1;
init();
while (_--)
{
slove();
}
return 0;
}