意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c

Q a b 求[a,b]的和。

#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define ll __int64
#define maxn 100005
#define mod 1000000009 struct node
{
ll l,r,sum;
ll lazy;
}tree[maxn*10];
ll a[maxn];
void Pushup(ll rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void Pushdown(ll rt)
{
if(tree[rt].lazy!=0)
{
tree[rt<<1].lazy+=tree[rt].lazy;//注意是+=,不是=;
tree[rt<<1|1].lazy+=tree[rt].lazy;
tree[rt<<1].sum+=(tree[rt<<1].r-tree[rt<<1].l+1)*tree[rt].lazy;
tree[rt<<1|1].sum+=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*tree[rt].lazy;
tree[rt].lazy=0;
}
}
void build(ll l,ll r,ll rt)
{
tree[rt].l=l;tree[rt].r=r;
tree[rt].sum=0;tree[rt].lazy=0;
if(l==r)
{
tree[rt].sum=a[l];
return;
}
ll mid=(l+r)/2;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
Pushup(rt);
} void update(ll rt,ll l,ll r,ll v)
{
Pushdown(rt);
if(tree[rt].l==l&&tree[rt].r==r)
{
tree[rt].lazy=v;
tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*v;
return ;
} ll mid=(tree[rt].l+tree[rt].r)>>1;
if(mid<l)
update(rt<<1|1,l,r,v);
else if(mid>=r)
update(rt<<1,l,r,v);
else
{
update(rt<<1,l,mid,v);
update(rt<<1|1,mid+1,r,v);
}
Pushup(rt);
}
ll query(ll rt,ll l,ll r)
{
if(tree[rt].l==l&&tree[rt].r==r)
return tree[rt].sum;
Pushdown(rt);
ll mid=(tree[rt].l+tree[rt].r)>>1,ret=0;
if(mid<l)
ret+=query(rt<<1|1,l,r);
else if(mid>=r)
ret+=query(rt<<1,l,r);
else
{
ret+=query(rt<<1,l,mid);
ret+=query(rt<<1|1,mid+1,r);
}
Pushup(rt);
return ret;
}
int main()
{
ll n,m;
while(~scanf("%I64d%I64d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%I64d",&a[i]);
build(1,n,1);
char s[2];
ll u,v,c;
while(m--)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%I64d%I64d",&u,&v);
printf("%I64d\n",query(1,u,v));
}
else
{
scanf("%I64d%I64d%I64d",&u,&v,&c);
update(1,u,v,c);
}
}
}
return 0;
}
/*
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 10 22
1 2 3 4 5 6 7 8 9 10
Q 4 4
C 1 10 3
C 6 10 3
C 6 9 3
C 8 9 -100
C 7 9 3
C 7 10 3
C 1 10 3
Q 6 10
Q 6 9
Q 8 9
Q 7 9
Q 7 10
Q 1 10
Q 2 4
C 3 6 3
Q 9 9
Q 1 1
Q 5 5
Q 6 6
Q 7 7
Q 6 8
*/

版权声明:本文博客原创文章。博客,未经同意,不得转载。

PKU A Simple Problem with Integers (段树更新间隔总和)的更多相关文章

  1. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  4. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  5. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  6. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  7. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  8. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

随机推荐

  1. myeclipse10.7皴,出口解决war包时报“SECURITY ALERT: INTEGERITY CHECK ERROR”

    一.操作系统的环境是win7,64bit和32bit的操作系统我试过都OK 依照网上一些Crack破解程序步骤操作就能够完毕破解过程, 也能够到我的CSDN资源里下载文件包 myeclipse10.7 ...

  2. [置顶] ※数据结构※→☆线性表结构(list)☆============双向链表结构(list double)(三)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点. ~~~~~~~~~~~~ ...

  3. javaScript滚动新闻

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. Redis深入之数据结构

    Redis主要数据结构 链表 Redis使用的C语言并没有内置这样的数据结构,所以Redis构建了自己的链表实现.列表键的底层实现之中的一个就是链表,一个列表键包括了数量比較多的元素,列表中包括的元素 ...

  5. Unity3D发布WebPlayer时Failed to download data file解决方案

    今天发布WebPlayer时, 发现直接打开html是可以正常运行的, 但是通过iis访问的话就会报错: Failed to download data file. 一开始以为是防火墙, 后来发现不是 ...

  6. 促销R语言应用性能

    1.       绩效评估 时间的确定 R测量时间是在最简单的方式提供是system.time性能. system.time(expr, gcFirst=TRUE) 这个函数会在不减少程序执行性能的情 ...

  7. 第三章 AOP 基于@AspectJ的AOP

    在前面,我们分别使用Pointcut.Advice.Advisor接口来描述切点.增强.切面.而现在我们使用@AdpectJ注解来描述. 在下面的例子中,我们是使用Spring自动扫描和管理Bean. ...

  8. c# 通过解析mp3规范命名并上传服务器

    引用using Shell32; private void Open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = ne ...

  9. DLNA它 Error, can&#39;t findlibavformat ! 解

    DLNA库版本号为libdlna-0.2.4 运行./configure出错: ------------------------------ Error, can't findlibavformat ...

  10. 小技巧,把execl.exe转换成dll

    小技巧 把execl.exe转换成dll1.把excel.exe复制到CProgram FilesMicrosoft Visual Studio .NET 2003SDKv1.1Bin夹中,和TlbI ...