#define rep(i,n) for(int i=0; i<(int)(n); i++) #include #ifdef _MSC_VER #include #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 #include #include namespace atcoder { template struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} lazy_segtree(const std::vector& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector(2 * size, e()); lz = std::vector(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 int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template 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 int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template 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 d; std::vector 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 #include #include namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::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.r < Rmax) return false; if(x.q > Qmax) return true; else return false; } int main(){ int N; cin >> N; vector A(N); vector B(N - 1); rep(i,N) cin >> A[i]; LL Amax = 0; rep(i,N) Amax = max(Amax, A[i]); int Q; cin >> Q; if(N == 1){ rep(i,Q){ int t; cin >> t; if(t == 1){ LL j, x; cin >> j >> x; j--; A[j] = x; } else cout << A[0] - (N - 2) << endl; } return 0; } LL Qsum = 0LL; rep(i,N) Qsum += (A[i] + 1) / (N - 1); vector 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 lseg(B); rep(i,Q){ int t; cin >> t; if(t == 1){ LL j, x; cin >> j >> x; j--; LL q_dif = 0; q_dif += (x + 1) / (N - 1) - (A[j] + 1) / (N - 1); LL r1 = (A[j] + 1) % (N - 1), r2 = (x + 1) % (N - 1); A[j] = x; lseg.apply(0, N - 1, q_dif); lseg.apply(r1, N - 1, 1); lseg.apply(r2, N - 1, -1); Amax = max(Amax, x); } else{ S mini = lseg.all_prod(); LL ans = mini.q * (N - 1) + mini.r; LL M = Amax - (N - 2); Qmax = M / (N - 1), Rmax = M % (N - 1); if(ans < M){ LL res_r = INF, res_l = INF; LL right_small = lseg.min_left(Rmax); if(right_small < Rmax){ res_r = (N - 1) * (Qmax + 1) + right_small; } LL right_large = lseg.min_left(N - 1); if(right_large != N - 1){ res_l = (N - 1) * Qmax + right_large; } ans = min(res_r, res_l); } cout << ans << endl; } } return 0; }