結果

問題 No.1394 Changing Problems
ユーザー leaf_1415leaf_1415
提出日時 2021-02-13 04:57:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,043 ms / 3,000 ms
コード長 4,112 bytes
コンパイル時間 3,058 ms
コンパイル使用メモリ 108,252 KB
実行使用メモリ 22,304 KB
最終ジャッジ日時 2023-09-27 12:02:50
合計ジャッジ時間 27,535 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,300 KB
testcase_01 AC 7 ms
11,188 KB
testcase_02 AC 7 ms
11,188 KB
testcase_03 AC 7 ms
11,424 KB
testcase_04 AC 7 ms
11,240 KB
testcase_05 AC 827 ms
22,124 KB
testcase_06 AC 975 ms
22,252 KB
testcase_07 AC 6 ms
11,360 KB
testcase_08 AC 29 ms
11,680 KB
testcase_09 AC 31 ms
11,296 KB
testcase_10 AC 29 ms
11,188 KB
testcase_11 AC 30 ms
11,240 KB
testcase_12 AC 27 ms
11,352 KB
testcase_13 AC 1,018 ms
22,164 KB
testcase_14 AC 1,043 ms
22,244 KB
testcase_15 AC 1,028 ms
22,240 KB
testcase_16 AC 1,034 ms
22,280 KB
testcase_17 AC 1,028 ms
22,236 KB
testcase_18 AC 1,005 ms
22,236 KB
testcase_19 AC 1,020 ms
22,260 KB
testcase_20 AC 1,026 ms
22,200 KB
testcase_21 AC 1,004 ms
22,104 KB
testcase_22 AC 989 ms
22,304 KB
testcase_23 AC 1,015 ms
22,108 KB
testcase_24 AC 992 ms
22,180 KB
testcase_25 AC 1,011 ms
22,132 KB
testcase_26 AC 902 ms
22,204 KB
testcase_27 AC 929 ms
22,108 KB
testcase_28 AC 916 ms
22,212 KB
testcase_29 AC 841 ms
22,208 KB
testcase_30 AC 846 ms
22,124 KB
testcase_31 AC 833 ms
22,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define outl(x) cout << x << endl
#define SP << " " << 
#define inf 1e18
#define mod 998244353

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;


// range-add, range-min query
 
struct LazySegTree{
	int size;
	vector<llint> seg, delay;
	
	LazySegTree(){}
	LazySegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
		delay.resize(1<<(size+1));
	}
	
	llint Ident(){ //identity element
		return inf;
	}
	llint ope(llint a, llint b){ //operator
		return min(a, b);
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++){
			seg[i] = Ident();
			delay[i] = 0; //
		}
	}
	
	void eval(int l, int r, int k) //
	{
		if(delay[k]){
			seg[k] += delay[k];  //総和クエリのときは区間幅を乗じる必要がある
			if(l < r){
				delay[k*2] += delay[k];
				delay[k*2+1] += delay[k];
			}
			delay[k] = 0;
		}
	}
	
	void update(int i, llint val)
	{
		int l = 0, r = (1<<size)-1, k = 1;
		
		eval(l, r, k);
		for(int j = size-1; j >= 0; j--){
			k <<= 1;
			if(i & (1<<j)){
				k++;
				l = (l+r)/2+1;
			}
			else r = (l+r)/2;
			eval(l, r, k);
		}
		
		seg[i+(1<<size)] = val;
		
		l = i, r = i, k = i+(1<<size);
		for(int j = 0; j < size; j++){
			k /= 2, l &= ~(1<<j), r |= 1<<j;
			eval(l, (l+r)/2, k*2), eval((l+r)/2+1, r, k*2+1);
			seg[k] = ope(seg[k*2], seg[k*2+1]);
		}
	}
	
	void add(int a, int b, int k, int l, int r, llint val)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return;
		if(a <= l && r <= b){
			delay[k] += val; //
			eval(l, r, k);
			return;
		}
		add(a, b, k*2, l, (l+r)/2, val);
		add(a, b, k*2+1, (l+r)/2+1, r, val);
		seg[k] = ope(seg[k*2], seg[k*2+1]);
	}
	void add(int a, int b, llint val){
		if(a > b) return;
		add(a, b, 1, 0, (1<<size)-1, val);
	}
 
	llint query(int a, int b, int k, int l, int r)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return Ident();
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return ope(lval, rval);
	}
	llint query(int a, int b)
	{
		if(a > b) return Ident();
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

ll n, Q;
ll a[200005];
LazySegTree seg(18);
multiset<ll> S;

P get(ll x)
{
	x = n-2-x;
	x += 2000000000 * (n-1);
	ll q = x / (n-1) - 2000000000, r = x % (n-1);
	return P(q, r);
}

ll find(ll x, ll l = 0)
{
	ll ub = n-1, lb = l-1, mid;
	while(ub-lb>1){
		mid = (ub+lb)/2;
		if(seg.query(l, mid) <= x) ub = mid;
		else lb = mid;
	}
	return ub;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	rep(i, 1, n) cin >> a[i], S.insert(a[i]);
	cin >> Q;
	
	ll p, x;
	if(n == 1){
		rep(q, 1, Q){
			cin >> p >> x;
			a[p] = x;
			cout << max(0LL, a[1]-(n-2)) << "\n";
		}
		return 0;
	}
	
	seg.init();
	rep(i, 0, n-1) seg.update(i, i);
	
	ll b = 0;
	rep(i, 1, n){
		P res = get(a[i]);
		//cout << res.first << " " << res.second << endl;
		b += res.first, seg.add(n-1-res.second, n-1, -1);
	}
	
	rep(q, 1, Q){
		cin >> p >> x;
		
		P res = get(a[p]);
		b -= res.first, seg.add(n-1-res.second, n-1, 1);
		S.erase(S.lower_bound(a[p]));
		
		a[p] = x;
		
		res = get(a[p]);
		b += res.first, seg.add(n-1-res.second, n-1, -1);
		S.insert(a[p]);
		
		ll lb = max(0LL, *S.rbegin() - (n-2));
		
		ll d = -b - lb/(n-1), ans = lb/(n-1)*(n-1);
		if(seg.query(lb%(n-1), n-1) <= -d){
			ans += find(-d, lb%(n-1));
			cout << ans << endl;
			continue;
		}
		d--, ans += n-1;
		
		ll m = -seg.query(0, n-1);
		if(d <= m) ans += find(-d);
		else ans += (d-m) * (n-1) + find(-m);
		cout << ans << endl;
	}

	return 0;
}
0