結果

問題 No.875 Range Mindex Query
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-08-01 12:39:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 1,878 ms
コンパイル使用メモリ 104,600 KB
実行使用メモリ 5,216 KB
最終ジャッジ日時 2023-09-22 06:19:41
合計ジャッジ時間 4,786 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 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,500 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 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 3 ms
4,376 KB
testcase_11 AC 201 ms
5,108 KB
testcase_12 AC 165 ms
4,376 KB
testcase_13 AC 145 ms
5,104 KB
testcase_14 AC 142 ms
5,212 KB
testcase_15 AC 191 ms
5,212 KB
testcase_16 AC 253 ms
5,216 KB
testcase_17 AC 275 ms
5,088 KB
testcase_18 AC 263 ms
5,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
 
const int inf = (int)1e9; 
 
ll gcd(ll a, ll b) {
	if (b > a) {
		ll tmp = b;
		b = a;
		a = tmp;
	}
	if (a%b == 0)return b;
	else return gcd(b, a%b);
}
template <typename T>
struct SegmentTree{
    typedef function<T(T,T)> F;
    int n;
    F f;
    T unit;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(int newn,F f,T t):f(f),unit(t){
        init(newn);
    }
    SegmentTree(const vector<T> &v,F f,T t):f(f),unit(t){
        int n_ = v.size();
        init(n_);
        for(int i=0;i<n_;i++)dat[n+i]=v[i];
        for(int i=n-1;i;i--){
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
        }
    }
 
    void init(int n_){
        n=1;
        while(n<n_)n<<=1;
        dat.assign(n<<1,unit);
    }
    
    void update(int k,T x){
        dat[k+=n]=x;
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
    }
    T query(int a,int b){
        T vl=unit,vr=unit;
        for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
            if(l&1)vl=f(vl,dat[l++]);
            if(r&1)vr=f(dat[--r],vr);
        }
        return f(vl,vr);
    }
 
};

int main(void){
	int n,q;
	cin>>n>>q;
	auto f = [](P l,P r){
		if(l.second>r.second)return r;
		else return l;
	};
	SegmentTree<P>seg = SegmentTree<P>(n,f,P(-1,inf));
	for(int i=0;i<n;i++){
		int a;
		cin>>a;
		seg.update(i,P(i,a));
	}
	for(int i=0;i<q;i++){
		int c;
		cin>>c;
		if(c==1){
			int l,r;
			cin>>l>>r;
			P tmp1 = seg.query(l-1,l);
			int vl = tmp1.second;
			P tmp2 = seg.query(r-1,r);
			int vr = tmp2.second;
			seg.update(l-1,P(l-1,vr));
			seg.update(r-1,P(r-1,vl));
		}
		else{
			int l,r;
			cin>>l>>r;
			P ans = seg.query(l-1,r);
			cout<<ans.first+1<<endl;
		}
	}
	return 0;
}
0