Circular RMQ

You are given circular array a0, a1, ..., an - 1. There are two types of operations with it:

  • inc(lf, rg, v) — this operation increases each element on the segment [lf, rg] (inclusively) by v;
  • rmq(lf, rg) — this operation returns minimal value on the segment [lf, rg] (inclusively).

Assume segments to be circular, so if n = 5 and lf = 3, rg = 1, it means the index sequence: 3, 4, 0, 1.

Write program to process given sequence of operations.

Input

The first line contains integer n (1 ≤ n ≤ 200000). The next line contains initial state of the array: a0, a1, ..., an - 1 ( - 106 ≤ ai ≤ 106), ai are integer. The third line contains integer m (0 ≤ m ≤ 200000), m — the number of operartons. Next m lines contain one operation each. If line contains two integer lf, rg (0 ≤ lf, rg ≤ n - 1) it means rmq operation, it contains three integers lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106) — inc operation.

Output

For each rmq operation write result for it. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Sample 1

InputOutput
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
1
0
0

思路:

树形结构。

数据较大时可以使用位运算。

以及注意命令的判断。

#include<iostream>
using namespace std;
typedef long long LL;
const LL INF = 0x3fffffff;
struct ss
{
    LL v,tag;
}a[200000 << 2];
LL Min(LL a,LL b)
{
    return a < b ? a:b;
}

void pushup(int cur)
{
    a[cur].v = Min(a[cur<<1].v,a[cur<<1|1].v);
}

void built(int l,int r,int cur)
{
    a[cur].v = 0;
    a[cur].tag = 0;
    if(l == r)
    {
        scanf("%I64d", &a[cur].v);
        return ;
    }
    int mid = (l + r) >> 1;
    built(l, mid,cur << 1);
    built(mid + 1,r,cur << 1|1);
    pushup(cur);
}

void pushdown(int cur)
{
    if(a[cur].tag != 0)
    {
        a[cur<<1].tag += a[cur].tag;
        a[cur<<1|1].tag += a[cur].tag;
        a[cur<<1].v += a[cur].tag;
        a[cur<<1|1].v += a[cur].tag;
        a[cur].tag = 0;
    }
}

void update(int l,int r,int ll,int rr,int cur,LL v)
{
    if(l<=ll&&r>=rr)
    {
        a[cur].v += v;
       a[cur].tag += v;
        return ;
    }
    pushdown(cur);
    int mid = (ll +rr)>>1;
    if(mid < r) update(l,r,mid + 1,rr,cur<<1|1,v);
    if(mid >= l) update(l,r,ll,mid,cur<<1,v);
    pushup(cur);
}

LL quiry(int l,int r,int ll,int rr,int cur)
{
    if(l<=ll&&r>=rr)
    {
        return a[cur].v;
    } 
    pushdown(cur);
    int mid = (ll + rr) >> 1;
    LL t1 =INF;
    LL t2 = INF;
    if(mid < r) t1 = quiry(l,r,mid + 1,rr,cur<<1|1);
    if(mid >= l) t2 =  quiry(l,r,ll,mid,cur<<1);
    return Min(t1,t2);
}

int main()
{
    int n;
    scanf("%d", &n);
    built(1,n,1);
    int m;
    scanf("%d", &m);
    while(m--)
    {
        int a,b,c;
        char ch;
        scanf("%d%d%c", &a, &b, &ch);
        a++,b++;
    if(ch != ' ')
    {
        if(a>b)
        {
            LL t1=quiry(a,n,1,n,1);
            LL t2 = quiry(1,b,1,n,1);
            printf("%I64d\n",Min(t1,t2));
        }
        else
            printf("%I64d\n",quiry(a,b,1,n,1));
      }
    else 
    {
        scanf("%d", &c);
        if(a>b)
        {
            update(a,n,1,n,1,c);
            update(1,b,1,n,1,c);
        }
        else update(a,b,1,n,1,c);
      }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TherAndI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值