結果

問題 No.875 Range Mindex Query
ユーザー WALLEatCoderWALLEatCoder
提出日時 2019-09-06 22:29:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 64,120 KB
実行使用メモリ 6,732 KB
最終ジャッジ日時 2023-09-07 01:11:56
合計ジャッジ時間 3,949 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 209 ms
6,332 KB
testcase_12 AC 174 ms
5,020 KB
testcase_13 AC 148 ms
6,528 KB
testcase_14 AC 144 ms
6,552 KB
testcase_15 AC 196 ms
6,344 KB
testcase_16 AC 270 ms
6,600 KB
testcase_17 AC 288 ms
6,728 KB
testcase_18 AC 275 ms
6,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<vector>
#include<iostream>
using namespace std;
#define df 0

struct mono{
  int no;
  int idx;
  
  mono(){no=123456; idx=0;}
  mono(int a,int i){no=a; idx=i;}
  mono(pair<int,int> p){no=p.first; idx=p.second;}
  pair<int,int> get(){return make_pair(no,idx);}
  mono operator+(mono m){
    if(no<m.no){return *this;}
    else return m;
  }
  void print(){printf("(%d,%d) ",no,idx);}
};

///  Segment Tree  (prototype)  ///
template<typename Monoid>
class SegTree{
  int n;
  vector<Monoid> node;
  
public:
  int parent(int a){return (a-1)/2;}
  int  left(int a) {return (a*2+1);}
  int right(int a) {return (a*2+2);}
  int leaf(int a){return (a+n-1);}
  SegTree(vector<Monoid> v);
  SegTree(int k);
  void update(int i,Monoid x);
  Monoid find(int a,int b,int i,int l,int r);
  Monoid find(int a,int b){return find(a,b,0,0,n);}
  Monoid find(int a){return node.at(leaf(a));}
  void print(){
    for(int i=0;i<2*n-1;i++){
      printf("%d: ",i);
      node.at(i).print();
      printf("\n");
    }
  }
};
///  Segment Tree  ///

int main(){
  if(df) printf("*debug mode*\n");
  int n,q; cin >>n >>q;
  vector<mono> p(n);
  for(int i=0;i<n;i++){
    int a; cin >>a;
    p.at(i)=(mono){a,i};
  }
  SegTree<mono> sgt(p);
  if(df)sgt.print();
  for(int i=0;i<q;i++){
    int flag; cin >>flag;
    if(flag==1){
      int l,r; cin >>l >>r; l--; r--;
      int a=sgt.find(l).get().first,b=sgt.find(r).get().first;
      sgt.update(l,(mono){b,l});
      sgt.update(r,(mono){a,r});
    }
    else{
      int l,r; cin >>l >>r; l--;
      int a=sgt.find(l,r).get().second;
      printf("%d\n",a+1);
    }
  }

  
}

///  Segment Tree  (main)  ///
template<typename Monoid>
SegTree<Monoid>::SegTree(vector<Monoid> v){
  int sz=v.size();
  n=1; while(n<sz)n*=2;
  node.resize(2*n-1);
  for(int i=0;i<v.size();i++){
    node.at(i+n-1)=v.at(i);
  }
  for(int i=n-2;i>=0;i--){
    node.at(i)=node.at(this->left(i))+node.at(right(i));
  }
}
template<typename Monoid>
SegTree<Monoid>::SegTree(int k){
  int sz=k;
  n=1; while(n<sz)n*=2;
  node.resize(2*n-1);
}

template<typename Monoid>
void SegTree<Monoid>::update(int i,Monoid x){
  i=this->leaf(i);
  node.at(i)=x;
  if(df){
    printf("%d:",i);
    x.print();
  }
  while(i>0){
    i=this->parent(i);
    node.at(i)=node.at(this->left(i))+node.at(this->right(i));
    if(df){
      printf("->%d:",i);
      node.at(i).print();
    }
  }
  if(df)printf("\n");
}
template<typename Monoid>
Monoid SegTree<Monoid>::find(int a,int b,int i,int l,int r){
  Monoid x;
  if(df)printf("[%d,%d) ",l,r);
  if(r<=a || b<=l)return x;
  if(a<=l && r<=b){
    if(df){
      printf("%d:",i);
      node.at(i).print();
      printf("\n");
    }
    return node.at(i);
  }
  Monoid m1=find(a,b,this->left(i) ,l,(l+r)/2);
  Monoid m2=find(a,b,this->right(i),(l+r)/2,r);
  x=m1+m2;
  return x;
}
///  Segment Tree  ///
/// confirm df==0 ///
0