题目链接:http://oj.cs203.net/problem.php?id=1298
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAXN 100010
#define INF 0x7FFFffff
int sum[MAXN<<3], sign[MAXN<<3];
int n, m;
int a[MAXN];
inline void up_push(int rt){
sum[rt] = min(sum[rt<<1], sum[rt<<1|1]);
}
void build(int rt, int l, int r){
if( l==r ){ sum[rt] = a[l]; return ; }
int mid = (l+r)>>1;
build(rt<<1, l, mid);
build(rt<<1|1, mid+1, r);
up_push(rt);
}
void update(int rt, int l, int r, int x, int v){
if( l==r ){
sum[rt] = v;
return ;
}
int mid = (l+r)>>1;
if( x<=mid ) update(rt<<1, l, mid, x, v);
else update(rt<<1|1, mid+1, r, x, v);
up_push(rt);
}
int getsum(int rt, int l, int r, int x, int y){
if( x<=l && r<=y ) return sum[rt];
int mid=(l+r)>>1, r1=INF, r2=INF;
if( x<=mid ) r1 = getsum(rt<<1, l, mid, x, y);
if( mid<y ) r2 = getsum(rt<<1|1, mid+1, r, x, y);
return min(r1, r2);
}
int main(){
char s[10], ch;
int x, y;
while( 2==scanf("%d %d\n", &n, &m) ){
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
build(1, 1, n);
while( m-- && 1==scanf("%6s", s) ){
if( 'q'==s[0] ){
scanf("%d,%d)", &x, &y);
printf("%d\n", getsum(1, 1, n, x, y));
}
else{
int pre=0, now=0, tmp;
for(int i=1; ; i++){
scanf("%d%c", &now, &ch);
if( pre ){
update(1, 1, n, pre, a[now]);
swap(a[pre], a[now]);
}
else{
pre=now;
tmp=a[now];
}
pre = now;
if( ')'==ch ) break;
}
update(1, 1, n, pre, tmp);
}
}
}
return 0;
}