結果

問題 No.2988 Min-Plus Convolution Query
ユーザー ygussanyygussany
提出日時 2024-12-14 11:25:54
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 5,039 bytes
コンパイル時間 541 ms
コンパイル使用メモリ 34,304 KB
実行使用メモリ 20,540 KB
最終ジャッジ日時 2024-12-14 11:27:09
合計ジャッジ時間 57,209 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,752 KB
testcase_01 AC 1 ms
13,764 KB
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 28 ms
13,460 KB
testcase_06 AC 2 ms
13,588 KB
testcase_07 AC 2 ms
13,584 KB
testcase_08 AC 2 ms
13,608 KB
testcase_09 AC 49 ms
13,764 KB
testcase_10 AC 86 ms
13,464 KB
testcase_11 AC 7 ms
13,764 KB
testcase_12 AC 47 ms
13,584 KB
testcase_13 AC 1,883 ms
13,768 KB
testcase_14 AC 56 ms
13,768 KB
testcase_15 AC 1,652 ms
13,768 KB
testcase_16 TLE -
testcase_17 AC 756 ms
13,476 KB
testcase_18 AC 524 ms
13,464 KB
testcase_19 AC 185 ms
14,092 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
6,816 KB
testcase_33 AC 2 ms
6,816 KB
testcase_34 AC 1 ms
6,816 KB
testcase_35 AC 2 ms
6,816 KB
testcase_36 AC 1 ms
6,824 KB
testcase_37 AC 2 ms
6,816 KB
testcase_38 AC 2 ms
6,820 KB
testcase_39 AC 2 ms
6,820 KB
testcase_40 AC 2 ms
6,816 KB
testcase_41 AC 2 ms
13,600 KB
権限があれば一括ダウンロードができます

ソースコード

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 400
#define B_NUM 500

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");

		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] - 1;
			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