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. Oracle存储过程练习题

    1.1.创建一个过程,能向dept表中添加一个新记录.(in参数) 创建过程 create or replace procedure insert_dept ( num_dept in number, ...

  2. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  3. 悲剧文本(Broken Keyboard (a.k.a. Beiju Text),UVA 11988)

    题目描述: 题目思路: 1.使用链表来重新定位各个字符 2.用数组实现链表 3.开一个数组list[i]来存储字符数组下一个字符的位置 #include <iostream> #inclu ...

  4. 【转】unity 热更新思路和实现

    声明:本文介绍的热更新方案是我在网上搜索到的,然后自己修改了一下,相当于是借鉴了别人的思路,加工成了自己的,在此感谢无私分享经验的朋友们. 想要使用热更新技术,需要规划设计好资源比较策略,资源版本,确 ...

  5. [leetcode-718-Maximum Length of Repeated Subarray]

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  6. js如何使浏览器允许脚本异步加载

    js如何使浏览器允许脚本异步加载 如果脚本体积很大,下载和执行的时间就会很长,因此造成浏览器堵塞,用户会感觉到浏览器“卡死”了,没有任何响应.这显然是很不好的体验,所以浏览器允许脚本异步加载,下面就是 ...

  7. 关于onclick和addeventlistener('click'),click的整理

    代码 $(function(){ $("#btn").click(function(){ console.log(2) }) $("#btn").click(f ...

  8. TCP系列12—重传—2、Linux超时重传引入示例

    在前面我们概述了TCP的超时重传之后我们简单的看一下tcp超时重传的示例.首先简单的描述一下测试过程 1.设置/proc/sys/net/ipv4/tcp_early_retrans为2,关掉TLP功 ...

  9. 软工网络15个人作业4-alpha阶段个人总结(201521123059 叶文柠)

    一.个人总结 (1) 类别 具体技能和面试问题 现在回答 毕业找工作时 语言 最拿手的计算机语言之一,代码量多少? 感觉自己没有最拿手的语言,而且拿手的在计算机网络这方面的,所以在软件变成这方面的代码 ...

  10. 解决ueditor编辑器图片在线管理图片无法显示

    使用ueditor,点击在线管理,服务器图片路径显示不正确,如下图所示  查看源码,如下:  发现图片src中中间多了一长串的项目跟路径,解决的办法是 把  jsp/controller.jsp 里面 ...