#include #include 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 p){no=p.first; idx=p.second;} pair get(){return make_pair(no,idx);} mono operator+(mono m){ if(no class SegTree{ int n; vector 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 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 p(n); for(int i=0;i>a; p.at(i)=(mono){a,i}; } SegTree sgt(p); if(df)sgt.print(); for(int i=0;i>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 SegTree::SegTree(vector v){ int sz=v.size(); n=1; while(n=0;i--){ node.at(i)=node.at(this->left(i))+node.at(right(i)); } } template SegTree::SegTree(int k){ int sz=k; n=1; while(n void SegTree::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 Monoid SegTree::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 ///