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.
 
题意:裸线段树 区间更新/查询 
 
题解:线段树每次写都有不同程度的错误.too vegatable
1.延迟标记的add 都要初始化为0 
2.延迟的传递是累加而不是赋值
具体看代码。
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#define ll long long
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
struct node
{
ll l,r;
ll add;
ll sum;
} tree[];
void buildtree(ll root,ll left,ll right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;//wa点 所有的延迟标记都要初始化
if(left==right)//不能放到下面的if中
{ ll exm;
scanf("%lld",&exm);
tree[root].sum=exm;
return ;
}
ll mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
void pushdown(ll root)
{
if(tree[root].add==) return ;
tree[root<<].add+=tree[root].add;//wa点 这里是增加而不是赋值
tree[root<<|].add+=tree[root].add;
tree[root<<].sum+=(tree[root<<].r-tree[root<<].l+)*tree[root].add;
tree[root<<|].sum+=(tree[root<<|].r-tree[root<<|].l+)*tree[root].add;
tree[root].add=;
}
void updata(ll root,ll left,ll right,ll c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add+=c;//wa点 这里是增加而不是赋值
tree[root].sum+=(right-left+)*c;
return ;
}
pushdown(root);
ll mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(root<<,left,right,c);
else
{
if(left>mid)
updata(root<<|,left,right,c);
else
{
updata(root<<,left,mid,c);
updata(root<<|,mid+,right,c);
}
}
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
ll query(ll root,ll left,ll right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].sum;
}
pushdown(root);
ll mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return query(root<<,left,mid)+query(root<<|,mid+,right);
}
}
ll n,q;
char what;
ll l1,r1,ad;
int main()
{
while(~scanf("%lld %lld",&n,&q))
{
buildtree(,,n);
getchar();
for(ll i=; i<=q; i++)
{
scanf("%c %lld %lld",&what,&l1,&r1);
if(what=='Q')
printf("%lld\n",query(,l1,r1));
else
{
scanf("%lld",&ad);
updata(,l1,r1,ad);
}
getchar();
}
}
return ;
}
/*
10 10
1 2 3 4 5 6 7 8 9 10
C 1 10 1
Q 2 3 10 10
1 2 3 4 5 6 7 8 9 10
C 1 5 1
Q 4 6
*/

poj 3468 线段树区间更新/查询的更多相关文章

  1. hdu 1698+poj 3468 (线段树 区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

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

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

  3. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  5. POJ 3468 (线段树 区间增减) A Simple Problem with Integers

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

  6. codevs 1299 线段树 区间更新查询

    1299 切水果  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 简单的说,一共N个水果排成 ...

  7. POJ 3225 线段树区间更新(两种更新方式)

    http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...

  8. POJ 3225 (线段树 区间更新) Help with Intervals

    这道题搞了好久,其实坑点挺多.. 网上找了许多题解,发现思路其实都差不多,所以就不在重复了. 推荐一篇比较好的题解,请戳这. 另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup ...

  9. POJ - 3468 线段树区间修改,区间求和

    由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...

随机推荐

  1. [开发笔记]-多线程异步操作如何访问HttpContext?

    如何获取文件绝对路径? 在定时器回调或者Cache的移除通知中,有时确实需要访问文件,然而对于开发人员来说, 他们并不知道网站会被部署在哪个目录下,因此不可能写出绝对路径, 他们只知道相对于网站根目录 ...

  2. 'NSInteger' (aka 'long') to 'int32

    怎么去掉Xcode工程中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int32 ...

  3. Asp.net项目因Session阻塞导致页面打开速度变慢

    发现罪魁祸首是Session阻塞造成的.默认情况下session状态是“可写状态”(EnableSessionState=”true”),即当用户打开任何一个页面时,该页面的Session就会持有一个 ...

  4. SharePoint 2013 企业搜索架构示例

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,我们来看一下企业中对于不同规模SharePoint搜索的场的架构是什么样的. 对于搜索场的规模, ...

  5. mp3文件 ID3v2 帧标识的含义

    mp3文件 ID3v2 帧标识的含义 Declared ID3v2 frames The following frames are declared in this draft. 4.20 AENC ...

  6. oracle字符集的查看和修改

    Oracle修改字符集2.3oracle数据库的字符集更改 A.oracle server 端 字符集查询 select userenv(‘language’) from dual 其中NLS_CHA ...

  7. hdu 2086

    PS:推算...数组如果开得不够大也会超时... 代码: #include "stdio.h" double cal(int t,double a[]); int main(){ ...

  8. numpy中的broadcast

    关于broadcast,官方文档描述如下: Each universal function takes array inputs and produces array outputs by perfo ...

  9. error C3163: “_vsnprintf”: 属性与以前的声明不一致

    这是在vs2008中遇到的错误,vs2008以前没有,vs2008以后的vs也没有. c:\program files\microsoft visual studio 9.0\vc\include\s ...

  10. 极客DIY:RFID飞贼打造一款远距离渗透利器

    本文使用最新的渗透工具RFID飞贼(Tastic RFID Thief)和RFID感应破解技术来获取一些拥有安防的建筑物的访问权限. Tastic RFID Thief是一个无声远距离RFID读卡器, ...