結果

問題 No.875 Range Mindex Query
ユーザー bal4ubal4u
提出日時 2019-09-06 21:53:26
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,922 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 30,132 KB
実行使用メモリ 4,528 KB
最終ジャッジ日時 2023-09-06 23:39:54
合計ジャッジ時間 2,354 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 33 ms
4,384 KB
testcase_12 AC 27 ms
4,376 KB
testcase_13 AC 27 ms
4,504 KB
testcase_14 AC 26 ms
4,464 KB
testcase_15 AC 33 ms
4,480 KB
testcase_16 AC 44 ms
4,528 KB
testcase_17 AC 48 ms
4,456 KB
testcase_18 AC 45 ms
4,408 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:29:17: 備考: in expansion of macro ‘pc’
   29 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: 875 Range Mindex Query
// 2019.9.6 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(int n) { // 整数の表示
	int i; char b[30];

	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

#define INF 0x7ffffff
//// セグメント木
int seg[1 << 19]; int sz;

void segInit(int n) {
	sz = 1; while (sz < n) sz <<= 1;
	memset(seg, INF, sizeof(seg));
}

// k番目(0-index)の値を v に変更
void update(int k, int v)
{
	int a, b;

	k += sz - 1;
//	if (seg[id][k] == v) return;
	seg[k] = v;
	while (k > 0) {
		k = (k - 1) >> 1;
		a = seg[(k << 1) + 1];
		b = seg[(k << 1) + 2];
		seg[k] = (a <= b)? a: b;
	}
}

// 空間 [a, b) 内の最小値
int getMin(int a, int b, int k, int l, int r)
{
	int left, right;

	if (r <= a || b <= l) return INF;
	if (a <= l && r <= b) return seg[k];
	left = getMin(a, b, (k << 1) + 1, l, (l + r) >> 1);
	right = getMin(a, b, (k << 1) + 2, (l + r) >> 1, r);
	return left <= right ? left : right;
}

int a[100005];
int p[100005];

int main()
{
	int i, N, Q;
	int syn;

	N = in(), Q = in();
	segInit(N+1);            // セグメント木の初期化
	for (i = 1; i <= N; i++) {
		a[i] = in(), p[a[i]] = i;
		update(i, a[i]);
	}

	while (Q--) {
		int id = in(), l = in(), r = in();      // 0-index
		if (id == 1) {
			int x = a[l]; a[l] = a[r], a[r] = x;
			x = p[a[r]], p[a[r]] = p[a[l]], p[a[l]] = x;
			update(l, a[l]), update(r, a[r]);
		} else out(p[getMin(l, r+1, 0, 0, sz)]);
	}
	return 0;
}
0