結果

問題 No.875 Range Mindex Query
ユーザー snrnsidysnrnsidy
提出日時 2021-03-05 07:36:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,790 bytes
コンパイル時間 1,143 ms
コンパイル使用メモリ 122,872 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 20:39:41
合計ジャッジ時間 3,541 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 91 ms
5,376 KB
testcase_12 AC 72 ms
5,376 KB
testcase_13 AC 64 ms
5,376 KB
testcase_14 AC 63 ms
5,376 KB
testcase_15 AC 85 ms
5,376 KB
testcase_16 AC 81 ms
5,376 KB
testcase_17 AC 88 ms
5,376 KB
testcase_18 AC 84 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>

using namespace std;

int tree[1 << 20];
int a[100001];
int init(int node, int start, int end)
{
	if (start == end)
	{
		return tree[node] = start;
	}
	int mid = (start + end) / 2;
	int l = init(2 * node, start, mid);
	int r = init(2 * node + 1, mid + 1, end);
	if (a[l] > a[r])
	{
		tree[node] = r;
	}
	else if (a[l] < a[r])
	{
		tree[node] = l;
	}
	else
	{
		tree[node] = min(l, r);
	}
	return tree[node];
}

void update(int node, int start, int end, int index)
{
	if (index < start || index > end)
	{
		return;
	}
	if (start == end)
	{
		tree[node] = start;
		return;
	}
	if (start != end)
	{
		int mid = (start + end) / 2;
		update(2 * node, start, mid, index);
		update(2 * node + 1, mid + 1, end, index);
	}
	int x = tree[2 * node];
	int y = tree[2 * node + 1];
	//cout << node << ' ' << start << ' ' << end << ' ' << tree[node] << '\n';
	//cout << 2*node << ' ' << start << ' ' << (start+end)/2 << ' ' << x <<  ' ' << a[x] << '\n';
	//cout << 2*node+1 << ' ' << (start+end)/2 + 1 << ' ' << end << ' ' << y << ' ' << a[y] << '\n'; 
	if (a[x] < a[y])
	{
		tree[node] = x;
	}
	else if (a[x] > a[y])
	{
		tree[node] = y;
	}
	else
	{
		tree[node] = min(x, y);
	}
}

int find(int node, int start, int end, int left, int right)
{
	if (right < start || end < left)
	{
		return 0;
	}
	if (left <= start && end <= right)
	{
		return tree[node];
	}
	int mid = (start + end) / 2;
	int x = find(2 * node, start, mid, left, right);
	int y = find(2 * node + 1, mid + 1, end, left, right);
	//cout << node << ' ' << start << ' ' << end << ' ' << tree[node] << '\n';
	//cout << 2*node << ' ' << start << ' ' << (start+end)/2 << ' ' << x <<  ' ' << a[x] << '\n';
	//cout << 2*node+1 << ' ' << (start+end)/2 + 1 << ' ' << end << ' ' << y << ' ' << a[y] << '\n'; 
	if (a[x] < a[y])
	{
		return x;
	}
	else if (a[x] > a[y])
	{
		return y;
	}
	return min(x, y);
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m, x, y, z;

	cin >> n >> m;

	for (int i = 0; i <= 100000; i++)
	{
		a[i] = 2e9;
	}

	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}

	init(1, 0, n);

	for (int i = 0; i < m; i++)
	{
		cin >> x >> y >> z;
		if (x == 1)
		{
			int tmpa = a[y];
			int tmpb = a[z];
			a[y] = tmpb;
			a[z] = tmpa;
			update(1, 0, n, y);
			update(1, 0, n, z);
			//init(1,0,n);
		}
		else
		{
			cout << find(1, 0, n, y, z) << '\n';
		}
	}

	return 0;
}
0