結果

問題 No.2988 Min-Plus Convolution Query
ユーザー ygussanyygussany
提出日時 2024-12-14 23:02:54
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 4,954 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 33,152 KB
実行使用メモリ 8,440 KB
最終ジャッジ日時 2024-12-14 23:03:08
合計ジャッジ時間 11,845 ms
ジャッジサーバーID
(参考情報)
judge5 / 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 RE -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
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 WA -
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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'main':
main.c:137:54: warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
  137 |         for (i = 1; i <= N; i++) if (num[i] >= 1000) exit(-1);
      |                                                      ^~~~
main.c:2:1: note: include '<stdlib.h>' or provide a declaration of 'exit'
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 | 
main.c:137:54: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
  137 |         for (i = 1; i <= N; i++) if (num[i] >= 1000) exit(-1);
      |                                                      ^~~~
main.c:137:54: note: include '<stdlib.h>' or provide a declaration of 'exit'

ソースコード

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;
}

// 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 argmin[])
{
	if (l3 > r3) return;
	chmax(&l2, l3 - r1);
	chmin(&r2, r3 - l1);
	
	int i, j, k = (l3 + r3) / 2, min = sup;
	for (i = l1, j = k - i; i <= r1 && j >= l2; i++, j--) {
		if (j > r2) continue;
		if (min > A[i] + B[j]) {
			min = A[i] + B[j];
			argmin[k] = i;
		}
	}
	min_plus_convolution(l1, argmin[k], l2, r2, l3, k - 1, A, B, argmin);
	min_plus_convolution(argmin[k], r1, l2, r2, k + 1, r3, A, B, argmin);
}

void solve(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[])
{
	static int argmin[400001];
	min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, argmin);
	
	int h, i, j, NN = N * 2 + 1, head, tail;
	static int appear[200001], q[2501];
	for (i = 1; i <= N; i++) appear[i] = 0;
	for (h = 1, tail = 0; h <= Q; h++) {
		A[p[h]] = x[h];
		if (appear[p[h]] == 0) {
			appear[p[h]] = 1;
			q[tail++] = p[h];
		}
		ans[h] = A[argmin[k[h]]] + B[k[h] - argmin[k[h]]];
		for (head = 0; head < tail; head++) {
			i = q[head];
			j = k[h] - i;
			if (1 <= j && j <= N) chmin(&(ans[h]), A[i] + B[j]);
		}
		
		if (tail == 5) {
			for (head = 0; head < tail; head++) appear[q[head]] = 0;
			tail = 0;
			min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, argmin);
		}
	}
}

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, argmin);
		ans[q] = A[argmin[k[q]]] + B[k[q] - argmin[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]));
	
	int num[200001] = {};
	for (i = 1; i <= Q; i++) num[p[i]]++;
	for (i = 1; i <= N; i++) if (num[i] >= 1000) exit(-1);
	/*
	solve(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;
	}
	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