結果

問題 No.1394 Changing Problems
ユーザー leaf_1415leaf_1415
提出日時 2021-02-13 04:29:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 4,169 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 105,576 KB
実行使用メモリ 22,412 KB
最終ジャッジ日時 2023-09-27 11:37:49
合計ジャッジ時間 26,338 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,440 KB
testcase_01 AC 5 ms
11,184 KB
testcase_02 AC 6 ms
11,288 KB
testcase_03 AC 6 ms
11,288 KB
testcase_04 AC 6 ms
11,348 KB
testcase_05 AC 766 ms
22,136 KB
testcase_06 AC 718 ms
22,172 KB
testcase_07 AC 6 ms
11,184 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 27 ms
11,292 KB
testcase_11 AC 29 ms
11,260 KB
testcase_12 AC 26 ms
11,256 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 970 ms
22,148 KB
testcase_17 AC 963 ms
22,268 KB
testcase_18 AC 981 ms
22,228 KB
testcase_19 AC 978 ms
22,176 KB
testcase_20 AC 981 ms
22,112 KB
testcase_21 AC 989 ms
22,128 KB
testcase_22 AC 973 ms
22,156 KB
testcase_23 AC 965 ms
22,132 KB
testcase_24 AC 969 ms
22,232 KB
testcase_25 AC 972 ms
22,412 KB
testcase_26 AC 877 ms
22,232 KB
testcase_27 AC 872 ms
22,232 KB
testcase_28 AC 921 ms
22,116 KB
testcase_29 AC 792 ms
22,228 KB
testcase_30 AC 786 ms
22,124 KB
testcase_31 AC 786 ms
22,136 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(0, 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));
		
		if(b >= 0){
			cout << lb << endl;
			continue;
		}
		
		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