bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作
Pku3468 A Simple Problem with Integers
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 2173 Solved: 951
[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
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
55
9
15
裸题
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio> #define N 100007
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,q;
char ch[]; #define ls p<<1
#define rs p<<1|1
struct seg
{
ll sum[N<<],add[N<<];
void build(int p,int l,int r)
{
if (l==r)
{
sum[p]=read();
return;
}
int mid=(l+r)>>;
build(ls,l,mid),build(rs,mid+,r);
sum[p]=sum[ls]+sum[rs];
}
void push_down(int p,int l,int r)
{
if (!add[p]) return;
ll ad=add[p],mid=(l+r)>>;add[p]=;
sum[ls]+=1ll*(mid-l+)*ad;
sum[rs]+=1ll*(r-mid)*ad;
add[ls]+=ad,add[rs]+=ad;
}
ll query(int p,int l,int r,int x,int y)
{
if (l==x&&y==r) return sum[p];
push_down(p,l,r);
int mid=(l+r)>>;
if (y<=mid) return query(ls,l,mid,x,y);
else if (x>mid) return query(rs,mid+,r,x,y);
else return query(ls,l,mid,x,mid)+query(rs,mid+,r,mid+,y);
}
void modify(int p,int l,int r,int x,int y,int z)
{
sum[p]+=1ll*(y-x+)*z;
if (l==x&&y==r)
{
add[p]+=z;
return;
}
int mid=(l+r)>>;
if (y<=mid) modify(ls,l,mid,x,y,z);
else if (x>mid) modify(rs,mid+,r,x,y,z);
else modify(ls,l,mid,x,mid,z),modify(rs,mid+,r,mid+,y,z);
}
}seg;
#undef ls
#undef rs
int main()
{
n=read(),q=read();
seg.build(,,n);
for (int i=;i<=q;i++)
{
scanf("%s",ch);
if (ch[]=='Q')
{
int x=read(),y=read();
printf("%lld\n",seg.query(,,n,x,y));
}
else
{
int x=read(),y=read(),z=read();
seg.modify(,,n,x,y,z);
}
}
}
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 ...
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- 3212: Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1053 So ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- python程序设计——面向对象程序设计:类
理解面向对象 基本原则是,计算机程序由多个能够起到子程序作用的单元或对象组合而成 关键性观念是,数据以及对数据的操作封装在一起,组成一个相互依存.不可分割的整体,即对象 python面向对象特性 完全 ...
- Python变量常量及注释
一.变量命名规则1.有字母.数字.下划线搭配组合而成2.不能以数字开头,更不能全为数字3.不能用Python的关键字4.不要太长5.名字要有意义6.不要用中文7.区分大小写8.采用驼峰体命名(多个单词 ...
- Juice账号
zhangxiaocong69 zxc6545398 15657167502 区块链账户: 0x00680404766965143796a0a070835c3cdf9a4a50
- SpringBoot在IntelliJ IDEA下for MAC 热加载
说在前面 热加载:文件内容变更服务器自动运行最新代码.实则在IDEA环境进行热部署后,下述过程一气呵成. 1代码变更,文件自动保存(IDEA自动保存代码,用户无需使用COMMAND+SAVE快捷键): ...
- [c++] Getting Started - CMake
CMake is an open-source cross platform build system, according to CMake's creator, Kitware. But CMak ...
- “今日校园” App 用户体验分析
一.背景 为进一步提升信息化应用水平,更好的服务师生,南通大学智慧校园移动端APP“今日校园”定于11月5日正式上线运行.登陆APP可浏览学校新闻.校园生活.各部门微信公众号等内容,查看校内通知.校内 ...
- java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决
我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.
- MongoDb Driver For Net
由于mongodb开源github提供的net驱动都比较新,从2.3及以上版本都是netcore系列了,netframework至少都是4.6以上,且提供的dll并没有签名, 这就产生了一些问题 1 ...
- Qt-排序
1.要求传入起始指针,总长度,单元素空间占用大小(sizeof(A[i])),判断函数. 判断函数参数类型为const void *,使用需要在函数内自行转换为对应类型, 返回值为整数型,升序排序时正 ...
- .NET环境下,通过LINQ操作SQLite数据库
//对应数据库中的某个表 [Table(Name = "main.Student")] public class Student { [Column(Na ...