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 教程 - 4:添加模型
原文:[译]ASP.NET MVC 5 教程 - 4:添加模型 在本节中,我们将添加一些管理电影数据库的类,这些类在ASP.NET MVC 应用程序中扮演“Model”的角色. 我们将使用.NET F ...
- SWT的TableVierer的使用二(数据排序)
有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序.那JFace是如何实现这个功能的呢?在JFace中是通过一个排序器来实现的,就是Vie ...
- Python的包管理
0.Python的包管理 在刚开始学习Python的时候比较头疼各种包的管理,后来搜到一些Python的包管理工具,比如setuptools, easy_install, pip, distribut ...
- http协议知识整理(转)
HTTP 协议 作为web开发人员,了解一些http协议的知识很有必要.本文简单介绍了HTTP协议的知识,若有错误的地方,望大家指正. 1.HTTP协议是什么? http协议是一个应用层的协议.规定了 ...
- Eclipse怎么显示行号,定位某行
Eclipse怎么显示行号,定位某行 设置显示行号 1 快捷键:Ctrl+F10,点击'Show Line Numbers'即可. 如果取消显示行号,也可以按这个快捷键,然后再点击show line ...
- _tkinter.TclError: no display name and no $DISPLAY environment variable
_tkinter.TclError: no display name and no $DISPLAY environment variable 这是在使用cocos2d-x的pluginx时遇到的一个 ...
- html弹窗,与弹出对话框
弹出对话框 <script type="text/JavaScript"> <!-- alert("Good Morning!"); //al ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...
- Just like normal variables,
Just like normal variables, pointers can be declared constant. There are two different ways that poi ...
- bzoj1001/BJOI2006 灰太狼抓到的兔子
1001: [BeiJing2006]狼抓兔子(id=1001">传送门) 图论新知识..没学过.. 平面图最小割等于对偶图的最短路 详见课件:http://wenku.baidu.c ...