PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树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 (段树更新间隔总和)的更多相关文章
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 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 ...
随机推荐
- 【译】ASP.NET MVC 5 教程 - 3:添加视图
原文:[译]ASP.NET MVC 5 教程 - 3:添加视图 在本节内容中,我们将修改HelloWorldController类,使用视图模板来干净利索的封装生成HTML响应客户端的过程. 您将创建 ...
- (适合入门)JVM堆内存相关的启动参数:年轻一代、岁和永久代内存分配
假设你要观察JVM进程消耗的堆内存,通过命令工具jmap或可视化工具jvisualvm.exe.JVM这些参数的默认启动值.假设你想知道JVM内存分配策略,最开始手动设置这些参数.通过JDK统计结果, ...
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...
- Ubuntu--有关VMware Tools安装问题
虚拟机中找不到VMware Tools选项 在虚拟机上安装了ubuntu系统后,是不可以进行系统间数据共享的,也就是说我win7系统里的文件,不能拷贝到虚拟机的ubuntu系统. 解决方案:我们需要安 ...
- 你真的了解try{ return }finally{}中的return?(转)
今天去逛论坛 时发现了一个很有趣的问题: 谁能给我我解释一下这段程序的结果为什么是:2.而不是:3 代码如下: class Test { public int aaa() { int x = 1; t ...
- Jetty开发指导:Jetty Websocket API
Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...
- session与cookie的差别
session session 的工作机制是:为每一个訪客创建一个唯一的 id (UID),并基于这个 UID 来存储变量.UID 存储在 cookie 中,或者通过 URL 进行传导. ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- Unity--关于优化方面的那些事儿(一)
近期做一个小项目,要求包的大小不能超过30M. 晚上做了个小实验,方法的确非常本,只是曾经非常多没懂的地方如今清晰了很多,我是菜鸟!希望本文章对大家有帮助,谢谢! 实验结果: 实验结果: 1.场景中仅 ...
- mySQL中删除unique key的语法 (删除某个字段的唯一性)
mySQL中删除unique key的语法 CREATE TABLE `good_booked` ( `auto_id` int(10) NOT NULL auto_increment, `goo ...