RMQ with Shifts

题意:

n个数两种操作,1、给出一组位置,使各位置的数循环移动,2、求给定区间的最小值。

分析:

单点更新,区间最值

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int minv[100010*4],n,q,a[100010];
char s[35];
void pushup(int rt){
    minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
       minv[rt]=a[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int v,int l,int r,int rt){
    if(l==r){
        minv[rt]=v;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m)update(p,v,lson);
    else update(p,v,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r)
        return minv[rt];
    int m=(l+r)>>1;
    int num=INF;
    if(L<=m)num=min(num,query(L,R,lson));
    if(R>m) num=min(num,query(L,R,rson));
    pushup(rt);
    return num;
}
int main()
{
    int numb[15],id;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    build(1,n,1);
    while(q--){
        scanf("%s",s);
        int tmp=0;
        id=0;
        for(int i=6;s[i]!=‘\0‘;++i){
            if(s[i]-‘0‘>=0&&s[i]-‘0‘<=9)
                tmp=tmp*10+s[i]-‘0‘;
            else if(s[i]==‘,‘||s[i]==‘)‘)
            {
                numb[id++]=tmp;
                tmp=0;
            }
        }
        if(s[0]==‘q‘)
            printf("%d\n",query(numb[0],numb[1],1,n,1));
        else{
                int tmp=a[numb[0]];
            for(int i=0;i<id-1;++i){
                update(numb[i],a[numb[i+1]],1,n,1);
                a[numb[i]]=a[numb[i+1]];
            }
            update(numb[id-1],tmp,1,n,1);
            a[numb[id-1]]=tmp;
        }
    }
return 0;
}
时间: 2024-10-10 22:28:28

RMQ with Shifts的相关文章

湖南省第七届大学生计算机程序设计竞赛 RMQ with Shifts (线段树)

RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], -, A[R]. Note that the indic

UVa 12299 RMQ with Shifts(线段树)

线段树,没了.. ----------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cctype> #define rep(i,n) for(int i=0;i<n

Uva 12299 RMQ with Shifts(线段树 + 单点更新 )

Uva 12299 RMQ with Shifts (线段树 + 单点更新) 题意: 对于给定的序列 x[i]给出一个操作 shift(a,b,c,d,e) 对应的是将 x[a]与x[b] x[b]与x[c] 这样相邻的两两交换For example, if A={6, 2, 4, 8, 5, 1, 4}then shift(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that,shift(1, 2) yields {8, 6, 4, 5, 4

UVa 12299 RMQ with Shifts(移位RMQ)

UVa 12299 - RMQ with Shifts(移位RMQ) Time limit: 1.000 seconds Description - 题目描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L,R) (L ≤ R), we report the minimum value among A[L], A[L + 1], ...,

nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], …, A[R]. Note that the indic

C. RMQ with Shifts

C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB 64-bit integer IO format: %lld      Java class name: Main In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) 

线段树模板(区间最小值优化 版) (RMQ with Shifts)

题意: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <fstream> using namespace std; const int maxn =100010; int RMQ[maxn<<2]; int str[maxn]; int N,M; char ctr[35]; int total[35],cn

树状数组求最大值 (RMQ with Shifts)

代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; const int Max=200010; int RMQ[Max+10]; int total[Max]; int sum[35]; int N,M,cnt; char ctr[35]; int bit(int x) {//每一个下标管辖的范围 ret

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 s