POJ 3468A Simple Problem with Integers(线段树)

本文介绍了一种使用线段树解决区间更新与查询问题的方法。具体包括如何构建线段树、进行区间加法操作及求解区间和。通过样例输入输出展示了算法的有效性。
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 111222 Accepted: 34630
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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


想法:

Q   a b表示询问a到b区间和

C  a b c表示a到b区间都加c

线段树模板

代码:

 #include<iostream>
 #include<cstdio>
 using namespace std;
 typedef long long ll;
 const int N=1e5+10;
 #define lson  l,m,rt<<1
 #define rson  m+1,r,rt<<1|1
 ll sum[N<<2];
 ll add[N<<2];
 void pushup(int rt)
 {
     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 }
 void pushdown(int rt,int m)
 {
     if(add[rt])
     {
         add[rt<<1]+=add[rt];
         add[rt<<1|1]+=add[rt];
         sum[rt<<1]+=add[rt]*(m-(m>>1));
         sum[rt<<1|1]+=add[rt]*(m>>1);
         add[rt]=0;
     }
 }
 void build(int l,int r,int rt)
 {
     add[rt]=0;
     if(l==r)
     {
         scanf("%lld",&sum[rt]);
         return ;
     }
     int m=(l+r)>>1;
     build(lson);
     build(rson);
     pushup(rt);
 }
 void update(int L,int R,int c,int l,int r,int rt)
 {
     if(L<=l&&R>=r)
     {
         add[rt]+=c;
         sum[rt]+=(ll)c*(r-l+1);
         return ;
     }
     pushdown(rt,r-l+1);
     int m=(l+r)>>1;
     if(L<=m) update(L,R,c,lson);
     if(R>m) update(L,R,c,rson);
     pushup(rt);
 }
 ll query(int L,int R,int l,int r,int rt)
 {
     if(L<=l&&R>=r)
        return sum[rt];
    pushdown(rt,r-l+1);
     int m=(r+l)>>1;
     ll res=0;
     if(L<=m) res+=query(L,R,lson);
     if(R>m) res+=query(L,R,rson);
    return  res;
 }
 int main()
 {
     int n,m;
    while(~scanf("%d%d",&n,&m))
     {
         build(1,n,1);
         while(m--)
         {
             char ch[2];
            int a,b,c;
             scanf("%s",ch);
             if(ch[0]=='Q')
             {
                 scanf("%d%d",&a,&b);
                printf("%lld\n",query(a,b,1,n,1));
             }
             else
             {
               scanf("%d%d%d",&a,&b,&c);
               update(a,b,c,1,n,1);
             }
         }
     }
     return 0;
 }

【Koopman】遍历论、动态模态分解和库普曼算子谱特性的计算研究(Matlab代码实现)内容概要:本文围绕【Koopman】遍历论、动态模态分解和库普曼算子谱特性的计算研究展开,重点介绍基于Matlab的代码实现方法。文章系统阐述了遍历理论的基本概念、动态模态分解(DMD)的数学原理及其与库普曼算子谱特性之间的内在联系,展示了如何通过数值计算手段分析非线性动力系统的演化行为。文中提供了完整的Matlab代码示例,涵盖数据驱动的模态分解、谱分析及可视化过程,帮助读者理解并复现相关算法。同时,文档还列举了多个相关的科研方向和技术应用场景,体现出该方法在复杂系统建模与分析中的广泛适用性。; 适合人群:具备一定动力系统、线性代数与数值分析基础,熟悉Matlab编程,从事控制理论、流体力学、信号处理或数据驱动建模等领域研究的研究生、博士生及科研人员。; 使用场景及目标:①深入理解库普曼算子理论及其在非线性系统分析中的应用;②掌握动态模态分解(DMD)算法的实现与优化;③应用于流体动力学、气候建模、生物系统、电力系统等领域的时空模态提取与预测;④支撑高水平论文复现与科研项目开发。; 阅读建议:建议读者结合Matlab代码逐段调试运行,对照理论推导加深理解;推荐参考文中提及的相关研究方向拓展应用场景;鼓励在实际数据上验证算法性能,并尝试改进与扩展算法功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值