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,那 ...
随机推荐
- [Java] Tomcat环境变量设置
@echo off title Tomcat环境变量设置 color 0a set /p inputTH=D:\Work\024_Tomcat if /i "%inputTH%"= ...
- 让你的APK瘦成一道闪电
APK瘦身是长久以来的难题,我们需要通过一些工具和技巧才能让它瘦下去,下面我来分享一下我在apk瘦身方面的经验. 一.apk中有哪些东西 1.代码 2.lib 3.so本地库 4.资源文件(图片,音频 ...
- UIModalPresentationStyle和UIModalTransitionStyle
一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...
- [Android]基于RxJava、RxAndroid的EventBus实现
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4578699.html Github:https://gith ...
- Android Handler机制(三)----Looper源码解析
一.Looper Looper对象,顾名思义,直译过来就是循环的意思,从MessageQueue中不断取出message. Class used to run a message loop for a ...
- iOS上架90034问题解决
开发完成的APP,我们当然要上传到AppStore里面了,这可是我们心血的结晶. 可是,就当我们兴奋之余,却发现我们的App根本无法上传到AppStore.我们百度.谷歌了N种方法,就像我这种逗逼,整 ...
- iOS多线程实现1-pthread
1 操作系统.进程.线程简单介绍 现在的程序都是在操作系统上跑,很少有裸机的,而且大部分的嵌入式应用也都支持操作系统,当然还有一些很低端的嵌入式设备没有操作系统. iPhone手机跑的是iOS操作系统 ...
- UI中一些不常用的控件UIActivityIndicatorView、UIProgressView、UISegmentedControl、UIStepper、UISwitch、UITextView、UIAlertController
//UIActivityIndicatorView //小菊花,加载 #import "ActivityIndicatorVC.h" @interface ActivityIndi ...
- IOS开发--支付宝支付
前言:继上次<IOS开发--微信支付>以来,一直没有太多时间,更新总结详细支付这样的长篇大论,很抱歉.今天,推出支付宝支付的详细流程. 1.开始下载和查看支付宝支付的Demo. 我们直接进 ...
- 从多个XML文档中读取数据用于显示webapi帮助文档
前言: 你先得知道HelpPageConfig文件,不知道说明你现在不需要这个,所以下文就不用看了,等知道了再看也不急.当然如果你很知道这个,下文也不用看了,因为你会了. 方法一: new XmlDo ...