Lazy Propagating 52C - Circular RMQ 标准模板题

C. Circular RMQ
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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), aiare 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).

Examples
input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
output
1
0
0

分析: 标准模板题,注意,当n=4时,当区间为[3,1]时,要拆成 [3,4)  [0,2)

       还有就是读取操作时如何判断读两个数据还是读三个数据。 我的方法是读第二个数据后的字符,如果是空格,就继续读第三个数据。


线段树的区间更新和区间查询用到了lazy数组。具体的思想是,对于每个结点,先判断当前结点对应的lazy数组中的值是否为0,若为0说明该值待更新。若不为0说明之前的操作更新过该区间。

若当前区间被所查询的区间完全包含,则更新该节点segtree的值,然后将lazy对应的两个孩子的值都加上delta。若该结点为叶结点,则不用将孩子加上delta。

手动实现一下更容易理解。不能懒!




#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
typedef long long ll;
const int maxn = 200000+10;
ll segtree[maxn*8],lazy[maxn*8];
int num[maxn*4];
int n,m;
void build(int l, int r, int pos){
    if(r-l == 1) {
        segtree[pos] = num[l];
        return;
    }
    int mid = (l+r)/ 2;
    build(l,mid,pos*2+1);
    build(mid,r,pos*2+2);
    segtree[pos] = min(segtree[pos*2+1],segtree[pos*2+2]);
}
ll queryrange(int ql,int qr,int l,int r,int pos){
    if(lazy[pos] != 0){
        segtree[pos] += lazy[pos];
        if(r - l != 1){
            lazy[pos*2+1] += lazy[pos];
            lazy[pos*2+2] += lazy[pos];
        }
        lazy[pos] = 0;
    }
    if(ql <= l && qr >= r){
        return segtree[pos];
    }
    if(ql >= r || qr <= l){
        return INF;
    }
    int mid = (l+r) / 2;
    return min(queryrange(ql,qr,l,mid,pos*2+1),queryrange(ql,qr,mid,r,pos*2+2));
    
}
void updaterange(int ul,int ur,int l,int r,int pos,int delta){
    if(lazy[pos] != 0){
        segtree[pos] += lazy[pos];
        if(r - l != 1){
            lazy[pos*2+1] += lazy[pos];
            lazy[pos*2+2] += lazy[pos];
        }
        lazy[pos] = 0;
    }
    if(ul <= l && ur >= r){
        segtree[pos] += delta;
        if(r-l != 1){
            lazy[pos*2+1] += delta;
            lazy[pos*2+2] += delta;
        }
        return;
    }
    if(ul >= r || ur <= l) return;
    
    int mid = (l+r) / 2;
    updaterange(ul,ur,l,mid,pos*2+1,delta);
    updaterange(ul,ur,mid,r,pos*2+2,delta);
    segtree[pos] = min(segtree[pos*2+1],segtree[pos*2+2]);
}
int main()
{
    while(scanf("%d",&n) != EOF){
        int k = 1;
        while(k<n) k <<= 1;
        memset(num,0x3f,sizeof(num));
        memset(lazy,0,sizeof(lazy));
        for(int i=0;i<n;i++){
            scanf("%d",&num[i]);
        } 
        build(0,k,0);
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            char a;
            int ql,qr,delta = INF;
            scanf("%d%d",&ql,&qr);
            qr++;
            a = getchar();
            if(a == ' ') scanf("%d",&delta);
            if(delta == INF){
                if(ql >= qr){
                    cout << min(queryrange(ql,k,0,k,0),queryrange(0,qr,0,k,0)) << endl;
                }
                else
                   cout << queryrange(ql,qr,0,k,0) << endl;
            }
            else{
                if(ql >= qr){
                    updaterange(ql,k,0,k,0,delta);
                    updaterange(0,qr,0,k,0,delta);
                }
                else
                    updaterange(ql,qr,0,k,0,delta);
            }
        }
    }
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值