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 indices start from 1, i.e. the left-most element is A[1].

In this problem, the array A is no longer static: we need to support another operation shift(i1, i2, i3, …, ik) (i1<i2<...<ik, k>1): we do a left “circular shift” of A[i1], A[i2], …, A[ik].

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, 1, 2}. 

输入
There will be only one test case, beginning with two integers n, q (1<=n<=100,000, 1<=q<=120,000), the number of integers in array A, and the number of operations. The next line contains n positive integers not greater than 100,000, the initial elements in array A. Each of the next q lines contains an operation. Each operation is formatted as a string having no more than 30 characters, with no space characters inside. All operations are guaranteed to be valid. Warning: The dataset is large, better to use faster I/O methods.
输出
For each query, print the minimum value (rather than index) in the requested range.
样例输入
7 5
6 2 4 8 5 1 4
query(3,7)
shift(2,4,5,7)
query(1,4)
shift(1,2)
query(2,2) 
样例输出
1
4
6 
来源
湖南省第七届大学生计算机程序设计竞赛
题目大意:有个shift操作,也就是常见的更新,只是这里的更新比较别致,是交换i和i+1,i+1和i+2,i+2和i+3,etc(也有人理解为移动i、i+1、i+2...对应的数组中的值)。然后就是询问区间内的最小值。
解题思路:就是按题意一直更新就好了。

#include<bits/stdc++.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=110000;
const int INF=1e9;
int minv[maxn*4];
int a[maxn],cnt=0;
int b[5000];
char str[100];
void PushUP(int rt){
        minv[rt]=min(minv[rt*2],minv[rt*2+1]);
}
void build(int rt,int L,int R){
    if(L==R){
        scanf("%d",&minv[rt]);
        a[cnt++]=minv[rt];
        return ;
    }
    build(lson);
    build(rson);
    PushUP(rt);
}
int get_b(int st,int en){
    int cnt=0;
    int tm,tmp=0;
    for(int i=st;i<en;i++){
        if(str[i]>=‘0‘&&str[i]<=‘9‘){
           tm=str[i]-‘0‘;
           tmp*=10;
           tmp+=tm;
        }else{
            b[cnt++]=tmp;
            tmp=0;
        }
    }
    return cnt;
}
void exchange(int la,int ra){
    int tm=a[la];
    a[la]=a[ra];
    a[ra]=tm;
}
int query(int rt,int L,int R,int l_ran,int r_ran){
    if(l_ran<=L&&R<=r_ran){
        return minv[rt];
    }
    int ret_l=INF,ret_r=INF;
    if(l_ran<=mid){
        ret_l=query(lson,l_ran,r_ran);
    }
    if(r_ran>mid){
        ret_r=query(rson,l_ran,r_ran);
    }
    return min(ret_l,ret_r);
}
void update(int rt,int L,int R,int pos,int val){
    if(L==R){
        minv[rt]=val;
        return ;
    }
    if(pos<=mid){
        update(lson,pos,val);
    }else{
        update(rson,pos,val);
    }
    PushUP(rt);
}
void debug(){
 for(int i=1;i<16;i++)
    printf("%d %d\n",i,minv[i]);
}
int main(){
    int n,q,m,ans;
    while(scanf("%d%d",&n,&q)!=EOF){
        cnt=0;
        build(1,1,n);
        for(int i=0;i<q;i++){
            scanf("%s",&str);
            int len=strlen(str);
            m= get_b(6,len);
            if(str[0]==‘q‘){
                ans=query(1,1,n,b[0],b[1]);
                printf("%d\n",ans);
            }else{
                b[m]=b[0];
                for(int i=0;i<m;i++){   //估计瞌睡了,这里迷糊了好久
                    update(1,1,n,b[i],a[b[i+1]-1]);
                }
                for(int i=0;i<m-1;i++){
                    exchange(b[i]-1,b[i+1]-1);
                }
            }
        }
    }
    return 0;
}

  

时间: 2024-08-19 11:29:40

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

HDU 2795 Billboard (线段树 单点更新 区间求最大值)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一块h*w 的广告版,有n块1*w[i]的广告,就着放广告尽量在顶上,尽量先放左边的原则,问在第几行能把广告放下,如果放不下,就打印-1: 思路:我们可以根据每一行建树,每一个子叶表示每一行的容量,而节点存放子节点的最大值,然后从最顶到底,快速查找能存放下广告的一行. 总之还是简单的线段树问题,难点在于抽象模型. #include <iostream> #include <cs

线段树 --- (单点更新、求区间最值、模板题)

A - 敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营 地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工 兵营地的人数都有可能发生

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

hdu 3308 线段树单点更新 区间合并

http://acm.hdu.edu.cn/showproblem.php?pid=3308 学到两点: 1.以区间端点为开始/结束的最长......似乎在Dp也常用这种思想 2.分类的时候,明确标准逐层分类,思维格式: 条件一成立: { 条件二成立: { } else { } } else { 条件二成立: { } else { } } 上面的这种方式很清晰,如果直接想到那种情况iif(条件一 &条件二)就写,很容易出错而且把自己搞乱,或者情况不全,,,我就因为这WA了几次 3.WA了之后 ,

hdu2795(线段树单点更新&amp;区间最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要尽量将其靠上贴,并输出其最上能贴在哪个位置: 思路:可以将每行剩余空间大小存储到一个数组中,那么对于当前 1 * x 的广告,只需找到所有剩余空间大于的 x 的行中位置最小的即可: 不过本题数据量为 2e5,直接暴力因该会 tle.可以用个线段树维护一下区间最大值,然后查询时对线段树二分即可: 代码

hdu - 4973 - A simple simulation problem.(线段树单点更新 + 区间更新)

题意:初始序列 1, 2, ..., n,m次操作(1 <= n,m<= 50000),每次操作可为: D l r,将区间[l, r]中的所有数复制一次: Q l r,输出区间[l, r]中同一数字个数的最大值. (0 <= r – l <= 10^8, 1 <= l, r <= 序列元素个数) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4973 -->>因为区间内数字是依次递增的,所以可以以数字为叶建线段

HDU 3308 线段树单点更新+区间查找最长连续子序列

LCIS                                                              Time Limit: 6000/2000 MS (Java/Others)                                                                 Memory Limit: 65536/32768 K (Java/Others)                                       

hdu1394(枚举/树状数组/线段树单点更新&amp;区间求和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 或 -1, 交换两个不相邻数 a, b, 逆序数 += 两者间大于 a 的个数 - 两者间小于 a 的个数: 所以只要求出初始时的逆序对数,就可以推出其余情况时的逆序对数.对于求初始逆序对数,这里 n 只有 5e3,可以直接暴力 / 树状数组 / 线段树 / 归并排序: 代码: 1.直接暴力 1

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)

Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every vill