poj 3468
A Simple Problem with Integers
|
Time Limit: 5000MS |
Memory Limit: 131072K |
|
|
Total Submissions: 90736 |
Accepted: 28268 |
|
|
Case Time Limit: 2000MS |
||
Description
You have N integers, A1, A2, ... , AN. You need to dealwith two kinds of operations. One type of operation is to add some given numberto each number in a given interval. The other is to ask for the sum of numbersin a given interval.
Input
The first linecontains 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 queryingthe sum of Aa, Aa+1, ... , Ab.
Output
You need to answerall Q commands in order. One answer in a line.
SampleInput
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
SampleOutput
4
55
9
15
Hint
The sumsmay exceed(超过) the range of 32-bit integers(整数).
Source
POJMonthly--2007.11.25, Yang Yi
注意:数组要用long long
线段树解法
Problem: 3468
User: ksq2013
Memory: 4776K
Time: 2532MS
Language: G++
Result: Accepted
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int n,q;
long long col[400001],sum[400001];
void pushup(int k)
{sum[k]=sum[k<<1]+sum[k<<1|1];}
void pushdown(int k,int m)
{
if(col[k]){
col[k<<1]+=col[k];
col[k<<1|1]+=col[k];
sum[k<<1]+=col[k]*(m-(m>>1));
sum[k<<1|1]+=col[k]*(m>>1);
col[k]=0;
}
}
void build(int s,int t,int k)
{
if(!(s^t)){
scanf("%lld",&sum[k]);
return;
}int m=(s+t)>>1;
build(s,m,k<<1);
build(m+1,t,k<<1|1);
pushup(k);
}
void update(int s,int t,int k,int l,int r,int c)
{
if(l<=s&&t<=r){
col[k]+=c;
sum[k]+=c*(t-s+1);
return;
}pushdown(k,t-s+1);
int m=(s+t)>>1;
if(l<=m)update(s,m,k<<1,l,r,c);
if(m<r)update(m+1,t,k<<1|1,l,r,c);
pushup(k);
}
long long query(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return sum[k];
pushdown(k,t-s+1);
int m=(s+t)>>1;
long long res=0;
if(l<=m)res+=query(s,m,k<<1,l,r);
if(m<r)res+=query(m+1,t,k<<1|1,l,r);
return res;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
for(int i=1;i<=q;i++){
char ak[3];
scanf("%s",ak);
if(ak[0]=='C'){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(1,n,1,a,b,c);
}
else{
int a,b;
scanf("%d%d",&a,&b);
printf("%lld\n",query(1,n,1,a,b));
}
}
return 0;
}
poj 3468的更多相关文章
- poj 3264 & poj 3468(线段树)
poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- 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 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- poj 3468(线段树+lazy思想)
题目链接:http://poj.org/problem?id=3468 思路:如果直接去做,每次都更新到叶子节点,那必然会TLE,我们可以采用lazy的思想:没必要每次更新都更新到叶子节点,只要有一个 ...
- POJ 3468 A Simple Problem with Integers(树状数组)
题目链接:http://poj.org/problem?id=3468 题意:给出一个数列,两种操作:(1)将区间[L,R]的数字统一加上某个值:(2)查询区间[L,R]的数字之和. 思路:数列A,那 ...
随机推荐
- Web UI - Javascript之DOM Ready
最近终于稍微适应了工作环境,终于可以让自己缓口气.于是决定要写点东西,算是督促.记录和提升自己的学习.代码的世界,你不轮它,以后就会被它轮.这个系列尽量保持在一周或两周更一篇,目标是在创造内容的时候更 ...
- [SharePoint] SharePoint 错误集 2
1 Run command “New-SPConfigurationDatabase" Feature Description: error message popup after run ...
- Java学习心得之 Linux下搭建Java环境
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Java学习心得之 Linux下搭建Java环境 1.前言2.JDK安装3.配置环境变量4. ...
- 智者当借力而行, 借助Autodesk应用程序商店实现名利双收
有没有注意到这个"精选应用"菜单?有没有想过这个菜单下的应用是从哪里来的?你的应用也可以出现在这里哦~ 如果你还不知道,Autodesk在几年前就发布了Autodesk应用程序商店 ...
- asp.net实现动态添加table行
asp.net动态的生成,删除table的行,主要是在后台动态创建单元行,单元表格,效果图: 2.代码: <%@ Page Language="C#" AutoEventWi ...
- Android 短信的还原
上篇文章讲到<Android 短信的备份>,本文主要实现Android 短信的还原,即是将一条 布局文件: <RelativeLayout xmlns:android="h ...
- AndroidStudio添加依赖库
以Gson为例 Step1:点击下图中的入口,进入ProjectStructure Step2: 在app项中选择Dependencies窗口,点击右侧的加号 Step3:在搜索框中输入gson,点击 ...
- git代码库误操作还原记录
先做一些前情提要: 我们项目使用git作为代码管理,同时为了操作更方便,安装了乌龟git(tortoiseGit)工具.以下几乎所有操作都是在乌龟git上进行. 我们的项目是分阶段完成的,在完成上一阶 ...
- [MySQL Reference Manual] 4 MYSQL Program
4 MYSQL Program 目录 4 MYSQL Program 4.3 MySQL Server和Server启动程序 4.3.1 mysqld 4.3.2 mysqld_safe 4.3.3 ...
- 转载 sql 存储过程与函数区别
SQL Server用户自定义函数和存储过程有类似的功能,都可以创建捆绑SQL语句,存储在server中供以后使用.这样能够极大地提高工作效率,通过以下的各种做法可以减少编程所需的时间: 重复使用编程 ...