結果

問題 No.1394 Changing Problems
ユーザー platinumplatinum
提出日時 2020-09-30 22:49:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 577 ms / 3,000 ms
コード長 11,355 bytes
コンパイル時間 905 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 30,832 KB
最終ジャッジ日時 2024-07-06 13:37:35
合計ジャッジ時間 15,145 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 504 ms
30,740 KB
testcase_06 AC 546 ms
30,652 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 15 ms
5,376 KB
testcase_09 AC 15 ms
5,376 KB
testcase_10 AC 15 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 15 ms
5,376 KB
testcase_13 AC 553 ms
30,624 KB
testcase_14 AC 555 ms
30,544 KB
testcase_15 AC 548 ms
30,640 KB
testcase_16 AC 556 ms
30,644 KB
testcase_17 AC 571 ms
30,476 KB
testcase_18 AC 571 ms
30,720 KB
testcase_19 AC 558 ms
30,720 KB
testcase_20 AC 562 ms
30,832 KB
testcase_21 AC 566 ms
30,676 KB
testcase_22 AC 561 ms
30,740 KB
testcase_23 AC 566 ms
30,720 KB
testcase_24 AC 559 ms
30,592 KB
testcase_25 AC 577 ms
30,780 KB
testcase_26 AC 533 ms
30,720 KB
testcase_27 AC 531 ms
30,660 KB
testcase_28 AC 549 ms
30,720 KB
testcase_29 AC 536 ms
30,600 KB
testcase_30 AC 533 ms
30,592 KB
testcase_31 AC 525 ms
30,596 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