結果

問題 No.2223 Merged Med
ユーザー 👑 hitonanodehitonanode
提出日時 2023-03-11 14:22:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 527 ms / 7,000 ms
コード長 4,416 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 185,436 KB
実行使用メモリ 20,872 KB
最終ジャッジ日時 2023-10-18 09:52:06
合計ジャッジ時間 11,229 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 152 ms
8,768 KB
testcase_14 AC 73 ms
6,088 KB
testcase_15 AC 224 ms
15,456 KB
testcase_16 AC 426 ms
16,492 KB
testcase_17 AC 116 ms
10,056 KB
testcase_18 AC 66 ms
5,992 KB
testcase_19 AC 135 ms
8,516 KB
testcase_20 AC 486 ms
20,496 KB
testcase_21 AC 484 ms
20,492 KB
testcase_22 AC 517 ms
20,500 KB
testcase_23 AC 500 ms
20,496 KB
testcase_24 AC 515 ms
20,496 KB
testcase_25 AC 500 ms
20,496 KB
testcase_26 AC 508 ms
20,496 KB
testcase_27 AC 460 ms
20,500 KB
testcase_28 AC 527 ms
20,496 KB
testcase_29 AC 499 ms
20,496 KB
testcase_30 AC 145 ms
20,872 KB
testcase_31 AC 133 ms
19,700 KB
testcase_32 AC 11 ms
4,348 KB
testcase_33 AC 365 ms
20,232 KB
testcase_34 AC 250 ms
20,276 KB
testcase_35 AC 237 ms
19,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
const std::vector<std::pair<int, int>> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); }
template <class T1, class T2> std::pair<T1, T2> operator+(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first + r.first, l.second + r.second); }
template <class T1, class T2> std::pair<T1, T2> operator-(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first - r.first, l.second - r.second); }
template <class T> std::vector<T> sort_unique(std::vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <class T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <class T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; }

#include <atcoder/segtree>

struct S {
    int sum = 0;
    int min_cumsum = 0;
    int max_cumsum = 0;
    int diff_max = 0;
    int cnt_minus = 0;
    static S plus() { return S{1, 0, 1, 1, 0}; }
    static S minus() { return S{-1, -1, 0, 0, 1}; }
};

S e() { return S(); }
S op(S l, S r) {
    return S{
        l.sum + r.sum,
        std::min(l.min_cumsum, l.sum + r.min_cumsum),
        std::max(l.max_cumsum, l.sum + r.max_cumsum),
        std::max({l.diff_max, r.diff_max, l.sum + r.max_cumsum - l.min_cumsum}),
        l.cnt_minus + r.cnt_minus,
    };
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    for (auto &x : A) cin >> x;

    vector<pint> query(Q);
    for (auto &[l, r] : query) cin >> l >> r, --l;

    vector<int> bin_ng(Q, 0), bin_ok(Q, N);

    vector<vector<int>> a2is(N + 1), c2qs(N + 1);
    REP(i, N) a2is.at(A.at(i)).push_back(i);

    vector<S> init(N, S::plus());
    atcoder::segtree<S, op, e> tree;
    while (true) {
        tree = atcoder::segtree<S, op, e>(init);

        bool flg = false;
        REP(q, Q) {
            if (bin_ng.at(q) + 1 < bin_ok.at(q)) {
                c2qs.at((bin_ng.at(q) + bin_ok.at(q)) / 2).emplace_back(q);
                flg = true;
            }
        }
        if (!flg) break;

        int added = 0;

        REP(c, c2qs.size()) {
            for (const int q : c2qs.at(c)) {
                while (added < c) {
                    ++added;
                    for (int i : a2is.at(added)) tree.set(i, S::minus());
                }
                const auto [l, r] = query.at(q);
                const auto p = tree.prod(l, r);

                bool success = false;
                int reduce_plus = 0;
                if (p.diff_max > 1) reduce_plus = p.diff_max - 1;
                int num_plus = r - l - p.cnt_minus - reduce_plus;
                if (p.cnt_minus > num_plus) success = true;

                (success ? bin_ok : bin_ng).at(q) = c;
            }
            c2qs.at(c).clear();
        }
    }
    for (auto x : bin_ok) cout << x << '\n';
}
0