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 ...
随机推荐
- 天平 (Not so Mobile UVA - 839)
题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...
- c# html 导出word
[CustomAuthorize] public FileResult ExportQuestionCenterWord(SearchBaseQuestion search) ...
- StrBlobPtr类——weak_ptr访问vector元素
#include <iostream> #include <memory> #include <string> #include <initializer_l ...
- 美美哒rand()函数
2016.3.7 天气:大雪都已经三月份竟然还下了这么大的雪,真是少见呀.今天为了提交软件工程的作业我们需要注册git的账号,真是前途艰难呀,后台服务器都要爆炸了,其实我觉得这个平台的服务器 ...
- C#2d命令行小游戏
[ 星 辰 · 第 二 条 约 定 ] 要求 空地:空格 | 边界/墙:'█' | 人物:'♜' 实现人物的上下左右移动 记录关系图.流程图.设计过程遇到的问题及解决 项目压缩包 [项目源码](htt ...
- 【week2】 累计进度条、psp、饼图
每周例行报告 本周PSP 类别 任务 开始时间 结束时间 被打断时间 总计工作时间 2016年9月9日 读书 构建之法-5.6章 19:00 20:00 0 60min 2016年9月10日 看博客 ...
- Httpd主配置文件httpd.conf 详解
Apache的主配置文件:/etc/httpd/conf/httpd.conf默认站点主目录:/var/www/html/Apache服务器的配置信息全部存储在主配置文件/etc/httpd/conf ...
- netem设置了网卡的流量控制,为啥发包的延迟就搞不定呢?
为啥我用netem做了一个流量的控制 但是发送的时候,感觉真正发送数据的时候还是没有达到每一个数据包都是1s的延迟呀,这里的1s的延迟是啥意思啊? 这里的delay并不是说每个数据包都delay 5s ...
- Maven pom 文件解释
1 - 什么是构建? 我们都知道,写完代码之后需要进行编译和运行,以笔者自身为例,使用 Eclipse 写完代码,需要进行编译,再生成 war 包,以便部署到 Tomcat. 在编写 Java 代码的 ...
- 【Python】python 调用c语言函数
虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了具体流程:c编写相关函数 ,编译成库然后在python中加载这些库,指定调用函数.这些函数 ...