結果

問題 No.875 Range Mindex Query
ユーザー A8pfA8pf
提出日時 2019-12-05 16:51:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 2,025 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 171,684 KB
実行使用メモリ 6,720 KB
最終ジャッジ日時 2023-08-23 07:42:41
合計ジャッジ時間 4,544 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,500 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 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,380 KB
testcase_11 AC 185 ms
6,260 KB
testcase_12 AC 153 ms
4,928 KB
testcase_13 AC 130 ms
6,476 KB
testcase_14 AC 127 ms
6,420 KB
testcase_15 AC 176 ms
6,412 KB
testcase_16 AC 237 ms
6,604 KB
testcase_17 AC 255 ms
6,720 KB
testcase_18 AC 247 ms
6,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
class segtree
{
  public:
  int depth;//セグ木の深さ(最上部が0)
  int size;
  vector<P> node;//セグ木本体

  //コンストラクタ
  //初期値の代入もする
  segtree(int n,vector<P> v)
  {
    depth=0;
    size=1;
    while(n>size)
    {
      size*=2;
      depth++;
    }
    size*=2;
    node.resize(size,P(1e8-1,1e8-1));
    //最下段に値を入れたあとに、下の段から順番に値を入れる
    //値を入れるには自分の子の2値を参照する
    for(int i=0;i<n;i++)
      node[i+(1<<depth)]=v[i];
    
    for(int d=depth-1;d>=0;d--)
    {
      for(int i=0;i<(1<<d);i++)
      {
        int ind=i+(1<<d);
        node[ind]=min(node[ind*2],node[ind*2+1]);
      }
    }
  }

  //値の更新
  //node[k]にaを加える
  void update(int k,P a)
  {
    k+=(1<<depth);//セグ木上のインデックスに読み替え
    node[k]=a;
    while(k>1)
    {
      k/=2;
      node[k]=min(node[k*2],node[k*2+1]);
    }
    return;
  }
  
  //値の取得
  //閉区間[a,b]に値はいくつあるか
  P get(int a,int b)
  {
    a+=(1<<depth);
    b+=(1<<depth);
    if(a==b)
      return node[a];
    P nowa=node[a];
    P nowb=node[b];
    while(a/2<b/2)
    {
      //左側はより右側を見る
      if((a&1)==0)
        nowa=min(nowa,node[a+1]);
      //右側はより左側を見る
      if((b&1)==1)
        nowb=min(nowb,node[b-1]);
      a/=2;
      b/=2;
    }
    P ret=min(nowa,nowb);
    return ret;
  }
};

vector<P> vec;

int main()
{
  int n,q;
	cin>>n>>q;
	vec=vector<P>(n);
	for(int i=0;i<n;i++)
	{
		int v;
		cin>>v;
		vec[i]=P(v,i+1);
	}
	segtree seg=segtree(n,vec);
	for(int i=0;i<q;i++)
	{
		int c,x,y;
		cin>>c>>x>>y;
		x--;y--;
		if(c==1)
		{
			P a=seg.node[x+(1<<seg.depth)];
			P b=seg.node[y+(1<<seg.depth)];
			seg.update(x,P(b.first,a.second));
			seg.update(y,P(a.first,b.second));
		}else
			cout<<seg.get(x,y).second<<endl;
	}
  return 0;
}
0