CodeForces 52C Circular RMQ (线段树的区间更新+lazy tag)

本文详细介绍了一种高效的数据结构——线段树,并通过一道实战题目进行应用展示。文章不仅讲解了线段树的基本概念、建树过程及更新查询操作,还提供了完整的代码实现,帮助读者理解并掌握线段树在解决复杂区间操作问题上的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 mlines 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

题解:

现在还在比赛。。但我已经写不出来题了,就来写博客了,好高兴线段树的题没看题解就写出来了,这几天线段树和树状数组的题没白刷,这题就是如果输入区间不是从小到大就分成两个区间做,然后要注意一下区间更新要lazy tag,延迟更新,然后输入的时候做点判断处理这题就ac了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
struct node
{
    int l,r;
    long long minn;
    long long tag;
};
node t[200005*4];
int a[200005];
void Build(int l,int r,int num)//日常建树
{
    t[num].l=l;
    t[num].r=r;
    t[num].tag=0;
    if(l==r)
    {
        t[num].minn=a[l];
        return;
    }
    int mid=(l+r)/2;
    Build(l,mid,num*2);
    Build(mid+1,r,num*2+1);
    t[num].minn=min(t[num*2].minn,t[num*2+1].minn);//更新下最小值
}
void update(int l,int r,int num,int v)
{
    if(t[num].l==l&&t[num].r==r)
    {
        t[num].minn+=v;
        t[num].tag+=v;
        return;
    }
    if(t[num].tag!=0)//除了这个都是日常更新,这个可以写成一个函数lazy tag,将上次没有更新的东西向下子节点更新
    {
        t[num*2].minn+=t[num].tag;
        t[num*2+1].minn+=t[num].tag;
        t[num*2].tag+=t[num].tag;
        t[num*2+1].tag+=t[num].tag;
        t[num].tag=0;//清除状态
    }
    int mid=(t[num].l+t[num].r)/2;
    if(r<=mid)
        update(l,r,num*2,v);
    else if(l>mid)
        update(l,r,num*2+1,v);
    else
    {
        update(l,mid,num*2,v);
        update(mid+1,r,num*2+1,v);
    }
    t[num].minn=min(t[num*2].minn,t[num*2+1].minn);//更新最小值
}
long long query(int l,int r,int num)//访问
{
    if(t[num].l==l&&t[num].r==r)
    {
        return t[num].minn;
    }
      if(t[num].tag!=0)//和上面一样,pushdown
    {
        t[num*2].minn+=t[num].tag;
        t[num*2+1].minn+=t[num].tag;
        t[num*2].tag+=t[num].tag;
        t[num*2+1].tag+=t[num].tag;
        t[num].tag=0;
    }
    int mid=(t[num].l+t[num].r)/2;
    if(r<=mid)
        return query(l,r,num*2);
    else if(l>mid)
    {
        return query(l,r,num*2+1);
    }
    else
        return min(query(l,mid,num*2),query(mid+1,r,num*2+1));
}
int main()
{
    int i,j,n,m,x,y,z,tag,flag;
    char s[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    Build(0,n-1,1);
    scanf("%d",&m);
    getchar();//吸收回车
    for(i=0;i<m;i++)
    {
        tag=0;
        gets(s);
        x=0,y=0,z=0;
        flag=0;
        for(j=0;j<strlen(s);j++)
        {
            if(s[j]==' ')//根据空格数目判断情况,储存数据
            {
                tag++;
                continue;
            }
            if(tag==0)
            {
                x=x*10+s[j]-'0';
            }
            else if(tag==1)
            {
                y=y*10+s[j]-'0';
            }
            else
            {
                if(s[j]=='-')
                    flag=1;
                else
                {
                    z=z*10+s[j]-'0';
                }
            }
        }
        if(flag)//z处有负号
            z=-z;
        if(tag==1)
        {
            if(x<=y)
                printf("%I64d\n",query(x,y,1));//codeforce要用i64d
            else
                printf("%I64d\n",min(query(0,y,1),query(x,n-1,1)));//如果输入的y大于x,就分成两个部分
        }
        else
        {
            if(x<=y)
                update(x,y,1,z);
            else
            {
                update(0,y,1,z);//同理分成两个部分
                update(x,n-1,1,z);
            }
        }
    }
    return 0;
}




当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值