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

描述

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.

输入

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.

输出

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


前几次提交的时候没有吧node[u].add清零,一直WA
第一道线段树的题,当晚没有调出来,第二天才想出来,不错
 
 
#include<cstdio>
#include<iostream>
#define LL long long
#define R(u) (u<<1|1)
#define L(u) (u<<1)
using namespace std;
const int maxx=100005;
LL a[maxx];
int n,m;
struct Node{
int r,l;
LL add,sum;
}node[maxx<<2];
void Pushup(int u)
{
node[u].sum=node[L(u)].sum+node[R(u)].sum;
return;
}
void Pushdown(int u)
{
node[L(u)].add+=node[u].add;
node[R(u)].add+=node[u].add; node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add; node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
node[u].add=0; //一定记得清零
}
void Build(int u,int left,int right)
{
node[u].l=left,node[u].r=right;
node[u].add=0;
if(left==right)
{
node[u].sum=a[left];
return;
}
int mid=(left+right)>>1;
Build(L(u),left,mid);
Build(R(u),mid+1,right);
Pushup(u);
}
void update(int u,int left,int right,LL val)
{
if(left==node[u].l&&node[u].r==right)
{
node[u].add+=val;
node[u].sum+=(node[u].r-node[u].l+1)*val;
return;
}
node[u].sum+=(right-left+1)*val;// 当更新区间小于这段
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) update(L(u),left,right,val);//在左边
else if(mid<left) update(R(u),left,right,val);
else {
update(L(u),left,mid,val);
update(R(u),mid+1,right,val);
}
//Pushup(u);前面已经直接算出sum后面不用再Pushup了
}
LL Qurey(int u,int left,int right)
{
if(left==node[u].l&&node[u].r==right)
return node[u].sum;
if(node[u].add)Pushdown(u);
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) Qurey(L(u),left,right);
else if(mid<left) Qurey(R(u),left,right);
else return (Qurey(L(u),left,mid)+Qurey(R(u),mid+1,right));
//Pushup(u);
}
int main()
{
cin>>n>>m;
LL c;
for(int i=1;i<=n;i++)
scanf("%I64d",a+i);
Build(1,1,n);
while(m--)
{char x;int ai,an;
scanf("%c %d %d",&x,&ai,&an);
cin>>x>>ai>>an;
if(x=='C')
{
scanf("%I64d",&c);
update(1,ai,an,c);
}
else
printf("%I64d\n",Qurey(1,ai,an));
}
return 0;
}

  

poj3468 A Simple Problem with Integers (线段树区间最大值)的更多相关文章

  1. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  2. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

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

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

  4. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  5. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  6. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

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

  8. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

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

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

随机推荐

  1. Bellman-Ford最短路径

    对于前面说到的最短路径的求解方法,不能解决负权边的情况,而Bellman-Ford却可以 共有n个顶点,m条边,每次输入u[i],v[i],w[i],代表从u[i]到v[i]的距离是w[i],对于所有 ...

  2. C++去掉字符串中首尾空格和所有空格

    c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧. 去掉首尾空格的代码如下: void trim(string &s) { if( !s.empty() ) { s.erase ...

  3. 【Python】 sorted函数

    我们需要对List.Dict进行排序,Python提供了两个方法对给定的List L进行排序,方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副本方法2.用built-in函数so ...

  4. http://www.cnblogs.com/20135131zxy/

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 二.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门( ...

  5. Hadoop HDFS编程 API入门系列之从本地上传文件到HDFS(一)

    不多说,直接上代码. 代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs5; import java.io.IOException; import ja ...

  6. weed-fs 压力测试

    阅读<Weed-FS/杂草文件系统 小文件存储集群 安装 使用 测试>中提到weedfs的负载压力不是很好,在看过代码后进行了相应测试,未发现负载压力有何问题.   weedfs mast ...

  7. flex中通过sprite在地图上画柱状图主要代码

    1.主要代码: var sprite:Sprite = new Sprite();     var columnSys:ColumnSymbol = new ColumnSymbol();     v ...

  8. ubuntu 14 配置JDK

    1. 下载JDK http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html 下载后的保存地址: /home/root1 ...

  9. Javascript函数、构造函数、原型、类和对象

    函数 函数是JavaScript中特殊的对象,对函数执行typeof运算会返回字符串"function",因为函数也是对象,他们可以拥有属性和方法. 静态方法 函数在JS中定义了类 ...

  10. Java NIO教程 Channel

    Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...