結果

問題 No.2988 Min-Plus Convolution Query
ユーザー ygussanyygussany
提出日時 2024-12-13 16:40:34
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 9,091 bytes
コンパイル時間 680 ms
コンパイル使用メモリ 36,864 KB
実行使用メモリ 66,972 KB
最終ジャッジ日時 2024-12-13 16:41:30
合計ジャッジ時間 54,924 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,496 KB
testcase_01 AC 1 ms
55,552 KB
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 108 ms
29,824 KB
testcase_06 AC 1 ms
18,968 KB
testcase_07 AC 2 ms
21,140 KB
testcase_08 AC 1 ms
10,496 KB
testcase_09 AC 89 ms
10,624 KB
testcase_10 AC 83 ms
62,976 KB
testcase_11 AC 19 ms
10,624 KB
testcase_12 AC 35 ms
55,296 KB
testcase_13 AC 476 ms
10,624 KB
testcase_14 AC 54 ms
55,168 KB
testcase_15 AC 756 ms
10,752 KB
testcase_16 TLE -
testcase_17 AC 471 ms
10,624 KB
testcase_18 AC 476 ms
55,168 KB
testcase_19 AC 304 ms
10,624 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 1 ms
5,248 KB
testcase_33 AC 1 ms
5,248 KB
testcase_34 AC 1 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 2 ms
5,248 KB
testcase_37 AC 1 ms
5,248 KB
testcase_38 AC 2 ms
5,248 KB
testcase_39 AC 2 ms
5,248 KB
testcase_40 AC 2 ms
5,248 KB
testcase_41 AC 2 ms
55,296 KB
権限があれば一括ダウンロードができます

ソースコード

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);
	i = l;
	j = k - i;
	if (j > N) {
		i += j - N;
		j -= j - N;
	}
	for (min = sup; i <= r && j >= 1; i++, j--) {
		if (min > A[i] + B[j]) {
			min = A[i] + B[j];
			argmin = i;
		}
	}
	update_segment(v, 1, k, k, 0, argmin);
	return argmin;
}

void solve(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[])
{
	static int min[400001], argmin[400001];
	min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, min, argmin);
	
	int i, NN = N * 2 + 1;
	static 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 q, l, r, m, ll, rr;
	for (q = 1; q <= Q; q++) {
		if (x[q] > A[p[q]]) {
			l = BS_right(v[1], 1, 1, NN, -(p[q] - 1));
			r = BS_left(v[0], 1, 1, NN, p[q] + 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, inf);
		} else if (x[q] < A[p[q]]) {
			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[q] || (i < p[q] && 1 <= m - p[q] && m - p[q] <= N && x[q] + B[m-p[q]] < 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[q] || (i > p[q] && 1 <= m - p[q] && m - p[q] <= N && x[q] + B[m-p[q]] < A[i] + B[m-i])) l = m + 1;
				else r = m;
			}
			rr = r;
			
			update_segment(v[0], 1, ll + 1, rr - 1, 0, p[q]);
			update_segment(v[1], 1, ll + 1, rr - 1, 0, -p[q]);
		}
		A[p[q]] = x[q];
		
		i = fix_argmin(N, A, B, v[0], k[q]);
		update_segment(v[1], 1, k[q], k[q], 0, -i);
		ans[q] = A[i] + B[k[q]-i];
	}
}

void naive(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[])
{
	int q;
	static int min[400001], argmin[400001];
	for (q = 1; q <= Q; q++) {
		A[p[q]] = x[q];
		min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, min, argmin);
		ans[q] = min[k[q]];
	}
}

#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL
#define MT_UPPER_MASK 0x80000000UL
#define MT_LOWER_MASK 0x7fffffffUL

static unsigned int mt[MT_N];
static int mti = MT_N + 1;

void init_genrand(unsigned int s)
{
    mt[0] = s & 0xffffffffUL;
    for (mti = 1; mti < MT_N; mti++) {
        mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        mt[mti] &= 0xffffffffUL;
    }
}

unsigned int genrand()
{
    unsigned int y;
    static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A};

    if (mti >= MT_N) {
        int kk;
        if (mti == MT_N + 1) init_genrand(5489UL);
		
        for (kk = 0; kk < MT_N - MT_M; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        for (; kk < MT_N - 1; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
        mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL];

        mti = 0;
    }
  
    y = mt[mti++];

    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

    return y;
}

int main()
{
	int i, N, Q;
	static int A[200001], B[200001], p[200001], x[200001], k[200001], ans[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]));
	for (i = 1; i <= Q; i++) scanf("%d %d %d", &(p[i]), &(x[i]), &(k[i]));
	solve(N, A, B, Q, p, x, k, ans);
	for (i = 1; i <= Q; i++) printf("%d\n", ans[i]);
	
	/*
	static int AA[200001], BB[200001], anss[200001];
	while (1) {
		for (i = 1; i <= N; i++) AA[i] = genrand() % 10;
		for (i = 1; i <= N; i++) BB[i] = (i - (N + 1) / 2) * (i - (N + 1) / 2);
		for (i = 1; i <= Q; i++) {
			p[i] = genrand() % N + 1;
			x[i] = genrand() % 10;
			k[i] = genrand() % (N * 2 - 1) + 2;
		}
		
		for (i = 1; i <= N; i++) {
			A[i] = AA[i];
			B[i] = BB[i];
		}
		solve(N, A, B, Q, p, x, k, ans);
		for (i = 1; i <= N; i++) {
			A[i] = AA[i];
			B[i] = BB[i];
		}
		naive(N, A, B, Q, p, x, k, anss);
		for (i = 1; i <= Q; i++) if (ans[i] != anss[i]) break;
		if (i <= Q) {
			for (i = 1; i <= N; i++) printf("%d ", AA[i]);
			printf("\n");
			for (i = 1; i <= N; i++) printf("%d ", BB[i]);
			printf("\n");
			for (i = 1; i <= Q; i++) printf("%d %d %d\n", p[i], x[i], k[i]);
			printf("\n");
			for (i = 1; i <= Q; i++) printf("%d ", ans[i]);
			printf("\n");
			for (i = 1; i <= Q; i++) printf("%d ", anss[i]);
			printf("\n");
			break;
		}
	}
	*/
	fflush(stdout);
	return 0;
}
0