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 ...
随机推荐
- Python原型模式
如果想根据现有对象复制出新的对象并对其修改,可以考虑原型模式(Prototype Pattern) class Point: __slots__ = ("x", "y&q ...
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
- 一步步学习ASP.NET MVC3 (14)——Route路由
请注明转载地址:http://www.cnblogs.com/arhat 由于今天是星期六,所以多写几篇,感觉前几天的忙碌没有及时发布文章,趁着周末老魏尽力的多写几篇文章.因为本系列基本上快结束了,所 ...
- CODEVS 3286 火柴排队
[题目描述 Description] 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为: ,其中 ai表示 ...
- JavaScript: top对象
一般的JS书里都会在讲框架集的时候讲top,这会让人误解,认为top对象只是代表框架集,其实top的含义应该是说浏览器直接包含的那一个页面对象,也就是说如果你有一个页面被其他页面以iframe的方式包 ...
- posix和system v有什么区别/?
posix和system v有什么区别/?现在在应用时应用那一标准浮云484212 | 浏览 243 次 2014-11-06 10:362014-11-19 22:36 最佳答案们是有关信号量的两组 ...
- 去除Coding4Fun中MessagePrompt的边框(Border)
在App.xaml文件中添加 xmlns:c4f="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)
一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...
- html5 飞船动画
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- tomcat docBase 和 path
<Context docBase="zjzc-web-api" path="/api" reloadable="false"/> ...