【UVA】12299-RMQ with Shifts(線段樹),12299-rmqshifts

來源:互聯網
上載者:User

【UVA】12299-RMQ with Shifts(線段樹),12299-rmqshifts

修改的時候由於資料很小,所以可以直接暴力修改,查詢的時候利用線段樹就行了。

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|1const 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\n",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("\n");        }    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.