結果

問題 No.2735 Demarcation
ユーザー warner1129warner1129
提出日時 2024-04-20 15:27:23
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 1,500 ms
コード長 3,084 bytes
コンパイル時間 3,831 ms
コンパイル使用メモリ 287,320 KB
実行使用メモリ 15,648 KB
最終ジャッジ日時 2024-04-20 15:27:32
合計ジャッジ時間 8,483 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 68 ms
15,568 KB
testcase_05 AC 28 ms
6,940 KB
testcase_06 AC 103 ms
14,332 KB
testcase_07 AC 164 ms
14,980 KB
testcase_08 AC 174 ms
14,932 KB
testcase_09 AC 63 ms
7,484 KB
testcase_10 AC 80 ms
9,052 KB
testcase_11 AC 26 ms
6,940 KB
testcase_12 AC 32 ms
6,940 KB
testcase_13 AC 90 ms
6,940 KB
testcase_14 AC 157 ms
11,724 KB
testcase_15 AC 71 ms
6,944 KB
testcase_16 AC 111 ms
13,484 KB
testcase_17 AC 177 ms
8,840 KB
testcase_18 AC 86 ms
6,940 KB
testcase_19 AC 159 ms
14,984 KB
testcase_20 AC 185 ms
13,680 KB
testcase_21 AC 205 ms
11,708 KB
testcase_22 AC 116 ms
15,280 KB
testcase_23 AC 37 ms
6,940 KB
testcase_24 AC 60 ms
9,012 KB
testcase_25 AC 90 ms
13,300 KB
testcase_26 AC 86 ms
12,760 KB
testcase_27 AC 76 ms
11,940 KB
testcase_28 AC 120 ms
15,648 KB
testcase_29 AC 116 ms
15,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <class F, class S>
ostream &operator<<(ostream &s, const pair<F, S> &v) {
    s << "(" << v.first << ", " << v.second << ")";
    return s;
}
template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
istream &operator>>(istream &s, T &&v) {
    for (auto &&x : v) s >> x;
    return s;
}
template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
ostream &operator<<(ostream &s, T &&v) {
    for (auto &&x : v) s << x << ' ';
    return s;
}

#ifdef LOCAL
template <class... T> void dbg(T... x) {
    char e{};
    ((cerr << e << x, e = ' '), ...);
}
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define debug(...) ((void)0)
#endif

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

template <class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
template <class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template <class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;

constexpr i64 mod = 998244353;

void solve() {
    int n;
    cin >> n;

    vector<int> A(n);
    cin >> A;

    A.push_back(-1);

    vector<int> dif(n + 1);
    for (int i = n - 1; i >= 0; i--) {
        dif[i] = dif[i + 1] + (A[i] != A[i + 1]);
    }

    vector<int> cnt(n + 1);
    auto DP = [&](int l, int r, int k) -> i64 {
        if (k == 0) return 0;
        vector<i64> dp(r - l + 1);
        dp[0] = 1;

        for (int i = l; i < r; i++) cnt[A[i]] = 0;
        int d = 0;
        
        for (int i = l, t = l - 1; i < r; i++) {
            d += (cnt[A[i]]++ == 0);
            while (d > k) {
                t++;
                d -= (--cnt[A[t]] == 0);
            }

            for (int j = t; j < i; j++) {
                dp[i - l + 1] += dp[j - l + 1];
                chmin(dp[i - l + 1], inf<i64>);
            }
        }

        return dp.back();
    };

    int q;
    cin >> q;
    while (q--) {
        int l, r;
        i64 s;
        cin >> l >> r >> s;
        l--;

        if (r - l > 90) {
            i64 p = r - l - 1 - (dif[l] - dif[r - 1]);
            if (p > 60 or (1ll << p) > s) {
                cout << 0 << '\n';
            } else {
                cout << 1 << '\n';
            }
        } else {
            int ans = *ranges::partition_point(views::iota(1, r - l + 1), 
                [&](int t) {
                    return DP(l, r, t) <= s;
                });

            if (ans == r - l + 1) {
                cout << -1 << '\n';
            } else {
                cout << ans - 1 << '\n';
            }
        }
    }
    
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
    
    return 0;
}




0