A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 72265   Accepted: 22299
Case Time Limit: 2000MS

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

Hint

The sums may exceed the range of 32-bit integers.

Source

 
题目意思:
给一个长度为n的数组,有q组操作,操作有两种:Q l, r  即查询l到r的和。  C l, r, val   即把l到r数组元素都加上val
 
思路:
线段树经典题目,需要用lazy思想,也就是当把l r加上val时,只标记这个区间lazy=val即可 ,当下次再加上一个val时且在l r之间,那么向下传递。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n; struct node{
int l, r;
__int64 val, sum;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=;
if(l==r){
scanf("%I64d",&a[root].sum);
return;
}
build(l,mid,ll);
build(mid+,r,rr);
a[root].sum=a[ll].sum+a[rr].sum;
} void down(int root){
if(a[root].val&&a[root].l!=a[root].r) {
a[ll].val+=a[root].val;
a[rr].val+=a[root].val;
a[ll].sum+=(__int64)(a[ll].r-a[ll].l+)*a[root].val;
a[rr].sum+=(__int64)(a[rr].r-a[rr].l+)*a[root].val;
a[root].val=;
}
} void update(int l,int r,__int64 val,int root){
if(a[root].l==l&&a[root].r==r){
a[root].val+=val;
a[root].sum+=(__int64)(a[root].r-a[root].l+)*val;
return;
}
down(root);
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else {
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
a[root].sum=a[ll].sum+a[rr].sum;
} __int64 query(int l,int r,int root){
if(a[root].l==l&&a[root].r==r){
return a[root].sum;
}
down(root);
if(r<=a[ll].r) return query(l,r,ll);
else if(l>=a[rr].l) return query(l,r,rr);
else return query(l,mid,ll)+query(mid+,r,rr);
} void out(int root){
if(a[root].l==a[root].r) {
printf("%I64d ",a[root].sum); return;
}
down(root);
out(ll);
out(rr);
}
main()
{
int i, j, k;
int q;
while(scanf("%d %d",&n,&q)==){
build(,n,);
char s[];
__int64 w;
int l, r;
while(q--){
scanf("%s",s);
if(strcmp(s,"Q")==){
scanf("%d %d",&l,&r);
printf("%I64d\n",query(l,r,));
}
else{
scanf("%d %d %I64d",&l,&r,&w);
update(l,r,w,);
// out(1);
}
}
}
}

POJ 3468 区间更新,区间求和(经典)的更多相关文章

  1. poj3468(线段树区间更新&区间求和模板)

    题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

  2. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. hdu6070(分数规划/二分+线段树区间更新,区间最值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...

  4. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  5. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树

    区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...

  7. 线段树区间更新+区间求和模板(数组实现)洛谷p3372,p3373

    模板题目1:https://www.luogu.org/problemnew/show/P3372 懒惰标记讲解:https://www.cnblogs.com/wushengyang/p/11194 ...

  8. POJ 3468 线段树区间修改查询(Java,c++实现)

    POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

  9. NBOJv2 1004 蛤玮打扫教室(线段树区间更新区间最值查询)

    Problem 1004: 蛤玮打扫教室 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %l ...

  10. 【DFS序+线段树区间更新区间求最值】HDU 5692 Snacks

    http://acm.hdu.edu.cn/showproblem.php?pid=5692 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...

随机推荐

  1. 选择列表控件的使用(PickList)

    需要下载picklist.dll类库配合使用 <%@ Register TagPrefix="cc1" Namespace="PickListControl&quo ...

  2. [css] px em rem

    一.区别 px是相对于显示器屏幕分辨率而言的. em相对于浏览器的默认字体尺寸. rem相对于HTML根元素. 二.使用  1.em 任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: ...

  3. strlen函数

    笔试题:不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数.函数接口声明如下:int strlen(const char *p); http://soft.chinabyt ...

  4. 转!大端模式&小端模式

    大端模式&小端模式   在C语言中除了8位的char型之外,还有16位的short型,32位的long型(要看具体的编译器),对于位数大于8位的处理器,例如16位或者32位的处理器,由于寄存器 ...

  5. 简单的css居中问题(日常记录)

    1.今天遇到了一个奇怪的问题:因为网页要适配大小分辨屏幕,需要把一张图片放到div中,我的初始思路是把图放在div中绝对对位给top50%left50%,但是不行,因为当网页调窄时图片就因为显得太大了 ...

  6. css读书笔记1:HTML标记和文档结构

    块级元素和行内元素:块级元素:上下堆叠,每个块级元素都独立占一行.块级元素的盒子宽度与父元素同宽.行内元素:左右堆叠,只有在空间不足的情况下才会折到下一行显示.行内元素的盒子会收缩包裹其内容,并尽可能 ...

  7. Link Collecting

    ----------------------------------\ ACM入门总结之常见输入输出格式暨hdu1089~1096 题解,谨献给对acm感兴趣的新人 - 博客频道 - CSDN.NET ...

  8. computer English

    算法常用术语中英对照Data Structures 基本数据结构Dictionaries 字典PriorityQueues 堆Graph Data Structures 图Set Data Struc ...

  9. 安装 request模块

    python3 requests 安装包下载安装[windows] 听语音 | 浏览:54 | 更新:2016-07-25 17:09 windows下直接使用:easy_install reques ...

  10. Oracle 表死锁 解决

    问题:更新的Update语句一直在更新 卡在执行update语句的地方. 清除的方法: Oracle表死锁解除   我是在plsql中处理  1.先查询  select * from v$locked ...