結果

問題 No.2988 Min-Plus Convolution Query
ユーザー ygussanyygussany
提出日時 2024-12-14 11:37:12
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 5,310 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 34,048 KB
実行使用メモリ 7,320 KB
最終ジャッジ日時 2024-12-14 11:37:50
合計ジャッジ時間 32,909 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
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 TLE -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1 ms
6,820 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int sup = 2000000001;

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

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

void min_plus_convolution2(int l0, int r0, int l1, int r1, int l2, int r2, int l3, int r3, int A[], int B[], int end_argmin[])
{
	/*
	fprintf(stderr, "[%d %d %d %d %d %d]\n", l1, r1, l2, r2, l3, r3);
	for (int i = 0; i <= r0 - l0 + 1; i++) fprintf(stderr, "%d ", end_argmin[i]);
	fprintf(stderr, "\n");
	*/
	
	if (l3 > r3) return;
	if (l1 + l2 > r3) {
		chmin(&(end_argmin[l1-l0]), l3);
		return;
	}
	if (r1 + r2 < l3) {
		chmin(&(end_argmin[r1-l0]), r3);
		return;
	}
	if (l1 == r1) {
		chmin(&(end_argmin[l1-l0]), l3);
		return;
	}
	chmax(&l2, l3 - r1);
	chmin(&r2, r3 - l1);
	
	int i, j, k = (l3 + r3) / 2, min, argmin;
	i = l1;
	j = k - i;
	if (j > r2) {
		i += j - r2;
		j -= j - r2;
	}
	for (min = sup; i <= r1 && j >= l2; i++, j--) {
		if (min > A[i] + B[j]) {
			min = A[i] + B[j];
			argmin = i;
		}
	}
	if (min == sup) {
		if (l1 + l2 <= r3) argmin = l1;
		else argmin = r1;
	}
	chmin(&(end_argmin[argmin-l0]), k);
	min_plus_convolution2(l0, r0, l1, argmin, l2, r2, l3, k - 1, A, B, end_argmin);
	min_plus_convolution2(l0, r0, argmin, r1, l2, r2, k + 1, r3, A, B, end_argmin);
}

#define B_SIZE 200
#define B_NUM 1000

void solve2(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[])
{
	int i, j, l, r, n;
	static int l_block[B_NUM + 1], r_block[B_NUM + 1], end_argmin[B_NUM + 1][B_SIZE + 1];
	for (i = 1, l = 1, r = B_SIZE; l <= N; i++, l += B_SIZE, r += B_SIZE) {
		chmin(&r, N);
		l_block[i] = l;
		r_block[i] = r;
		for (j = 1, end_argmin[i][0] = 2; j <= r - l + 1; j++) end_argmin[i][j] = N * 2 + 1;
		min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, end_argmin[i]);
		for (j = r - l; j >= 1; j--) chmin(&(end_argmin[i][j]), end_argmin[i][j+1]);

		// for (j = 0; j <= r - l + 1; j++) fprintf(stderr, "%d ", end_argmin[i][j]);
		// fprintf(stderr, "\n");
	}
	n = i - 1;
	/*
	for (l = 2; l <= N * 2; l++) {
		fprintf(stderr, "%2d: ", l);
		for (i = 1, j = l - i; i <= N; i++, j--) {
			if (1 <= j && j <= N) fprintf(stderr, "%3d ", A[i] + B[j]);
			else fprintf(stderr, "100 ");
		}
		fprintf(stderr, "\n");
	}
	*/
	
	int q, m;
	for (q = 1; q <= Q; q++) {
		A[p[q]] = x[q];
		for (i = 1; r_block[i] < p[q]; i++);
		l = l_block[i];
		r = r_block[i];
		for (j = 1, end_argmin[i][0] = 2; j <= r - l + 1; j++) end_argmin[i][j] = N * 2 + 1;
		min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, end_argmin[i]);
		for (j = r - l; j >= 1; j--) chmin(&(end_argmin[i][j]), end_argmin[i][j+1]);

		// fprintf(stderr, "%d: ", i);
		// for (j = 0; j <= r - l + 1; j++) fprintf(stderr, "%d ", end_argmin[i][j]);
		// fprintf(stderr, "\n");
		
		ans[q] = sup;
		l = 0;
		r = r_block[i] - l_block[i] + 1;
		while (l < r) {
			m = (l + r) / 2;
			if (end_argmin[i][m] <= k[q]) l = m + 1;
			else r = m;
		}
		l += l_block[i] - 1;
		if (1 <= k[q] - l && k[q] - l <= N) chmin(&(ans[q]), A[l] + B[k[q] - l]);

		for (i = 1, ans[q] = sup; i <= n; i++) {
			/*
			l = 0;
			r = r_block[i] - l_block[i] + 1;
			while (l < r) {
				m = (l + r) / 2;
				if (end_argmin[i][m] <= k[q]) l = m + 1;
				else r = m;
			}
			*/
			l = l_block[i];
			if (1 <= k[q] - l && k[q] - l <= N) chmin(&(ans[q]), A[l] + B[k[q] - l]);
		}
	}
}

#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]));
	solve2(N, A, B, Q, p, x, k, ans);
	for (i = 1; i <= Q; i++) printf("%d\n", ans[i]);
	
	/*
	for (i = 1; i <= N; i++) A[i] = genrand() % 10;
	for (i = 1; i <= N; i++) B[i] = 0;
	for (i = 1; i <= Q; i++) {
		p[i] = genrand() % N + 1;
		x[i] = genrand() % 10;
		k[i] = genrand() % (N * 2 - 1) + 2;
	}
	solve2(N, A, B, Q, p, x, k, ans);
	for (i = 1; i <= Q; i++) printf("%d\n", ans[i]);
	*/
	fflush(stdout);
	return 0;
}
0