• csu 1110 RMQ with Shifts (线段树单点更新)


    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #define lson l,m,rt<<1
    #define rson m + 1,r ,rt<<1|1
    using namespace std;
    const int maxn = 120005;
    int node[maxn<<2];
    int n,m,a[maxn],shift[maxn];
    void up(int rt)
    {
        node[rt] = min(node[rt<<1],node[rt<<1|1]);
    }
    void build(int l,int r,int rt)
    {
        if(l == r)
        {
            scanf("%d",&node[rt]);
            a[l] = node[rt];
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        up(rt);
    }
    int A,B;
    void update(int l,int r,int rt)
    {
        if(l == r)
        {
            node[rt] = B;
            return;
        }
        int m = (l + r) >> 1;
        if(A <= m) update(lson);
        else update(rson);
        up(rt);
    }
    int ans;
    void qurry(int l,int r,int rt)
    {
        if(A <= l && r <= B)
        {
            ans = min(ans,node[rt]);
            return;
        }
        int m = (l + r) >> 1;
        if(A <= m) qurry(lson);
        if(B > m) qurry(rson);
    }
    int main()
    {
        while(scanf("%d %d",&n,&m) != EOF)
        {
            build(1,n,1);
            char str[105];
            while(m--)
            {
                scanf("%s",str);
                int i = 6,len = 0;
                if(str[0] == 'q')
                {
                    /*
                    shift[0] = 0;
                    for(; str[i] != ')'; i++)
                    {
                        if(str[i] == ',')
                        {
                            len++;
                            shift[len] = 0;
                            continue;
                        }
                        shift[len] = shift[len] * 10 + str[i] - 48;
                    }
                    len++;
                    A = shift[0],B = shift[1];*/
                     
                    sscanf(str+5,"(%d,%d)",&A,&B);
                     
                    ans = maxn * 100;
                    qurry(1,n,1);
                    printf("%d
    ",ans);
                }
                else
                {
                    shift[0] = 0;
                    for(; str[i] != ')'; i++)
                    {
                        if(str[i] == ',')
                        {
                            len++;
                            shift[len] = 0;
                            continue;
                        }
                        shift[len] = shift[len] * 10 + str[i] - 48;
                    }
                    len++;
                    //printf("len  = %d
    ",len);
                    for(i = 0; i < len - 1; i++)
                    {
                        A = shift[i],B = a[shift[i+1]];
                       // printf("A = %d B = %d
    ",A,B);
                        update(1,n,1);
                    }
                    A = shift[i],B = a[shift[0]];
                    //printf("A = %d B = %d
    ",A,B);
                    update(1,n,1);
                    int tmp = a[shift[0]];
                    for(i = 0; i < len - 1; i++)
                    {
                        a[shift[i]] = a[shift[i+1]];
                    }
                    a[shift[i]] = tmp;
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    问题6-10
    7.19 1
    经济学人常见词汇清单
    英语广播原声听力100篇MP3及听力原文
    6.30.2018
    6.26
    6.26
    6.26
    6.25
    6.25
  • 原文地址:https://www.cnblogs.com/sola1994/p/4714028.html
Copyright © 2020-2023  润新知