【UVA】12299-RMQ with Shifts(线段树)

改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了。

14337858 12299 RMQ with Shifts Accepted C++ 0.282 2014-10-11 16:02:53

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson pos<<1
#define rson pos<<1|1
const int maxn = 111111;
const int  INF = 111111;
int arr[maxn];
int sz;
struct Node{
    int l,r;
    int _min;
}node[maxn <<2];
void BuildTree(int L,int R,int pos){
    node[pos].l = L; node[pos].r = R;
    if(L == R){
        scanf("%d",&node[pos]._min);
        arr[sz ++] = node[pos]._min;
        return ;
    }
    int m = (L + R) >> 1;
    BuildTree(L,m,lson);
    BuildTree(m + 1,R,rson);
    node[pos]._min = min(node[lson]._min,node[rson]._min);
    return;
}
void GetNum(int &l,int &r,char *str){
    int L = strlen(str),j;
    for(j = 0; j < L; j++){
        if(str[j] >= '0' && str[j] <= '9'){
            l = l * 10 + (str[j] - '0');
        }
        else if(l)
            break;
    }
    for(;j < L; j++){
        if(str[j] >= '0' && str[j] <= '9'){
            r = r * 10 + str[j] - '0';
        }
        else if(r)
            break;
    }
    return ;
}
int Query(int L,int R,int pos){
    if(L <= node[pos].l && node[pos].r <= R)
        return node[pos]._min;
    int m = (node[pos].l + node[pos].r) >> 1;
    int ret = INF;
    if(L <= m)
        ret = min(ret,Query(L,R,lson));
    if(R  > m)
        ret = min(ret,Query(L,R,rson));
    return ret;
}
void UpDate(int aim,int value,int pos){
    if(node[pos].l == node[pos].r){
        node[pos]._min = value;
        return ;
    }
    int m = (node[pos].l + node[pos].r) >> 1;
    if(aim <= m)
        UpDate(aim,value,lson);
    else
        UpDate(aim,value,rson);
    node[pos]._min = min(node[lson]._min,node[rson]._min);
    return;
}
int main(){
    int n,q;
    while(scanf("%d%d",&n,&q) != EOF){
        sz = 1;
        BuildTree(1,n,1);
        char str[100];
        int array[30];
        for(int x = 0; x < q; x++){
            scanf("%s",str);
            if(str[0] == 'q'){
                int l = 0,r = 0;
                GetNum(l,r,str);
                printf("%d
",Query(l,r,1));
            }
            else{
                memset(array,0,sizeof(array));
                int size = 0, n = strlen(str);
                for(int j = 0 ; j < n; j++){
                    if(str[j] >= '0' && str[j] <= '9'){
                        array[size] = array[size] * 10 + str[j] - '0';
                    }
                    else if(array[size] != 0){
                        size ++;
                    }
                }
                //左移一位
                int temp = arr[array[0]];
                for(int i = 0; i < size; i++){
                    int e;
                    if(i < size - 1){
                        UpDate(array[i],arr[array[i + 1]],1);
                        arr[array[i]] = arr[array[i + 1]];
                    }
                    else{
                        UpDate(array[i],temp,1);
                        arr[array[i]] = temp;
                    }
                }
            }
//            for(int i = 1 ;i <= n; i++) printf("%d ",arr[i]);
//            printf("
");
        }
    }
    return 0;
}