poj------(3468)A Simple Problem with Integers(区间更新)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 60745 | Accepted: 18522 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
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
Hint
Source
#include<cstdio>
#include<cstring>
const int maxn=;
struct node
{
int lef,rig;
__int64 sum,cnt;
int mid(){
return lef+(rig-lef>>);
}
};
node reg[maxn<<]; void Build(int left ,int right,int pos)
{
reg[pos]=(node){left,right,,};
if((left==right))
{
scanf("%I64d",®[pos].sum);
return ;
}
int mid=reg[pos].mid();
Build(left,mid,pos<<);
Build(mid+,right,pos<<|);
reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
}
void Update(int left,int right,int pos,int val)
{
if(reg[pos].lef>=left&®[pos].rig<=right)
{
reg[pos].cnt+=val;
reg[pos].sum+=val*(reg[pos].rig-reg[pos].lef+);
return ;
}
if(reg[pos].cnt)
{
reg[pos<<].cnt+=reg[pos].cnt;
reg[pos<<|].cnt+=reg[pos].cnt;
reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
reg[pos].cnt=;
}
int mid=reg[pos].mid();
if(left<=mid)
Update(left,right,pos<<,val);
if(right>mid)
Update(left,right,pos<<|,val);
reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
}
__int64 Query(int left,int right,int pos)
{
if(left<=reg[pos].lef&®[pos].rig<=right)
{
return reg[pos].sum;
}
if(reg[pos].cnt) //再向下更新一次
{
reg[pos<<].cnt+=reg[pos].cnt;
reg[pos<<|].cnt+=reg[pos].cnt;
reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
reg[pos].cnt=;
}
int mid=reg[pos].mid();
__int64 res=;
if(left<=mid)
res+=Query(left,right,pos<<);
if(mid<right)
res+=Query(left,right,pos<<|);
return res;
}
int main()
{
int n,m,a,b,c;
char ss;
while(scanf("%d%d",&n,&m)!=EOF)
{
Build(,n,);
while(m--)
{
getchar();
scanf("%c %d%d",&ss,&a,&b);
if(ss=='Q')
printf("%I64d\n",Query(a,b,));
else{
scanf("%d",&c);
Update(a,b,,c);
}
}
}
return ;
}
poj------(3468)A Simple Problem with Integers(区间更新)的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- 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 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- glyphicons-halflings-regular.ttf 404
这个是用bootstrap框架时我遇到的问题,个人解决过程如下: ① 这个资源不是我手动引用的,是bootstrap.min.css文件间接调用的. ② 默认的路径是css文件路径是project/c ...
- linux远程XP
众所周知XP下有"远程桌面连接"用来远程登录桌面,设置也非常简单. 那有没有什么办法在linux下远程登录到XP呢?有.用rdesktop这个linux下的软件就能实现. 发现新立 ...
- python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法
python_way day18 html-day4 1.Django-路由系统 - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...
- HDU 1247 Hat's Words (map+string)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HIHO线段树(成段)
#include <stdio.h> #define lson l,mid,id<<1 #define rson mid+1,r,id<<1|1 ; ],lazy[ ...
- Python学习笔记3—字符串
原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...
- overflow:auto/hidden的应用
一.自适应两栏布局 <!DOCTYPE html><html lang="zh-CN"><head> <meta charset=&quo ...
- android源码解析(十七)-->Activity布局加载流程
版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...
- 【linux命令】:查看系统运行状态,命令top 【转载】
原文:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的性能分析工具,能够实时显示系 ...
- kvm相关文章
配置KVM虚拟机的网络 Bridge和Nat方式http://www.it165.net/os/html/201503/12231.html KVM虚拟机网络配置 Bridge方式,NAT方式 htt ...