結果

問題 No.875 Range Mindex Query
ユーザー fura
提出日時 2020-06-01 03:55:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 197,544 KB
最終ジャッジ日時 2025-01-10 20:13:40
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:34:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   34 |         int n,q; scanf("%d%d",&n,&q);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:37:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |                 scanf("%d",&p[i]); p[i]--;
      |                 ~~~~~^~~~~~~~~~~~
main.cpp:45:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |                 int type,l,r; scanf("%d%d%d",&type,&l,&r);
      |                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

class segment_tree{
	int n;
	vector<int> dat;
	int query(int l,int r,int a,int b,int u){
		if(l<=a && b<=r) return dat[u];
		int res=INF;
		int c=(a+b)/2;
		if(l<c && a<r) res=min(res,query(l,r,a,c,2*u+1));
		if(l<b && c<r) res=min(res,query(l,r,c,b,2*u+2));
		return res;
	}
public:
	segment_tree(int N){
		for(n=1;n<N;n*=2);
		dat.assign(2*n-1,0);
	}
	void update(int u,int val){
		u+=n-1;
		dat[u]=val;
		while(u>0) u=(u-1)/2, dat[u]=min(dat[2*u+1],dat[2*u+2]);
	}
	int query(int l,int r){ return query(l,r,0,n,0); }
};

int main(){
	int n,q; scanf("%d%d",&n,&q);
	vector<int> p(n),inv(n);
	rep(i,n){
		scanf("%d",&p[i]); p[i]--;
		inv[p[i]]=i;
	}

	segment_tree ST(n);
	rep(i,n) ST.update(i,p[i]);

	rep(_,q){
		int type,l,r; scanf("%d%d%d",&type,&l,&r);
		if(type==1){
			l--; r--;
			ST.update(l,p[r]);
			ST.update(r,p[l]);
			swap(inv[p[l]],inv[p[r]]);
			swap(p[l],p[r]);
		}
		else{
			l--;
			printf("%d\n",inv[ST.query(l,r)]+1);
		}
	}

	return 0;
}
0