A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 149972   Accepted: 46526

题目链接:http://poj.org/problem?id=3468

Description:

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

题解:

线段树模板题,注意一下lazy标记的下传操作,标记也是long long 型的。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e5+;
int n,m;
ll a[N];
ll ans;
struct Tree{
int l,r;
ll f,w;
}tre[(N<<)+];
void build(int o,int l,int r){
tre[o].l=l;tre[o].r=r;tre[o].f=;
if(l==r){
tre[o].w=a[l];
return ;
}
int mid=l+r>>;
build(o<<,l,mid);
build(o<<|,mid+,r);
tre[o].w=tre[o<<].w+tre[o<<|].w;
}
void down(int o){
tre[o<<].f+=tre[o].f;
tre[o<<|].f+=tre[o].f;
tre[o<<].w+=tre[o].f*(tre[o<<].r-tre[o<<].l+);
tre[o<<|].w+=tre[o].f*(tre[o<<|].r-tre[o<<|].l+);
tre[o].f=;
}
void update(int o,int l,int r,int val){
int L=tre[o].l,R=tre[o].r;
if(L>=l && R<=r){
tre[o].w+=(ll)val*(R-L+);
tre[o].f+=val;
return ;
}
down(o);
int mid=L+R>>;
if(l<=mid) update(o<<,l,r,val);
if(r>mid) update(o<<|,l,r,val);
tre[o].w=tre[o<<].w+tre[o<<|].w;
}
void query(int o,int l,int r){
int L=tre[o].l,R=tre[o].r;
if(L>=l && R<=r){
ans+=tre[o].w;
return ;
}
down(o);
int mid=L+R>>;
if(l<=mid) query(o<<,l,r);
if(r>mid) query(o<<|,l,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
build(,,n);
char s[];
for(int i=;i<=m;i++){
scanf("%s",s);
if(s[]=='Q'){
int l,r;ans=;
scanf("%d%d",&l,&r);
query(,l,r);
printf("%I64d\n",ans);
}else{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(,a,b,c);
}
}
return ;
}

POJ3468:A Simple Problem with Integers(线段树模板)的更多相关文章

  1. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  2. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  3. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  4. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  9. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  10. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. 【WXS数据类型】Function

    属性: 名称 值类型 说明 [Function].constructor [String] 返回值为“Function”,表示类型的结构字符串 [Function].length [Number] 返 ...

  2. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  3. String和StringBuffer以及StringBuilder的区别

    今天在读<java编程思想>的时间,在看到String和StringBuffer以及StringBuffer这三个类的时间,做一个随笔小结,为自己的面试做好准备! 一:String,Str ...

  4. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  5. spark提交任务的两种的方法

    在学习Spark过程中,资料中介绍的提交Spark Job的方式主要有两种(我所知道的): 第一种: 通过命令行的方式提交Job,使用spark 自带的spark-submit工具提交,官网和大多数参 ...

  6. django request bug

    bug描述:django请求request接收数据时,如果参数中包含分号时,会导致分号后面的消息丢失. 比如前台js调用代码 $.post('/get_params', { "A" ...

  7. C++字符串拼接和输入

    一 .char类型字符串以空字符结尾 1.以空字符结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾. char dog[4]={'a','b','c','d'}   //不是一个字符串 ...

  8. LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string

    1.题目大意 Write a program that outputs the string representation of numbers from 1 to n. But for multip ...

  9. 关于14道魔鬼js考题的整理

    1.(function(){ return typeof arguments })(); 这里返回时是argument类型,它是个类数组,也就对象,所以是object,准确谁是[object argu ...

  10. PCB各层介绍及AD软件画PCB时的规则

    好久没画过板了,最近因为工作关系,硬件软件全部得自己来,不得不重新打开闲置很久的AltiumDesigner.以前做过点乱七八糟的笔记,本来想回头翻看一下,结果哪儿也找不到,估计已经被不小心删掉了.  ...