結果

問題 No.1394 Changing Problems
ユーザー 👑 platinumplatinum
提出日時 2020-09-30 22:49:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 821 ms / 3,000 ms
コード長 11,355 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 87,636 KB
実行使用メモリ 30,652 KB
最終ジャッジ日時 2023-09-20 18:44:34
合計ジャッジ時間 20,774 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 646 ms
30,616 KB
testcase_06 AC 799 ms
30,472 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 18 ms
4,380 KB
testcase_09 AC 18 ms
4,380 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 18 ms
4,380 KB
testcase_12 AC 18 ms
4,380 KB
testcase_13 AC 792 ms
30,572 KB
testcase_14 AC 798 ms
30,500 KB
testcase_15 AC 791 ms
30,604 KB
testcase_16 AC 814 ms
30,608 KB
testcase_17 AC 813 ms
30,476 KB
testcase_18 AC 795 ms
30,432 KB
testcase_19 AC 821 ms
30,628 KB
testcase_20 AC 818 ms
30,540 KB
testcase_21 AC 809 ms
30,424 KB
testcase_22 AC 818 ms
30,484 KB
testcase_23 AC 809 ms
30,500 KB
testcase_24 AC 814 ms
30,536 KB
testcase_25 AC 805 ms
30,348 KB
testcase_26 AC 759 ms
30,552 KB
testcase_27 AC 779 ms
30,652 KB
testcase_28 AC 763 ms
30,484 KB
testcase_29 AC 689 ms
30,504 KB
testcase_30 AC 680 ms
30,568 KB
testcase_31 AC 686 ms
30,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0; i<(int)(n); i++)
#include <algorithm>
#include <set>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder



#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

using namespace std;
using namespace atcoder;
using LL = long long;
const LL INF = 1e18;
LL Qmax, Rmax;

struct S{
  LL q, r;
  bool operator< (const S &x) const{
    if(q != x.q) return q < x.q;
    else return r < x.r;
  }
};

S op(S a, S b){
    return min(a, b);
}

S e(){
  	S x = {INF, INF};
    return x;
}

S mapping(LL f, S x){
  	S y = {x.q + f, x.r};
    return y;
}

LL composition(LL f, LL g){
    return f + g;
}

LL id(){
    return 0LL;
}

bool g_small(S x){
	if(x.q > Qmax + 1) return true;
	else return false;
}

bool g_large(S x){
	if(x.q > Qmax) return true;
	else return false;
}

int main(){
    int N;
    cin >> N;
    vector<LL> A(N);
  	vector<S> B(N - 1);
    rep(i,N) cin >> A[i];
  	multiset<LL,greater<LL>> st;
  	rep(i,N) st.insert(A[i]);
  	LL Amax = *st.begin();
    int Q;
    cin >> Q;
  	if(N == 1){
      rep(t,Q){
        LL i, x;
        cin >> i >> x;
        i--;
        A[i] = x;
        cout << A[0] - (N - 2) << endl;
      }
      return 0;
    }
    LL Qsum = 0LL;
    rep(i,N) Qsum += (A[i] + 1) / (N - 1);
    vector<LL> R(N - 1), Rsum(N);
    rep(i,N){
        LL r = (A[i] + 1) % (N - 1);
        R[r]++;
    }
  	rep(i,N - 1) Rsum[i + 1] = Rsum[i] + R[i];
    rep(i,N - 1){
      	S b = {Qsum + i - Rsum[i + 1], i};
        B[i] = b;
    }
    lazy_segtree<S, op, e, LL, mapping, composition, id> lseg(B);
    rep(t,Q){
        LL i, x;
        cin >> i >> x;
        i--;
        LL q_dif = 0;
        q_dif += (x + 1) / (N - 1) - (A[i] + 1) / (N - 1);
        LL r1 = (A[i] + 1) % (N - 1), r2 = (x + 1) % (N - 1);
        lseg.apply(0, N - 1, q_dif);
        lseg.apply(r1, N - 1, 1);
        lseg.apply(r2, N - 1, -1);
        auto itr = st.find(A[i]);
        st.erase(itr);
        st.insert(x);
        A[i] = x;
        Amax = *st.begin();
        S mini = lseg.all_prod();
        LL ans = mini.q * (N - 1) + mini.r;
        LL M = Amax - (N - 2);
        if(M <= 0){
           cout << 0 << endl;
           continue;
        }
        Qmax = M / (N - 1), Rmax = M % (N - 1);
        if(ans < M){
           LL res_r = INF, res_l = INF;
           LL right_small = lseg.max_right<g_small>(0);
           if(right_small < Rmax){
           		res_r = (N - 1) * (Qmax + 1) + right_small;
           }
           LL right_large = lseg.max_right<g_large>(Rmax);
           if(right_large != N - 1){
            	res_l = (N - 1) * Qmax + right_large;
           }
           ans = min(res_r, res_l);
        }
        cout << ans << endl;
    }

    return 0;
}
0