意甲冠军:一个典型的段树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. 移动开发平台-应用之星app制作教程

    目前在AppStore.GooglePlay等应用商店里已经有以百万计的Apps,应用程序使移动互联网空间得以无限拓展.很多人梦想着AngryBirds式的奇迹在自己身上发生,他们渴望自己开发的应用程 ...

  2. CrossBridge介绍

    CrossBridge介绍 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs CrossBridge是Adobe FlasCC的开源版本,它提供了一个完整 ...

  3. MYSQL高可用(HA)随想

    记得在上一篇文章“Java集群--大型网站是怎样解决多用户高并发访问的”的结尾处本人阐述了数据库的高可用的一种方案----实现主从部署,那么今天,就让我聊聊本人关于数据库的一些所思所想吧! 下面是本人 ...

  4. 金色的 SQL注意事项(1)

    page(1-75) 最好是没有意义的主键字段,以方便未来的扩展. PS:主键,以后标书编码填错须要改的时候,关联表都须要跟着改.假设是一个无意义的自增字段是主键就无此原因. 主键最好不要设置为联合主 ...

  5. Spring jdbc 对象Mapper的简单封装

    一般查询实体的时候,都需要这么使用/**      * 根据id查询      *       * @return      */     public Emp queryEmpById(Intege ...

  6. windows phone 墓碑化(9)

    原文:windows phone 墓碑化(9) 几个术语的理解: 名称 说明 ;        App ap = (App)Application.Current;        // 构造函数    ...

  7. Socket编程实践(13) --UNIX域名协议

    UNIX域名协议 UNIX域套接字与TCP相比, 在同一台主机上, UNIX域套接字更有效率, 差点儿是TCP的两倍(因为UNIX域套接字不须要经过网络协议栈,不须要打包/拆包,计算校验和,维护序号和 ...

  8. 使用cocos2d 2.1制作一条河游戏(4): 主要的游戏逻辑BaseLayer设计

    前段时间一直忙着.没有时间更新博客.今天,仍然需要一段时间才能实现对游戏的一小部分,最后打动他. BaseLayer.h: #import <GameKit/GameKit.h> #imp ...

  9. Windows Phone开发(25):启动器与选择器之WebBrowserTask

    原文:Windows Phone开发(25):启动器与选择器之WebBrowserTask 从名字上就看出来,这个家伙就是打开浏览并浏览到指定页面. 它有两个用途完全一样的属性:Uri属性是Syste ...

  10. Android定义自己的面板共享系统

    在Android分享知道有一个更方便的方法.调用的共享面板来分享我们的应用程序的系统.主要实现例如,下面的: public Intent getShareIntent(){ Intent intent ...