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 ...
随机推荐
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
[spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...
- 开发设计模式(七)工厂模式(Factory Method Pattern)
工厂模式是我们最常用的模式了,著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因为工厂模式就相当于创建实例对象的new,我们经常要根 ...
- Provider Communication with Apple Push Notification Service
This chapter describes the interfaces that providers use for communication with Apple Push Notificat ...
- js正则匹配查找
var pattern1 = /好/g; console.log(pattern1.test("你好")); 字符串查找: var pattern1 = /\w/; console ...
- display:inline-block的深入理解
在使用 CSS 实现表现的时候,会经常接触到 display:inline-block 这一属性,无论是 初接触 Web 标准还是接触标准已久的朋友,大都会对这一属性感觉很迷惑和模糊. display ...
- asp.net(c#)有关 Session 操作的几个误区
1. this.Session["username"] = null HttpSessionState 内部使 用 NameObjectCollection 类型的集合对象来存储 ...
- 1842-A. Broj
#include <iostream> using namespace std; int main() { int n; cin>>n; if(n>0&& ...
- c++学习之旅-Cygwin+Eclipse ide for c++
一,cygwin下载完毕后配置系统环境片两path指向cygwin/bin 二,eclipse设置 2.1 设置工作目录的cygwin映射 cygwin/d ->d:\ 2.2设置编译 下面新建 ...
- asp.net网站后台退出后,点后退按钮仍能进,如何安全退出
用户登录成功后,将用户名保存Session Session["usrename"]=username; 退出后Sessssion["username"]=str ...
- 综合经验:IO读写错误必然导致程序崩溃
仿佛是忽然间产生的问题,每次程序退出时候,必然崩溃,花了整整一天才找到原因,就是对数据库的IO读写错误.主要是因为析构函数调用了Disconnect函数,内容如下: void SFTPTool::Di ...