CodeForces 52C Circular RMQ (区间更新线段树)

49 篇文章 0 订阅
11 篇文章 0 订阅

C - Circular RMQ

Time Limit:3000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

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

题意:两种操作,[l, r] + v min([l, r]); [l, r] 是循环区间

例如:n = 5 l = 3, r = 1, [l, r]区间就是 3, 4, 0, 1.


线段树,又是照大白写的,但是感觉是大白有错,上次也是,后来看了下网上的线段树完全版 至此是明白了


不过刚发现lld 和 I64d 都A了


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cstdlib>

using namespace std;

#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;

const int maxn = 200000 + 20;
const int maxo = maxn*4;
int n;
int A[maxn];
LL minv[maxo];
LL addv[maxo];

void pushUp(int o) {
    minv[o] = min(minv[o*2], minv[o*2+1]);
}

void pushDown(int o) {
    if(addv[o]) {
        int lc = o*2;
        int rc = o*2+1;
        addv[lc] += addv[o];
        minv[lc] += addv[o];
        addv[rc] += addv[o];
        minv[rc] += addv[o];
        addv[o] = 0;
    }
}

void build(int o, int L, int R) {
    addv[o] = 0;
    if(L < 1 || R > n) return ;
    if(L == R) {
        minv[o] = A[L];
        return ;
    }
    int mid = L + (R-L)/2;
    build(o*2, L, mid);
    build(o*2+1, mid+1, R);
    pushUp(o);
}

int y1, y2;
LL v;
void update(int o, int L, int R) {
    int lc = o*2;
    int rc = o*2+1;
    if(y1 <= L && R <= y2) {
        addv[o] += v;
        minv[o] += v;
        return ;
    }
    pushDown(o);
    int mid = L + (R-L)/2;
    if(y1 <= mid) update(lc, L, mid);
    if(y2 > mid) update(rc, mid+1, R);
    pushUp(o);
}

LL query(int o, int L, int R) {
    if(y1 <= L && R <= y2) {
        return minv[o];
    }
    pushDown(o);
    int mid = L + (R-L)/2;
    LL res = INF;
    if(y1 <= mid) res = query(o*2, L, mid);
    if(y2 > mid) res = min(res, query(o*2+1, mid+1, R));
    return res;
}

int main() {

    while(scanf("%d", &n) != EOF) {
        //memset(addv, 0, sizeof(addv));
        for(int i=1; i<=n; i++) scanf("%d", &A[i]);
        build(1, 1, n);
        int m;
        scanf("%d", &m);
        getchar();
        while(m--) {
            char s[100];
            gets(s);
            int tv;
            int type = sscanf(s, "%d%d%d", &y1, &y2, &tv);
            y1++;
            y2++;
            v = tv;
            if(type == 2) {
                LL _min = INF;
                if(y1 <= y2) {
                    _min = query(1, 1, n);
                } else {
                    int t = y1;
                    y1 = 1;
                    _min = query(1, 1, n);
                    y1 = t;
                    y2 = n;
                    _min = min(_min, query(1, 1, n));
                }
                P64I(_min);
            } else if(type == 3) {
                if(y1 <= y2) {
                    update(1, 1, n);
                } else {
                    int t = y1;
                    y1 = 1;
                    update(1, 1, n);
                    y1 = t;
                    y2 = n;
                    update(1, 1, n);
                }
            }
        }
    }

    return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值