Codeforces div.3 E2. Array and Segments (Hard versio

题意,给定一个数组,要求在每个区间减一的操作中选出使得数组中最小与最大值差距最大的操作组合,并求出最大差值

思路:对于最大最小值,我们要求的是两者之间的最大差值,因此有一个经典的特性,对于最大最小值,我们同时将它们减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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值