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 ...
随机推荐
- 解决eclipse project前出现红色感叹号 但没有提示错误出在什么地方build path jar包也没报错
在problems窗体能够看到错误信息 菜单条 window-->show view-->problems 或alt+shift+q 然后按x 又一次选一下包就攻克了
- UVA12304 2D Geometry 110 in 1! 计算几何
计算几何: 堆几何模版就能够了. . .. Description Problem E 2D Geometry 110 in 1! This is a collection of 110 (in bi ...
- VS2010,原来还有这些快捷键,果断记下来!
一直认为VS的快捷键跟eclipse比起来差远了,那些由于不知道还有如此多有效却不知的快捷键阿 1. 检查括号匹配(在左右括号间切换): Ctrl +] 2. 选中从光标起到行首(尾)间的代码: Sh ...
- c++自带倒置数组函数
#include<stdio.h> #include <vector> #include <queue> #include<algorithm> usi ...
- ubuntu server编译安装nginx
刚刚安装好了ubuntu server14.04,如今要安装一个webserver,纯静态就用nginx应用程序server吧,性能出众啊. 安装编译环境 我们这里採用源代码编译安装的方式,大家能够看 ...
- Windows Phone开发(35):使用Express Blend绘图
原文:Windows Phone开发(35):使用Express Blend绘图 上一节中我们简单扯了一下绘图指令,然而那也不是最简单的绘图法,今天,我再向大家推荐一种更好的绘图方案--Express ...
- java实现代理domino web邮件下载
[背景] 近日在研究domino web邮件下载功能,下载的邮件能够导入foxmail.outlook邮件client,下载的邮件格式为eml. [參考代码](须要下载jmail.jar包) priv ...
- 开源 自由 java CMS - FreeCMS1.8 网上申报
项目地址:http://code.google.com/p/freecms/ 在线申报 1. 转交申报 用户能够把申报转交给其它人办理,系统会记录此申报的转交记录. 注意:同一时候仅仅能转交一个申报. ...
- Spring MVC helloWorld中遇到的问题及解决办法
1.java.io.FileNotFoundException: Could not open ServletContext resource不能加载ServletContext的用法是配置到web. ...
- java线程池的注意事项
java线程池是建立一个技术进步的线程.到来之前线程池打造一批在线程的线程尚未,队列上的备用,然后再利用这些资源.减少频繁创建和销毁对象, 1.jdk1.5以上提供了现成的线程池 2.java线程池的 ...