poj 3468 成段增减
Sample Input
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
Sample Output
4
55
9
15
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
using namespace std;
const int maxn=;
ll sum[maxn<<];
ll cov[maxn<<];
int n,m,t;
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int m)
{
if(cov[rt])
{
cov[rt<<]+=cov[rt];
cov[rt<<|]+=cov[rt];
sum[rt<<]+=cov[rt]*(m-(m>>)); //注意括号
sum[rt<<|]+=cov[rt]*(m>>);
cov[rt]=;
}
}
void build(int l,int r,int rt)
{
cov[rt]=;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int add,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
cov[rt]+=add;
sum[rt]+=(ll)add*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(add,L,R,lson);
if(m<R) update(add,L,R,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if (L<=l&&r<=R)
{
return sum[rt];
}
pushdown(rt,r-l+);
int m=(l+r)>>;
ll ret=;
if (L<=m) ret+=query(L,R,lson);
if (m<R) ret+=query(L,R,rson);
return ret;
}
int main()
{
int i,j,k,q;
//freopen("1.in","r",stdin);
scanf("%d%d",&n,&q);
build(,n,);
while(q--)
{
char s[];
scanf("%s",s);
if(s[]=='Q')
{
int L,R;
scanf("%d%d",&L,&R);
printf("%lld\n",query(L,R,,n,));
}
else
{
int L,R,c;
scanf("%d%d%d",&L,&R,&c);
update(c,L,R,,n,);
}
}
return ;
}
poj 3468 成段增减的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- 线段树之成段更新( 需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候)
HDU 1698 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***
不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
随机推荐
- WebBrowser 禁用右键
禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...
- Swift翻译之-Swift语法入门 Swift语法介绍
目录[-] Hello world - Swift 简单赋值 控制流 函数与闭包 对象和类 枚举与结构 协议和扩展 泛型 2014.6.3日,苹果公布最新编程语言Swift,Swift是一种新的编程语 ...
- PHP中array_chunk的用法
转自:http://cn2.php.net/manual/zh/function.array-chunk.php (PHP 4 >= 4.2.0, PHP 5) array_chunk — 将一 ...
- matlab 更改横坐标坐标值的方向
[转载]http://blog.sina.com.cn/s/blog_7270407901012dyd.html#post 非常有用的Matlab代码,收藏了. function th=rotat ...
- 抓包工具 wireshark
http://fangxin.blog.51cto.com/1125131/735178
- poj1142.Smith Number(数学推导)
Smith Number Time Limit: 1 Sec Memory Limit: 64 MB Submit: 825 Solved: 366 Description While skimm ...
- L4 如何在XCode中下进行工作
原地址:http://www.howzhi.com/course/286/lesson/2107 管理您的工作流程在Xcode 正如你在本教程中看到了 你的第一个iOS应用,你在Xcode工作区窗口 ...
- Java和Python运行速度对比
Java和Python运行速度对比:同一个函数运行一百万次,Java耗时0.577秒,Python耗时78秒--135倍的差距. 版本:Java 8,Python 2.7.10 Java测试代码: i ...
- 【云计算】Docker Nginx示例
使用数据卷容器,配置Nginx Docker作为静态文件服务器 . 该方法是直接使用命令行,当然也可使用Dockerfile文件进行创建. 其实,使用docker创建nginx容器是很简单的,但要和数 ...
- Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...