[JZOJ5863] 【NOIP2018模拟9.11】移动光标

Description
Input
Output
Sample Input
4
3
2
4
3
3
1 1 3 2
3 3 4 2
1 3 3 4
Sample Output
3
2
5
Data Constraint

 

 


 

 

感觉这题和在纪中时候打的一场CF的A题特别像。。。

其实就是维护区间最小值。然后画画图就行了。

 


 

 

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define reg register
inline char gc() {
    static const int BS = 1 << 22;
    static unsigned char buf[BS], *st, *ed;
    if (st == ed) ed = buf + fread(st = buf, 1, BS, stdin);
    return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
    int res = 0;char ch=gc();bool fu=0;
    while(!isdigit(ch))fu|=(ch=='-'),ch=gc();
    while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=gc();
    return fu?-res:res;
}

int n, L[100005];
int st[100005*20][21];

inline int query(int l, int r)
{
    int k = log(r - l + 1) / log(2);
    return min(st[l][k], st[r - (1 << k) + 1][k]);
}

int main()
{
//    freopen("cusor.in", "r", stdin);
//    freopen("cusor.out", "w", stdout);
    n = read();
    for (reg int i = 1 ; i <= n ; i ++) L[i] = read();
    for (reg int i = 1 ; i <= n ; i ++) st[i][0] = L[i];
    for (reg int j = 1 ; j <= 20 ; j ++)
        for (reg int i = 1 ; i <= n - (1 << j) + 1 ; i ++)
            st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
    int q = read();
    while(q--)
    {
        int x1 = read(), y1 = read(), x2 = read(), y2 = read();
        if (x1 > x2) swap(x1, x2), swap(y1, y2);
        int mn = query(x1, x2);
        int ans = abs(x2 - x1);
        if (mn <= y1 and mn <= y2) ans += abs(y1 - mn) + abs(y2 - mn);
        else ans += abs(y1 - y2);
        printf("%d\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/BriMon/p/9735651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值