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. 七、context command

    context command是用来新建自己的工具,可以调用OPENGL,获取鼠标操作函数,在view窗口画自己想画的东西.(我是这麽理解的,可以以后再确定一下) 下面是一个context comma ...

  2. [oracle] update和merge语句的几点写法

    1.update t2 set parentid=(select ownerid from t1 where t1.id=t2.id); 2. update tb_client_win_lost_re ...

  3. cookie sessionStorage localStorage 区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  4. 为什么C++中空类和空结构体大小为1?(转载)

    原文链接:http://www.spongeliu.com/260.html 对于结构体和空类大小是1这个问题,首先这是一个C++问题,在C语言下空结构体大小为0(当然这是编译器相关的).这里的空类和 ...

  5. 创建一个swift项目

    笔者认为你已经有了oc的开发基础,流程是一样的,选择开发语言为swift即可.这里主要说明一下一些常用的配置: 一般我们不使用XIB和storyboard,所以在系统自动创建的文件中删除main.st ...

  6. PHP时间日期

    PHP常用的几个时间日期函数有:date(),mktime(),strtotime(); 一.string date ( string $format [, int $timestamp ] ) 函数 ...

  7. javascript的replace+正则 实现ES6的字符串模版

    采用拼接字符串的形式,将 JSON 数据嵌入 HTML 中.开始时代码量较少,暂时还可以接受.但当页面结构复杂起来后,其弱点开始变得无法忍受起来: 书写不连贯.每写一个变量就要断一下,插入一个 + 和 ...

  8. RAD 版本迁移工具,不怕升级麻烦了。

    RAD 版本迁移工具,不怕升级麻烦了. http://community.embarcadero.com/blogs?view=entry&id=8865 migrationtool.exe ...

  9. CSS3基础01

    一.选择器: 分为关系选择器 ,属性选择器 ,伪类选择器 1.1关系选择器  后代选择器   ul li  选择所有的后代元素 子代选择器   ul > li 选择ul的儿子 紧邻选择器  .b ...

  10. js 模块开发之一(模块开发价值)

    首先引用我们的今天的主角 ----<前端模块化开发的价值> 1,前端开发最常见的两个问题 ---命名冲突和文件依赖 2,对于命名冲突的基本解决办法就是学习其他语言的习惯,添加命名空间 va ...