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

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



这题是个裸的线段树题,用到区间更新和查询。

比赛时拿到题一看就知道方法,然后开始写,因为平时没怎么写线段树的题,结果写完后全是bug,调了好久,所以还是得多练,尤其像数据结构这类题,尽量写的时候不要写错,不然找bug真心难。。。附上ac代码(如果线段树查询更新部分看不懂可以去看我的“线段树入门”,自认为写的很详细啦,认真一点就能看懂)~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3fffffff
using namespace std;
int n,m;
long long int a[200005];//这题需要用long long,注意一下
struct node
{
    int l,r;
    long long int val;
    int tag;//区间要更改的值
}segtree[200005*4];
void pushup(int root)
{
    segtree[root].val=min(segtree[root<<1].val,segtree[root<<1|1].val);
}
void build(int root,int l,int r)
{
    segtree[root].l=l;
    segtree[root].r=r;
    segtree[root].tag=0;
    if(l==r)
    {
        segtree[root].val=a[l];
        return;
    }
    int mid=(segtree[root].l+segtree[root].r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    pushup(root);
}
void pushdown(int root)
{
    if(segtree[root].tag!=0)
    {
        segtree[root<<1].val+=segtree[root].tag;
        segtree[root<<1|1].val+=segtree[root].tag;
        segtree[root<<1].tag+=segtree[root].tag;
        segtree[root<<1|1].tag+=segtree[root].tag;
        segtree[root].tag=0;
        return;
    }
}
long long int query(int root,int l,int r)
{
    int ll=segtree[root].l;
    int rr=segtree[root].r;
    if(l<=ll&&rr<=r)
    {
        return segtree[root].val;
    }
    pushdown(root);
    int mid=(ll+rr)>>1;
    long long int ans1=inf,ans2=inf;
    if(l<=mid)
        ans1=min(ans1,query(root<<1,l,r));
    if(r>mid)
        ans1=min(ans1,query(root<<1|1,l,r));
    return min(ans1,ans2);
}
void update(int root,int l,int r,int loop)
{
    int ll=segtree[root].l;
    int rr=segtree[root].r;
    if(l<=ll&&rr<=r)
    {
        segtree[root].val+=loop;
        segtree[root].tag+=loop;
        return;
    }
    pushdown(root);
    int mid=(ll+rr)>>1;
    if(l<=mid)
        update(root<<1,l,r,loop);
    if(r>mid)
        update(root<<1|1,l,r,loop);
    pushup(root);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        build(1,1,n);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            long long int a,b,c;
            char ch;
            scanf("%lld%lld",&a,&b);//这题输入很关键,需要注意一下
            ch=getchar();
            if(ch!=' ')
            {
                long long int ans=inf;
                if(b<a)//如果b比a小的话,就分两段去查询,因为我习惯端点从1开始,所以都加上了1
                {
                    ans=query(1,1,b+1);
                    ans=min(ans,query(1,a+1,n));
                }
                else
                    ans=query(1,a+1,b+1);
                printf("%I64d\n",ans);
            }
            else
            {
                scanf("%lld",&c);
                if(b<a)//和查询一样,这里分两段去更新
                {
                    update(1,1,b+1,c);
                    update(1,a+1,n,c);
                }
                else
                    update(1,a+1,b+1,c);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值