結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
Imperi_Night
|
| 提出日時 | 2019-09-06 21:43:10 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 620 ms / 2,000 ms |
| コード長 | 2,463 bytes |
| コンパイル時間 | 989 ms |
| コンパイル使用メモリ | 106,428 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-24 17:18:59 |
| 合計ジャッジ時間 | 5,092 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <assert.h>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using P = std::pair<ll, ll>;
#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug "<< i << std::endl
// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;
//最大値セグ木 0-indexed 初期値はminで指定
template <class T>
class SegmentTree {
private:
T n, init;
std::vector<T> dat;
std::function<T(T, T)> fn;
public:
SegmentTree(T i, T para, std::function<T(T, T)> fun) : init(para), fn(fun) {
n = 1;
while (n < i) {
n *= 2;
}
dat = std::vector<T>(2 * n - 1, init);
}
// k番目(0-indexed)を値aで更新,dest=trueのときは更新前を破壊して初期化する
void update(T k, T a, bool dest) {
k += n - 1;
if (dest)
dat[k] = a;
else
dat[k] = fn(dat[k], a);
while (k > 0) {
k = (k - 1) / 2;
dat[k] = fn(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
T query(T a, T b, T k, T l, T r) {
if (r <= a || b <= l) {
return init;
}
if (a <= l && r <= b) {
return dat[k];
} else {
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return fn(vl, vr);
}
}
//[a,b)での最大値を返す
T query(T a, T b) { return query(a, b, 0, 0, n); }
T lower_bound(T a,T b,T value){
ll left=a,right=b;
while(right-left>1){
ll mid=left+(right-left)/2;
if(query(a,mid)==value)right=mid;
else left=mid;
}
return left;
}
};
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
//問題文中の添え字が0-indexか1-indexか確認!
ll n,q;
std::cin>>n>>q;
SegmentTree<ll> a(n+2,MOD,[](ll x,ll y){return (x<y)?x:y;});
rep(i,0,n){
ll temp;
std::cin>>temp;
a.update(i,temp,true);
}
rep(i,0,q){
ll query,l,r;
std::cin>>query>>l>>r;
l--;r--;
if(query==1){
ll al=a.query(l,l+1),ar=a.query(r,r+1);
a.update(l,ar,true);a.update(r,al,true);
}else{
ll value=a.query(l,r+1);
std::cout<<a.lower_bound(l,r+1,value)+1<<"\n";
}
}
return 0;
}
Imperi_Night