https://vjudge.net/contest/66989#problem/C

You have N integers, A1, A2, ... , 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 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 querying the sum of Aa, Aa+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
 //线段树区间和裸题
//#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=;
long long sum[maxn<<],add[maxn<<];
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int ln,int rn)
{
if(add[rt])
{
sum[rt<<]+=add[rt]*ln;
sum[rt<<|]+=add[rt]*rn;
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
add[rt]=;
}
}
void build(int l,int r,int rt)
{
if(r==l)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
sum[rt]+=(long long)c*(r-l+);
add[rt]+=c;
return;
}
int m=(l+r)>>;
pushdown(rt,m-l+,r-m);
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,rson);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
int m=(r+l)>>;
pushdown(rt,m-l+,r-m);
long long ans=;
if(L<=m)
ans+=query(L,R,lson);
if(R>m)
ans+=query(L,R,rson);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,m,x,y,z,q;
char h;
while(~scanf("%d%d",&n,&q))
{
memset(sum,,sizeof sum);
memset(add,,sizeof add);
build(,n,);
for(int i=;i<=q;i++)
{
getchar();
scanf("%c",&h);
if(h=='Q')
{
scanf("%d%d",&x,&y);
cout<<query(x,y,,n,)<<endl;
}
else if(h=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,,n,);
}
}
}
return ;
}

注意点:

是道裸题了

BUT 在输入数据的时候 ,scanf("%lld",&sum[rt]);一开始把它写成%d了,害我WA了好几发;

sum[rt]+=(long long)c*(r-l+1); 一开始没注意把数据转化为long long(>人<;)。

以后一定要注意呀!

POJ3468(线段树区间求和+区间查询)的更多相关文章

  1. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  2. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  3. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

  4. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  5. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  6. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

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

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

  8. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

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

随机推荐

  1. MYSQL一次千万级连表查询优化

    概述:交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上. 这个S ...

  2. Codeforces675D(SummerTrainingDay06-J)

    D. Tree Construction time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  3. POJ3090(SummerTrainingDay04-M 欧拉函数)

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7450   Accepted: ...

  4. Vue, element-ui Module build failed: Error: No PostCSS Config found

    使用vue框架写pc页面时,我们经常会用到element-ui这个框架. 当我们吧把需要的东西都装在好运行项目的时候,有时会出现这样的错误, Module build failed: Error: N ...

  5. 【读书笔记】iOS-iOS定位

    iOS提供3种不同的定位途径: 1,WiFi定位,通过查询一个WiFi路由器的地理位置信息,比较省电:iPhone,iPod touch和iPad都可以采用: 2,蜂窝式移动电话基站定位,通过移动运营 ...

  6. 清除input numer 点击样式

    input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; }

  7. 如何在Oracle数据库中查看哪些用户在执行哪些SQL

    对于DBA来说,这是一个非常常见的问题,DBA需要找出以下问题: 1.哪些用户在跑哪些SQL? 2.一个特定的SQL是被哪个用户在执行? 3.一个特定的用户在跑哪些SQL? 从这些问题中可以很明显的看 ...

  8. 网络基础 港湾FlexHammer5010交换机镜像端口配置

    港湾FlexHammer5010交换机镜像端口配置 by:授客 QQ:1033553122 1.登陆港湾交换机FlexHammer5010交换机 方法: telent 交换机ip 输入用户名 输入用户 ...

  9. Android之使用枚举利弊及替代方案

    Android上不应该使用枚举,占内存,应该使用@XXXDef注解来替代 使用 Enum 的缺点 每一个枚举值都是一个对象,在使用它时会增加额外的内存消耗,所以枚举相比与 Integer 和 Stri ...

  10. Python 列表(List)操作方法详解

    Python 列表(List)操作方法详解 这篇文章主要介绍了Python中列表(List)的详解操作方法,包含创建.访问.更新.删除.其它操作等,需要的朋友可以参考下   列表是Python中最基本 ...