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 abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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.
 
 
这是线段树模板题,段更新,有一个向下压的过程。
 
 
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 100100
#define Lson r<<1
#define Rson r<<1|1 struct node
{
int L,R;
long long sum,e;
int mid()
{
return (L+R)/; }
int len()
{
return R-L+;
}
}a[N<<]; void BuildTree(int r,int L,int R)
{
a[r].L=L;
a[r].R=R;
a[r].e=;
if(L==R)
{
scanf("%lld",&a[r].sum);
return;
}
BuildTree(Lson,L,a[r].mid());
BuildTree(Rson,a[r].mid()+,R);
a[r].sum=a[Lson].sum+a[Rson].sum;
}
void Down(int r)
{
a[Lson].sum+=a[Lson].len()*a[r].e;
a[Lson].e+=a[r].e;
a[Rson].sum+=a[Rson].len()*a[r].e;
a[Rson].e+=a[r].e;
a[r].e=;
}
void Add(int r,int L,int R,int e)
{
a[r].sum+=(R-L+)*e;
if(a[r].L==L && a[r].R==R)
{
a[r].e+=e;
return;
}
Down(r);
if(R<=a[r].mid())
Add(Lson,L,R,e);
else if(L>a[r].mid())
Add(Rson,L,R,e);
else
{
Add(Lson,L,a[r].mid(),e);
Add(Rson,a[r].mid()+,R,e);
}
}
long long Qurry(int r,int L,int R)
{
if(a[r].L==L && a[r].R==R)
{
return a[r].sum;
}
Down(r);
if(R<=a[r].mid())
return Qurry(Lson,L,R);
else if(L>a[r].mid())
return Qurry(Rson,L,R);
else
{
long long int a1=Qurry(Lson,L,a[r].mid());
long long int a2=Qurry(Rson,a[r].mid()+,R);
return a1+a2;
}
} int main()
{
int n,m,d,b,c;
while(scanf("%d %d",&n,&m)!=EOF)
{
BuildTree(,,n);
char s[];
while(m--)
{
scanf("%s",s);
if(s[]=='Q')
{
scanf("%d %d",&d,&b);
printf("%lld\n",Qurry(,d,b));
}
else
{
scanf("%d %d %d",&d,&b,&c);
Add(,d,b,c);
}
}
}
return ;
}

A Simple Problem with Integers-POJ3468的更多相关文章

  1. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  2. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

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

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

  4. poj------(3468)A Simple Problem with Integers(区间更新)

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

  5. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

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

  6. POJ3468:A Simple Problem with Integers(线段树模板)

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

  7. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. POJ 3468 A Simple Problem with Integers (splay tree入门)

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

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

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

  10. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

随机推荐

  1. jquery readio checked

    今天太鬼火了为这个难问题搜了一下午了到最后还是csdn的朋友给了我正确的答案http://bbs.csdn.net/topics/300162450谢谢这位朋友 // $("#ISOK1&q ...

  2. Spring Cloud是什么?

    [学习笔记] 3)Spring Cloud是什么?马克-to-win@马克java社区:i)Spring Cloud是一个微服务框架,Spring Cloud基于微服务基础框架Netflix进行了up ...

  3. CF949A/950C Zebras

    思路: 贪心乱搞. 实现: #include <bits/stdc++.h> using namespace std; vector<vector<int>> v; ...

  4. 白话容器namespace

    进入正题之前是例行装X环节: 过年7天吃的,花了45天又回来了. 近年来容器大火,也正是因为容器,生生灭掉了一个IT岗位!哥也是被生生的逼上了邪路. 那究竟什么是容器呢? 就三个字:它就是个进程!(多 ...

  5. 对gridview绑定数据的操作方法及自定义显示内容

    GridView中Eval和 Bind 的使用 Eval:绑定的是只读数据的显示:Bind:可以绑定只读数据也可以绑定更新数据,Bind方法还把字段和控件的绑定属性联系起来,使得 数据控件(比如Gri ...

  6. InChatter系统之客户端消息处理中心

    一.模块结构 首先来看下客户端消息处理中心模块的简单结构: ChatCallback:服务器端我们定义的回调接口IChatCallback的客户端实现 ChatMsgCenter:服务端的消息处理中心 ...

  7. JavaScript——responseType

    https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...

  8. PowerDesigner 操作手册

    1.错误信息:Generation aborted due to errors detected during the verification of the model 解决方案: 把检查模型的选项 ...

  9. apropos - 在 whatis 数据库中查找字符串

    总览 (SYNOPSIS) apropos keyword ... 描述 (DESCRIPTION) apropos 命令在一些特定的包含系统命令的简短描述的数据库文件里查找关键字, 然后把结果送到标 ...

  10. vsphere中的vcenter创建esxi模板虚拟机新建无法连接网络

    1.删除网卡配置文件下的uuid和hwaddr  这是因为虚拟机模板创建网卡mac没改变 2.删除规则文件 rm -f /etc/udev/rules.d/-prtsistent-net.rules ...