ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线段树区间更新之 Circular RMQ

2022-07-15 13:31:24  阅读:160  来源: 互联网

标签:lf rt RMQ int 线段 rg operation root Circular


Description

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 Input

Input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
Output
1
0
0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

typedef long long ll;

#define maxn 800080
#define MAX 0x3f3f3f3f

ll f[maxn];
ll lazy[maxn];

void Pushup(int root)
{
    int rt=root<<1;
    f[root]=min(f[rt],f[rt+1]);
}

void build(int left,int right,int root)
{
    lazy[root]=0;
    if(left==right)
    {
        scanf("%I64d",&f[root]);
        return ;
    }
    
    int rt=root<<1;
    int mid=(left+right)>> 1;
    
    build(left,mid,rt);
    build(mid+1,right,rt+1);
    
    Pushup(root);
}

void Pushdown(int root)
{
    int rt=root<<1;
    
    if(lazy[root])
    {
        lazy[rt]+=lazy[root];
        lazy[rt+1]+=lazy[root];
        
        f[rt]+=lazy[root];
        f[rt+1]+=lazy[root];
        
        lazy[root]=0;
    }
}

void Update(int uleft,int uright,ll c,int left,int right,int root)
{
    if(uleft<=left&&right<=uright)
    {
        lazy[root]+=c;
        f[root]+=c;
        return ;
    }
    
    Pushdown(root);
    
    int rt=root<<1;
    int mid=(left+right)>> 1;
    
    if(uleft<=mid)
    {
        Update(uleft,uright,c,left,mid,rt);
    }
    if(uright>mid)
    {
        Update(uleft,uright,c,mid+1,right,rt+1);
    } 
    Pushup(root);
}

ll Query(int qleft,int qright,int left,int right,int root)
{
    ll  ans=MAX;
    
    if(qleft<=left&&right<=qright)
    {
        return f[root];
    }
       
    Pushdown(root);
    
    int mid=(left+right)>>1;
    int rt=root<<1;
    
    if(qleft<=mid)
    {
        ans=min(ans,Query(qleft,qright,left,mid,rt));
    }
     
    if(qright>mid)
    {
        ans=min(ans,Query(qleft,qright,mid+1,right,rt+1));
    } 
    return ans;
}

int main()
{
    int n,m,a,b;
    ll c;
    char ch;
    
    scanf("%d",&n);
    build(1,n,1);
    scanf("%d",&m);
    
    while(m--)
    {
        scanf("%d%d%c",&a,&b,&ch);
        
        if(ch==' ')
        {
            scanf("%I64d",&c);
            
            if(a>b)
            {
                Update(1,b+1,c,1,n,1);
                Update(a+1,n,c,1,n,1);
            }
            else{
                Update(a+1,b+1,c,1,n,1);
            }
            
        }
        else
        {
            if(a>b)
            {
                printf("%I64d\n",min(Query(1,b+1,1,n,1),Query(a+1,n,1,n,1)));
            }
            else
            {
                printf("%I64d\n",Query(a+1,b+1,1,n,1));
            }
                
        }
    }
    return 0;
}

 

标签:lf,rt,RMQ,int,线段,rg,operation,root,Circular
来源: https://www.cnblogs.com/killjoyskr/p/16481073.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有