POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=;
struct node
{
LL sum,val;
}tree[maxn*]; void pushup(int rt)
{
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushdown(int rt,int m)
{
if(tree[rt].val)
{
tree[rt<<].val+=tree[rt].val;
tree[rt<<|].val+=tree[rt].val;
tree[rt<<].sum+=(LL)(m-(m>>))*tree[rt].val;
tree[rt<<|].sum+=(LL)(m>>)*tree[rt].val;
tree[rt].val=;
}
} void build_tree(int l,int r,int rt)
{
tree[rt].val=;
if(l==r)
{
scanf("%I64d",&tree[rt].sum);
return ;
}
int m=(l+r)>>;
build_tree(l,m,rt<<);
build_tree(m+,r,rt<<|);
pushup(rt);
} LL query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return tree[rt].sum;
}
int m=(l+r)>>;
pushdown(rt,r-l+);
LL ans=;
if(L<=m)
ans+=query(L,R,l,m,rt<<);
if(m<R)
ans+=query(L,R,m+,r,rt<<|);
pushup(rt);
return ans;
} void update(int L,int R,int add,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
tree[rt].sum+=(LL)add*(r-l+);
tree[rt].val+=add;
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)
update(L,R,add,l,m,rt<<);
if(R>m)
update(L,R,add,m+,r,rt<<|);
pushup(rt);
} int main()
{
int n,a,b,q;
LL c;
while(~scanf("%d%d",&n,&q))
{
build_tree(,n,);
char s[];
while(q--)
{
scanf("%s",s);
if(s[]=='Q')
{
cin>>a>>b;
cout<<query(a,b,,n,)<<endl;
}
else if(s[]=='C')
{
cin>>a>>b>>c;
update(a,b,c,,n,);
}
}
}
return ;
}
POJ 3468 A Simple Problem with Integers 线段树 区间更新的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- osg 基本几何图元
转自:osg 基本几何图元 //osg 基本几何图元 // ogs中所有加入场景中的数据都会加入到一个Group类对象中,几何图元作为一个对象由osg::Geode类来组织管理. // 绘制几何图元对 ...
- Linux_Struct file()结构体
struct file结构体定义在/linux/include/linux/fs.h(Linux 2.6.11内核)中,其原型是:struct file { /* * f ...
- uCGUI简介
何为GUI? GUI是Graphic User Interface(图形用户界面)的缩写.最早的操作系统都是字符界面,使用者必须记忆和输入许多指令.而现在广泛使用的Windows操作系统则是适应GUI ...
- 如何让listview列表为空时显示提示
先在布局文件中声明一个TextView,再设置listview.setEmptyView(TextView).这样当listview中的数据为空时就会列表的位置就会显示textviw中的提示.
- Memcached(四)Memcached的CAS协议
1. 什么是CAS协议很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS是 ...
- js amd
http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html http://www.adequatelygood ...
- sqlserver删除重复数据
select identity(int,1,1) as autoID, * into #Tmp from [QYTS_QiYeCecdb]select min(autoID) as autoID in ...
- HTTP错误代码详细介绍
HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 40 ...
- Altium Designer完美双屏显示方法演示
布线时我们往往需要对一些信号线做特别的走线处理,这样需要边布线边对照原理图,在protel99中那是一个很痛苦的事,在Altium Designer中这种情况将变很简单. 硬件要求,笔记本+外接显示器 ...
- 定时显示提示控件 TToolTip
转载过来的,文章出自: http://www.delphifans.com/infoview/Article_3640.html { 修改者:ghs 日期:20071218 功能:在 ...