其他分享
首页 > 其他分享> > 线段树区间更新之 Circular RMQ

线段树区间更新之 Circular RMQ

作者:互联网

Description

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

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