A Simple Problem with Integers
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 77964 Accepted: 24012
Case Time Limit: 2000MS
Description
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
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly–2007.11.25, Yang Yi
线段树的区间查询与区间更新,lazy优化,不然会超时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
const int MAX = 110000;
struct node
{
LL lazy;
LL sum;
} Tree[MAX*12];
LL a[MAX];
void Build(int L,int R,int site)
{
if(L==R)
{
Tree[site].lazy=0;
Tree[site].sum=a[L];
return ;
}
Tree[site].lazy=0;
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
void update(int L,int R,int l,int r,int site,LL w)
{
if(L==l&&r==R)
{
if(L==R)
Tree[site].lazy=0;
else
Tree[site].lazy+=w;
Tree[site].sum+=((R-L+1)*w);
return ;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
}
Tree[site].sum+=((r-l+1)*w);
if(mid>=r)
{
update(L,mid,l,r,site<<1,w);
}
else if(l>mid)
{
update(mid+1,R,l,r,site<<1|1,w);
}
else
{
update(L,mid,l,mid,site<<1,w);
update(mid+1,R,mid+1,r,site<<1|1,w);
}
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
}
LL Query(int L,int R,int l,int r,int site)
{
if(L==l&&r==R)
{
return Tree[site].sum;
}
int mid=(L+R)>>1;
if(Tree[site].lazy!=0)
{
update(L,mid,L,mid,site<<1,Tree[site].lazy);
update(mid+1,R,mid+1,R,site<<1|1,Tree[site].lazy);
Tree[site].lazy=0;
}
if(mid>=r)
{
return Query(L,mid,l,r,site<<1);
}
else if(mid<l)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int n;
int Q;
char s[5];
int u,v;
LL w;
while(~scanf("%d %d",&n,&Q))
{
for(int i=1; i<=n; i++)
{
scanf("%I64d",&a[i]);
}
Build(1,n,1);
while(Q--)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d %d",&u,&v);
printf("%I64d\n",Query(1,n,u,v,1));
}
else
{
scanf("%d %d %I64d",&u,&v,&w);
update(1,n,u,v,1,w);
}
}
}
return 0;
}
A Simple Problem with Integers的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- Java基础之创建窗口——使用BoxLayout管理器(TryBoxLayout4)
控制台程序. javax.swing.BoxLayout类定义的布局管理器在单行或单列中布局组件.创建BoxLayout对象时,需要指定是在行还是列中布局组件. 对于行,组件是从左到右地添加:对于列, ...
- 给ubuntu系统换新装
此次安装主要按照Flatabulous:Ubuntu系统美化主题 但自己在安装过程中发现了很多问题,一下一段黑色的是上面链接的文章,红色的是自己在操作过程中的一些改动 安装主题的第一步是安装Ubunt ...
- redhat vim编辑器永久添加行号
cd ~ vim .vimrc 第一行加入: set nu :wq 保存退出,即可 如果想取消设置,同理删除set nu即可
- Leetcode: Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- how to use automapper in c#, from cf~
[DataContract] public class GroupDto { [DataMember] public int id { get; set; } [DataMember] public ...
- 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等
使用配置文件properties进行连接数据库 首先创建一个file自定义文件名,但是后缀名必须改为.properties(不分大小写):如config.properties: 然后双击config. ...
- java读properties的通用类,兼容linux和windows
package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...
- Android activity的回传数据
package com.example.myact3; import android.content.Intent; import android.os.Bundle; import android. ...
- js高级程序设计笔记之-addEventListener()与removeEventListener(),事件解除与绑定
js高级程序设计笔记之-addEventListener()与removeEventListener(),事件解除与绑定 addEventListener()与removeEventListener( ...
- java 字符串转json,json转对象等等...
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...