結果

問題 No.875 Range Mindex Query
ユーザー silopysilopy
提出日時 2019-09-06 22:29:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 1,169 ms
コンパイル使用メモリ 88,400 KB
実行使用メモリ 9,404 KB
最終ジャッジ日時 2023-09-07 01:11:19
合計ジャッジ時間 4,534 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 290 ms
8,364 KB
testcase_12 AC 224 ms
6,424 KB
testcase_13 AC 222 ms
9,000 KB
testcase_14 AC 216 ms
8,736 KB
testcase_15 AC 295 ms
8,952 KB
testcase_16 AC 313 ms
8,944 KB
testcase_17 AC 338 ms
9,404 KB
testcase_18 AC 330 ms
9,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
using lint=int64_t;

constexpr int INF=1e9;

class SegmentTree
{
	private:
	public:
	int N;
	vector<int> tree;

	public:
	SegmentTree(vector<int>& src)
	{
		int size=src.size();
		N=1;
		while(N<size)N*=2;

		tree.resize(2*N-1);
		initTree();

		for(int i=0;i<size;i++)
			update(i,src[i]);
	}

	SegmentTree(int* src,int size)
	{
		N=1;
		while(N<size)N*=2;

		tree.resize(2*N-1);
		initTree();

		for(int i=0;i<size;i++)
			update(i,src[i]);
	}

	void initTree()
	{
		for(auto&& i:tree)
			i=INF;
	}

	void update(int i,int value)
	{
		tree[i+N-1]=value;
		
		i=i+N-1;
		while(i>0)
		{
			i=(i-1)/2;
			tree[i]=min(tree[2*i+1],tree[2*i+2]);
		}
	}

	int getMin(int tl,int tr,int l=0,int r=-1,int i=0)
	{
		if(r<0)
			r=N;

		if(tr<=l || r<=tl)
			return INF;

		if(tl<=l && r<=tr)
			return tree[i];

		int retl=getMin(tl,tr,l,(l+r)/2,2*i+1);
		int retr=getMin(tl,tr,(l+r)/2,r,2*i+2);

		return min(retl,retr);
	}

	int getv(int i)
	{
		return tree[i+N-1];
	}
};

int main()
{
	int N,Q;
	vector<int> a;
	map<int,int> index;

	cin >> N >> Q;
	for(int i=0;i<N;i++)
	{
		int in;
		cin >> in;
		a.push_back(in);
		index[in]=i;
	}
	
	SegmentTree seg(a);

	for(int i=0;i<Q;i++)
	{
		int o,l,r;
		cin >> o >> l >> r;

		if(o==1)
		{
			int vl=seg.getv(l-1);
			int vr=seg.getv(r-1);

			seg.update(l-1,vr);
			seg.update(r-1,vl);

			index[vl]=r-1;
			index[vr]=l-1;
		}
		if(o==2)
		{
			cout << index[seg.getMin(l-1,r)]+1 << endl;
		}
	}
	return 0;
}
0