結果

問題 No.2988 Min-Plus Convolution Query
ユーザー ygussanyygussany
提出日時 2024-12-13 15:47:46
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 6,166 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 34,816 KB
実行使用メモリ 47,540 KB
最終ジャッジ日時 2024-12-13 15:48:20
合計ジャッジ時間 31,164 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,060 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 104 ms
29,516 KB
testcase_06 AC 2 ms
8,064 KB
testcase_07 AC 2 ms
8,064 KB
testcase_08 AC 2 ms
8,068 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 17 ms
10,128 KB
testcase_12 AC 52 ms
8,056 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 641 ms
8,064 KB
testcase_18 WA -
testcase_19 AC 471 ms
8,056 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
8,056 KB
testcase_33 AC 2 ms
8,064 KB
testcase_34 AC 2 ms
8,056 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 2 ms
8,060 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int sup = 2000000001, inf = -sup;

void chmin(int *a, int b)
{
	if (*a > b) *a = b;
}

void chmax(int *a, int b)
{
	if (*a < b) *a = b;
}

typedef struct {
	int left, right, max, la, lb;
} lazy_seg_node;

// Initialize a lazy-update segment tree between l (>= 0) to r (k = 1 for use)
void init_node(lazy_seg_node v[], int k, int l, int r)
{
	v[k].left = l;
	v[k].right = r;
	v[k].max = 0;
	v[k].la = 1;
	v[k].lb = 0;
	if (l < r) {
		init_node(v, k << 1, l, (l + r) / 2);
		init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r); 
	}
}

// Update the max values from v[k] to the root
void update_max_to_root(lazy_seg_node v[], int k)
{
	int j;
	for (j = k >> 1; j > 0; k = j, j >>= 1) v[j].max = (v[k].max > v[k^1].max)? v[k].max: v[k^1].max;
}

// Push the lazy update from v[k] to just below (if exists)
void push_update_below(lazy_seg_node v[], int k)
{
	int j;
	if (v[k].left == v[k].right || (v[k].la == 1 && v[k].lb == 0)) return;
	j = k << 1;
	v[j].max = v[j].max * v[k].la + v[k].lb;
	v[j].la *= v[k].la;
	v[j].lb = v[j].lb * v[k].la + v[k].lb;
	j ^= 1;
	v[j].max = v[j].max * v[k].la + v[k].lb;
	v[j].la *= v[k].la;
	v[j].lb = v[j].lb * v[k].la + v[k].lb;
	v[k].la = 1;
	v[k].lb = 0;
}

// Update the max values x between l and r to x * a + b (k = 1 for use)
void update_segment(lazy_seg_node v[], int k, int l, int r, int a, int b)
{
	if (r < v[k].left || v[k].right < l) return;
	else if (l <= v[k].left && v[k].right <= r) {
		v[k].max = v[k].max * a + b;
		v[k].la *= a;
		v[k].lb = v[k].lb * a + b;
		update_max_to_root(v, k);
	} else {
		push_update_below(v, k);
		update_segment(v, k << 1, l, r, a, b);
		update_segment(v, (k << 1) ^ 1, l, r, a, b);
	}
}

// Get the max value between l and r (k = 1 for use)
int get_max(lazy_seg_node v[], int k, int l, int r)
{
	int tmp[2];
	if (r < v[k].left || v[k].right < l) return inf;
	else if (l <= v[k].left && v[k].right <= r) return v[k].max;
	else {
		push_update_below(v, k);
		tmp[0] = get_max(v, k << 1, l, r);
		tmp[1] = get_max(v, (k << 1) ^ 1, l, r);
		return (tmp[0] > tmp[1])? tmp[0]: tmp[1];
	}
}

// Find the smallest index between l and r whose max value is at least x (k = 1 for use)
int BS_left(lazy_seg_node v[], int k, int l, int r, int x)
{
	int tmp;
	if (v[k].max < x || r < v[k].left || v[k].right < l) return r + 1;
	else if (v[k].left == v[k].right) return v[k].left;
	else {
		push_update_below(v, k);
		tmp = BS_left(v, k << 1, l, r, x);
		if (tmp <= r) return tmp;
		else return BS_left(v, (k << 1) ^ 1, l, r, x);
	}
}

// Find the largest index between l and r whose max value is at least x (k = 1 for use)
int BS_right(lazy_seg_node v[], int k, int l, int r, int x)
{
	int tmp;
	if (v[k].max < x || r < v[k].left || v[k].right < l) return l - 1;
	else if (v[k].left == v[k].right) return v[k].left;
	else {
		push_update_below(v, k);
		tmp = BS_right(v, (k << 1) ^ 1, l, r, x);
		if (tmp >= l) return tmp;
		else return BS_right(v, k << 1, l, r, x);
	}
}

// B should convex
// min[l3-r3] will be min-plus convolution of (A[l1-r1], B[l2-r2])
void min_plus_convolution(int l1, int r1, int l2, int r2, int l3, int r3, int A[], int B[], int min[], int argmin[])
{
	if (l3 > r3) return;
	chmax(&l2, l3 - r1);
	chmin(&r2, r3 - l1);
	
	int i, j, k = (l3 + r3) / 2;
	for (i = l1, j = k - i, min[k] = sup; i <= r1 && j >= l2; i++, j--) {
		if (j > r2) continue;
		if (min[k] > A[i] + B[j]) {
			min[k] = A[i] + B[j];
			argmin[k] = i;
		}
	}
	min_plus_convolution(l1, argmin[k], l2, r2, l3, k - 1, A, B, min, argmin);
	min_plus_convolution(argmin[k], r1, l2, r2, k + 1, r3, A, B, min, argmin);
}

void min_plus_convolution_naive(int n1, int n2, int A[], int B[], int min[], int argmin[])
{
	int i, j, k;
	for (k = 2; k <= n1 + n2; k++) min[k] = sup;
	for (i = 1; i <= n1; i++) {
		for (j = 1; j <= n2; j++) {
			k = i + j;
			if (min[k] > A[i] + B[j]) {
				min[k] = A[i] + B[j];
				argmin[k] = i;
			}
		}
	}
}

int fix_argmin(int N, int A[], int B[], lazy_seg_node v[], int k)
{
	int i = get_max(v, 1, k, k);
	if (i > 0) return i;

	int NN = N * 2 + 1, j, l, r, min, argmin;
	l = BS_right(v, 1, 1, k - 1, 1);
	r = BS_left(v, 1, k + 1, NN, 1);
	l = get_max(v, 1, l, l);
	r = get_max(v, 1, r, r);
	for (i = l, j = k - i, min = sup; i <= r && j >= 1; i++, j--) {
		if (j <= N && min > A[i] + B[j]) {
			min = A[i] + B[j];
			argmin = i;
		}
	}
	update_segment(v, 1, k, k, 0, argmin);
	return argmin;
}

int main()
{
	int i, N, Q, A[200001], B[200001];
	scanf("%d %d", &N, &Q);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(B[i]));
	
	int min[400001], argmin[400001];
	min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, min, argmin);
	
	int NN = N * 2 + 1;
	lazy_seg_node v[2][1048576];
	init_node(v[0], 1, 1, NN);
	init_node(v[1], 1, 1, NN);
	for (i = 2; i <= NN - 1; i++) {
		update_segment(v[0], 1, i, i, 0, argmin[i]);
		update_segment(v[1], 1, i, i, 0, -argmin[i]);
	}
	update_segment(v[0], 1, 1, 1, 0, 1);
	update_segment(v[0], 1, NN, NN, 0, N);
	update_segment(v[1], 1, 1, 1, 0, -1);
	update_segment(v[1], 1, NN, NN, 0, -N);

	int p, x, k, l, r, m, ll, rr;
	while (Q--) {
		scanf("%d %d %d", &p, &x, &k);
		if (x > A[p]) {
			l = BS_right(v[1], 1, 1, NN, -(p - 1));
			r = BS_left(v[0], 1, 1, NN, p + 1);
			if (l == 0) l++;
			if (r == NN + 1) r--;
			update_segment(v[0], 1, l + 1, r - 1, 0, inf);
			update_segment(v[1], 1, l + 1, r - 1, 0, sup);
		} else if (x < A[p]) {
			l = 2;
			r = NN - 1;
			while (l < r) {
				m = (l + r + 1) / 2;
				i = fix_argmin(N, A, B, v[0], m);
				if (i >= p || (i < p && 1 <= m - p && m - p <= N && x + B[m-p] < A[i] + B[m-i])) r = m - 1;
				else l = m;
			}
			ll = l;

			l = 2;
			r = NN - 1;
			while (l < r) {
				m = (l + r) / 2;
				i = fix_argmin(N, A, B, v[0], m);
				if (i <= p || (i > p && 1 <= m - p && m - p <= N && x + B[m-p] < A[i] + B[m-i])) l = m + 1;
				else r = m;
			}
			rr = r;
			
			update_segment(v[0], 1, ll + 1, rr - 1, 0, p);
			update_segment(v[1], 1, ll + 1, rr - 1, 0, -p);
		}
		A[p] = x;
		
		i = fix_argmin(N, A, B, v[0], k);
		update_segment(v[1], 1, k, k, 0, -i);
		printf("%d\n", A[i] + B[k-i]);
	fflush(stdout);
	}
	fflush(stdout);
	return 0;
}
0