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 ...
随机推荐
- 【pyhton】【转】修改递归次数
import sys sys.setrecursionlimit(1500) # set the maximum depth as 1500 def recursion(n): if(n <= ...
- .Net 平台下的互联网架构新思考
上一篇<互联网应用架构谈>有很多阅读量,但后来实践中发现基于WCF服务层技术用于实现网站访问的效率是有问题的. 我的本意是以WCF中间层解决商业逻辑,供各个平台调用,然而还是带来诸多的麻烦 ...
- GPU CUDA常量内存使用
#include <cuda.h> #include <stdio.h> int getMulprocessorCount(){ cudaDeviceProp prop; cu ...
- NGUI系列教程二
接下来我们创建一个Label,NGUI->Open the Widget Wizard,打开widgetTool对话框,在Template中选择Label,确定AddTo右侧选项为panel,点 ...
- javascript debut trick, using the throw to make a interrupt(breakpoint) in your program
console.log('initialize'); try { throw "breakPoint"; } catch(err) {} when I debug the extj ...
- mongodb Install the MongoDB service
在用到mongodb时,首先要运行mongod.exe以启动mongo,这样就会出现命令框( command prompt),为了避免出现这种情况.要以服务的形式来启动mongo,这样就不会出现命令框 ...
- strstr函数与strcmp函数
1.strstr函数主要完成在一个字串中寻找另外一个字串 函数实现工程如下:摘自http://baike.baidu.com/link?url=RwrzOxs0w68j02J2uQs5u1A56bEN ...
- 剖析 Linux hypervisor--KVM 和 Lguest 简介
慢慢弄清楚.. M. Tim Jones, 顾问工程师, Emulex Corp. M. Tim Jones 是一名嵌入式软件工程师,他是 Artificial Intelligence: A S ...
- Java中对List集合的排序
方法一: 第一种方法,就是list中对象实现Comparable接口,代码如下: 实体类: public class Person implements Comparable<Person> ...
- HDU 5044 TREE
题意:一棵树上两种操作,操作1,改变u到v的每一点的值增加k,操作2,改变u到v每一条边值增加k.最后结束时问,每一点和每一条边的值. 初始时,点和边的值都为0. 分析: 很显然要用树链剖分,将点和边 ...