线段树 Circular RMQ

来源:互联网 发布:域名被墙做跳转有用吗 编辑:程序博客网 时间:2024/05/28 17:06
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).

Example
Input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
Output
1
0

0

题意:环形数列n=5时,a1,a2,a3,a4,a5,//把数列想想成一个圆圈,而不是想象成一条直线,[5,2]就是a5,a1,a2]三个数

剩下的就是线段树的查询,成段更新,最好把线段树封装成一个结构体,更好操作。

下面给出我的代码,大佬不喜勿喷

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define e tree[id]
#define lson tree[id<<1]
#define rson tree[id<<1|1]
#define N 200005
LL a[N];


struct node
{
    LL d,v;
    int le,ri;
}tree[N<<2];


LL min(LL a,LL b)
{
    return a<b?a:b;
}


void  pushup(int id)//结点赋值
{
    e.v=min(lson.v,rson.v);
}


void pushdown(int id)
{
    if(e.d!=0&&e.le!=e.ri)//不是叶子结点,更新
    {
        lson.d+=e.d;lson.v+=e.d;
        rson.d+=e.d;rson.v+=e.d;
        e.d=0;
    }
}


void Tree(int id,int le,int ri,LL d)//建树
{
    e.le=le;e.ri=ri;e.d=d;
    if(le==ri) {e.v=a[le];return;}
    int mid=(le+ri)/2;
    Tree(id<<1,le,mid,d);
    Tree(id<<1|1,mid+1,ri,d);
    pushup(id);
}


LL Query(int id,int x,int y)//查询
{
     int le=e.le,ri=e.ri;
     if(x<=le&&ri<=y) {return e.v;}
     pushdown(id);
     int mid=(le+ri)/2;
     LL ret=INF;
     if(x<=mid) ret=min(ret,Query(id<<1,x,y));
     if(y>mid)  ret=min(ret,Query(id<<1|1,x,y));
     return ret;
}


void update(int id,int x,int y,LL d)
{
    int le=e.le,ri=e.ri;
    if(x<=le&&ri<=y){e.v+=d;e.d+=d;return;}
    pushdown(id);
//延迟更新,只更新到小区间的值,下次更新时如果缩小范围,就会进一步向儿子节点更新上一次的值
    int mid=(le+ri)/2;
    if(x<=mid) update(id<<1,x,y,d);
    if(y>mid)  update(id<<1|1,x,y,d);
    pushup(id);
}


int main()
{
    int n,m,lf,rg;
    LL v;
    char str[30];
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        memset(str,0,sizeof(str));
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        Tree(1,1,n,0);
        scanf("%d",&m);
        getchar();
        while(m--)
        {
           gets(str);
           if(sscanf(str,"%d%d%I64d",&lf,&rg,&v)==2)
           {
             if(lf>rg)
             {
                 LL ret=min(Query(1,1,rg+1),Query(1,lf+1,n));
                 printf("%I64d\n",ret);
             }
             else
             {
                 printf("%I64d\n",Query(1,lf+1,rg+1));
             }
           }
           else
           {
              if(lf>rg)
              {
                  update(1,1,rg+1,v);
                  update(1,lf+1,n,v);
              }
              else
              {
                  update(1,lf+1,rg+1,v);
              }
           }
        }
    }
}