bzoj 3212: Pku3468 A Simple Problem with Integers 线段树

C语言新手必练:10个实战小项目带你快速上手(附完整代码) 本文为C语言初学者精选10个实战项目,从基础的温度转换器到高级的迷宫求解算法,涵盖输入输出、条件判断、循环控制、数组操作等核心知识点。每个项目配有完整代码和详细解析,帮助新手通过实践掌握编程思维,特别适合刚学完语法但缺乏实战经验的学习者。 阅读详情

                                          bzoj 3212 线段树模板题

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3212


Description

 
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input


The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output


You need to answer all Q commands in order. One answer in a line.

 

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

HINT

The sums may exceed the range of 32-bit integers. 

这是bzoj中难得的模板题~~~~~

#include<cstdio>
#include<iostream>
#define MAXN 800010

using namespace std;

char op[10];int n,m;
struct Segtree{
  #define lc(x) x<<1
  #define rc(x) ((x<<1)|1)
  int l[MAXN],r[MAXN];
  long long add[MAXN],s[MAXN];
  void update(int x)
  {
    s[x]=s[lc(x)]+s[rc(x)];

  }
  void push(int x)
  {
     s[lc(x)]+=add[x]*(r[lc(x)]-l[lc(x)]+1);
     s[rc(x)]+=add[x]*(r[rc(x)]-l[rc(x)]+1);
     add[lc(x)]+=add[x];
     add[rc(x)]+=add[x];
     add[x]=0;
  }
  long long  query(int x,int L,int R)
   {
       push(x);
      if(l[x]>R||r[x]<L)
        return 0;
      if(l[x]>=L&&r[x]<=R)
        return s[x];
      return query(lc(x),L,R)+query(rc(x),L,R);
   }
   void change(int x,int L,int R,int v)
   {
      push(x);
     if(l[x]>R||r[x]<L)
        return ;
     if(l[x]>=L&&r[x]<=R)
        {
            s[x]+=v*(r[x]-l[x]+1);
            add[x]=v;
            return ;
        }
        change(lc(x),L,R,v);
        change(rc(x),L,R,v);
        update(x);
   }
   void build(int x,int L,int R)
   {
       l[x]=L,r[x]=R,add[x]=0;
       if(L==R)
       {
           scanf("%lld",&s[x]);
           return ;
       }
       int mid=(L+R)/2;
       build(lc(x),L,mid);
       build(rc(x),mid+1,R);
       update(x);
   }
}seg;
int main()
{
   scanf("%d%d",&n,&m);
   seg.build(1,1,n);
   while(m--)
   {
      scanf("%s",op);
      if(op[0]=='C')
      {
          int l,r;long long  v;
          scanf("%d%d%lld",&l,&r,&v);
          seg.change(1,l,r,v);
      }
      else
      {
         int l,r;
         scanf("%d%d",&l,&r);
         printf("%lld\n",seg.query(1,l,r));
      }
   }
   return 0;
}


STM32F407+LWIP实战:手把手教你移植mbedtls库实现Modbus TCP双向认证 本文详细介绍了如何在STM32F407微控制器上移植mbedtls库,结合LWIP协议栈实现Modbus TCP的双向认证功能。通过TLS 1.2加密通道,有效提升工业物联网通信的数据安全性,涵盖证书管理、内存优化及性能调优等关键步骤,为嵌入式系统开发者提供了一套完整的解决方案。 阅读详情

相关推荐

豆包大模型API调用两种方法

ps: 因为是自用,所以这里图方便采用硬编码密钥的形式;如果有需求/安全风险,建议参考官方教程设置ARK_API_KEY环境变量的方式。坏处是:看别人的教程走了一大圈弯路hh;基础调用的话方法一就可以满足了,也比较方便快捷,容易上手。将 <Model> 替换为 Endpoint ID即可。创建就按照提示填写信息就行,需要实名认证,有免费额度。绿框框里就是Endpoint ID。

kkkkkkkkkasey的博客 1万+

BZOJ 3212: Pku3468 A Simple Problem with Integers【树状数组||线段树】

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Description You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of o...

XSamsara的博客 AFO 273

霍尔式流量计背后的技术和算法

例如,在一项实验中,当流量低于最小操作范围(0.250 L/min)时,误差比其他流量大,但仍然在95%的精度范围内可接受。例如,在Honeywell电子学的测试中,使用30英寸水柱的压力计和热电偶测量气体特性,记录并计算实际和理论质量流量,以确定修正系数。霍尔式流量计的校准系数是通过一系列精确的测量和计算步骤确定的,以确保其测量结果的准确性和可靠性。霍尔式流量计的校准系数是通过严格的环境条件、精确的测量步骤和实验数据确定的。其中,Qs是标尺介质的流量,K是修正系数,Qm是用户介质的流量。

百态·数智思维 | 聚焦大数据、量化与未来AI 1234

bzoj3212: Pku3468 A Simple Problem with Integers(线段树)

3212: Pku3468 A Simple Problem with Integers 题目:传送门 题解:    感谢Rose_max大佬的倾情相(推)助(水题)    一起打线段树啊~~~~ 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include...

weixin_30443895的博客 133

USB接口ESD防护设计实战:从金属外壳到信号线的完整避坑指南

本文深入探讨USB接口ESD防护设计的实战要点,聚焦金属外壳作为ESD能量泄放关键通道的处理策略。文章详细解析了从外壳接地路径控制、信号线TVS管选型与布局,到PCB分区及电源线滤波等完整设计流程,旨在帮助硬件工程师构建系统级防护,避免常见设计误区,确保产品通过严苛的静电测试。

k8l9m0n的博客 481

Linux 命令之 localectl -- 控制系统的本地化与键盘布局

文章目录命令介绍常用选项参考示例(一)显示当前的本地化设置与键盘映射(二)设置系统的本地化字符集环境变量(三)列出本地所有字符集(四)列出所有可用的控制台键盘映射(五)设置控制台的键盘映射 命令介绍 localectl 命令可用于查询与修改系统的本地化(locale)与键盘布局的设置。 它通过与 systemd-localed.service 通信来修改例如 /etc/locale.conf 与 /etc/vconsole.conf 之类的配置文件。本地化设置控制着 用户界面的语言、字符类型与字符编码、 日

liaowenxiong的博客 5396

[bzoj 3212]A Simple Problem with Integers

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations.One type of operation is to add some given number to each number in a given interval. The other is to ask for the ...

galiqing的博客 220

rk3399 linux 5.10 usb 2.0设备上电概率性注册失败

先排除硬件上的问题,然后就烧录kernel 4.1的的镜像,多次测试也没有发现这个问题,所以排除了硬件上的问题,然后。查下usb hub和usb 4G的复位脚,写驱动都给它们配上了,但是还是存在这个问题。最后通过对比rk3399.dtsi发现少了个power-domains = ;多次开关机,发现usb hub和4G都通信失败了,这就有点奇怪了,按理说usb驱动是没啥问题的。加上后,重新编译烧录,重新测试没有发现这个问题了!先查看usb log。

qq_32017483的博客 2672

BZOJ3212 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with IntegersTime Limit: 1 Sec Memory Limit: 128 MB Submit: 1277 Solved: 559 [Submit][Status][Discuss] DescriptionYou have N integers, A1, A2, … , AN. You need to de

DCrusher's blog 654

【Dify(v1.x) 核心源码深入解析】File 模块

文件模型(File Model)定义了文件的核心属性,如文件类型、传输方法、相关标识符等,是整个文件处理流程的数据基础。文件解析器(UploadFileParser)负责解析上传的文件,根据配置决定以何种方式(URL 或 Base64)提供图像数据。工具文件解析器(ToolFileParser)专注于工具文件的管理,提供工具文件管理器的获取接口。签名验证与生成(helpers.py)包含文件 URL 签名的生成与验证逻辑,确保文件访问的安全性。文件管理器(file_manager.py)

668

安卓刷Windows太难?这款手机平板双系统刷机工具,小白也能轻松拿捏!(小白福音!手机平板专用,一键搞定安卓刷 Windows 双系统!)

安卓刷Windows太难?这款手机平板双系统刷机工具,小白也能轻松拿捏!(小白福音!手机平板专用,一键搞定安卓刷 Windows 双系统!)

XiaoqiangClub的博客 4672

bzoj 3212 线段树

  裸的线段树 /************************************************************** Problem: 3212 User: BLADEVIL Language: Pascal Result: Accepted Time:100 ms Memory:19756 kb ***...

weixin_34302798的博客 127

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

bzoj 3212: Pku3468 A Simple Problem with Integers (线段树)

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1565  Solved: 676 [Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You

clover_hxy的博客 451

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

BZOJ 3212 Pku3468 A Simple Problem with Integers 线段树

Description   You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other

I'm Growing 355

BZOJ3212 Pku3468 A Simple Problem with Integers 题解

题目大意:   一个数列,有两个操作:1.修改操作,将一段区间内的数加上c;2.查询操作,查询一段区间内的数的和。 思路:   线段树裸题,区间修改、区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可。 代码: 1 #include<cstdio> 2 #include<cstring> 3 #in...

diyaochang9767的博客 139

BZOJ 3212【Pku 3468】A Simple Problem with Integers==线段树裸题

题目传送:23333 直接建树,区间修改可以打永久标记,查询直接查就可以了。 记得要开long long==我不知道不开会不会WA反正我开了 #include #include #include #include #define rep(j,k,l) for (long long j=k;j<=l;j++) using namespace std; long

YYMHL 292

【bzoj 3212】【POJ 3468】A Simple Problem with Integers(线段树)

寒乍起,孤寂伴,长立风中,愿为风散

reverie_mjp的博客 3363

BZOJ3212 Pku3468 A Simple Problem with Integers

线段树 题目传送门 全裸线段树 代码: #include&lt;cctype&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; #define N 100005 #define F inline using namespace std; typedef long long LL; s...

forezxl的博客 259

[BZOJ3212] Pku3468 A Simple Problem with Integers

传送门http://www.lydsy.com/JudgeOnline/problem.php?id=3212题目大意区间修改,区间查询和题解水题const maxn=100005; type aa=record l,r:longint; val,tag:int64; end; var w:array[0..8*maxn]of aa; i,j,k:longint; n,m:longi

slongle的专栏 447

BZOJ3212: Pku3468 A Simple Problem with Integers

【传送门:BZOJ3212】 简要题意:   给出一段序列,有两种操作:  1.Q l r求出l到r的和   2.C l r c将l到r的值加c 题解:   线段树水题   区间修改+区间询问,加个标记处理   注意加long long 参考代码: #include<cstdio> #include<cstdlib> #include&...

weixin_30278237的博客 80

bzoj 3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1954  Solved: 857 [Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You

Brian551 's Home 323

bzoj3212 Pku3468 A Simple Problem with Integers 线段树

3212: Pku3468 A Simple Problem with IntegersTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 Solved: 892[Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You need to dea...

weixin_30652879的博客 131
上一篇: bzoj 2005 [Noi2010]能量采集 数学
下一篇: bzoj 1406: [AHOI2007]密码箱 数学
DJS_K_D
博客等级 码龄12年 2粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值