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 Solved: 560
[Submit][Status][Discuss]
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.
题目大意:区间修改和区间查询
裸线段树毫无爆点
代码如下:
/**************************************************************
Problem: 3212
User: DaD3zZ
Language: C++
Result: Accepted
Time:48 ms
Memory:8304 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100001
long long sum[maxn<<2]={0},delta[maxn<<2]={0};
long long a[maxn]={0};
void updata(int now)
{
sum[now]=sum[now<<1]+sum[now<<1|1];
}
void build(int now,int l,int r)
{
if (l==r)
{
sum[now]=a[l];
return;
}
int mid=(l+r)>>1;
build(now<<1,l,mid);
build(now<<1|1,mid+1,r);
updata(now);
}
void pushdown(int now,int ln,int rn)
{
if (delta[now]!=0)
{
delta[now<<1]+=delta[now];
delta[now<<1|1]+=delta[now];
sum[now<<1]+=delta[now]*ln;
sum[now<<1|1]+=delta[now]*rn;
delta[now]=0;
}
}
void change(int L,int R,int now,int l,int r,long long data)
{
if (L<=l && R>=r)
{
sum[now]+=data*(r-l+1);
delta[now]+=data;
return;
}
int mid=(l+r)>>1;
pushdown(now,mid-l+1,r-mid);//这里需要先下放一波标记不然会出错
if (L<=mid)
change(L,R,now<<1,l,mid,data);
if (R>mid)
change(L,R,now<<1|1,mid+1,r,data);
updata(now);
}
long long query(int L,int R,int now,int l,int r)
{
if (L<=l && R>=r)
return sum[now];
int mid=(l+r)>>1;
pushdown(now,mid-l+1,r-mid);
long long total=0;
if (L<=mid)
total+=query(L,R,now<<1,l,mid);
if (R>mid)
total+=query(L,R,now<<1|1,mid+1,r);
return total;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1; i<=n; i++)
scanf("%lld",&a[i]);
build(1,1,n);
for (int i=1; i<=m; i++)
{
char c[3];
int l,r;
scanf("%s",&c);
scanf("%d%d",&l,&r);
if (c[0]=='Q')
{
long long ans=query(l,r,1,1,n);
printf("%lld\n",ans);
}
else
{
long long data;
scanf("%lld",&data);
change(l,r,1,1,n,data);
}
}
return 0;
}
BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询的更多相关文章
- bzoj 3212 Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Description You ...
- bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作
Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2173 Solved: ...
- 3212: Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1053 So ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- 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: 92632 ...
- poj 3468 A Simple Problem with Integers【线段树区间修改】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 79137 ...
- [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 55273 ...
随机推荐
- AC日记——搞笑世界杯 codevs 1060(dp)
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...
- java 20 - 9 带有缓冲区的字节输出流和字节输入流
由之前字节输入的两个方式,我们可以发现,通过定义数组读取数组的方式比一个个字节读取的方式快得多. 所以,java就专门提供了带有缓冲区的字节类: 缓冲区类(高效类) 写数据:BufferedOutpu ...
- linux上的常见命令掌握
http://coolshell.cn/articles/8883.html 这篇文章来源于Quroa的一个问答<What are some time-saving tips that ever ...
- for循环的三种写法
第一种写法 传统的方法,遍历数组 String[] arr = { "amy", "heinrich", "cindy", "g ...
- 08Mybatis_入门程序——增加用户的操作以及返回自增主键的功能以及返回非自增主键的功能
本文要实现的功能是:给user表增加一个用户. 建表如下:
- svn命令行修改已提交的版本备注
svn命令行修改已提交的版本备注 参考文章: stackoverflow.com/questions/304383/how-do-i-edit-a-log-message-that-i-already ...
- solaris之复习
1.在vmware 下面加一个硬盘 参考资料:http://bbs.51cto.com/thread-483151-1.html VMWare上solaris 加硬盘的方法(笨办法及新思路 ...
- Memcached通用类(基于enyim.com Memcached Client)
一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP.如下图: <?xml version="1.0"?> <configuratio ...
- 使用Spring.net中对Ado.net的抽象封装来访问数据库
使用Spring.net中对Ado.net的抽象封装来访问数据库 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入 ...
- 2015国产犯罪传记《暴力天使》HD720P.泰语中字
导演: 吴强编剧: 阮明玉主演: 张玉英 / 金理 / 至宝类型: 传记语言:泰语制片国家/地区: 中国大陆上映日期: 2016年3月25日片长: 92分钟又名: Huong Ga暴力天使的剧情简介 ...