結果
| 問題 | No.2223 Merged Med |
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2023-03-11 14:22:20 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 471 ms / 7,000 ms |
| コード長 | 4,416 bytes |
| 記録 | |
| コンパイル時間 | 2,210 ms |
| コンパイル使用メモリ | 186,188 KB |
| 実行使用メモリ | 20,960 KB |
| 最終ジャッジ日時 | 2024-09-18 06:21:43 |
| 合計ジャッジ時間 | 9,135 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#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';
}
hitonanode