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 ...
随机推荐
- 175. Invert Binary Tree【LintCode by java】
Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 解题:题目要求讲二叉树的左子树和右子树对调 ...
- 【shell 练习3】用户管理脚本(一)
一.创建十个用户,密码为八位 [root@localhost ~]# cat UserManger02.sh #!/bin/bash . /etc/init.d/functions [ $UID -n ...
- Shell 常用命令、基本用法总结
Filter Filter 常用于从大量文本.数据中提取需求的部分.下面介绍几个常用的 filter 命令. cut $ cut -c 5-8 textfile.txt # 切出 textfile.t ...
- LeetCode 108——将有序数组转化为二叉搜索树
1. 题目 2. 解答 一棵高度平衡的二叉搜索树意味着根节点的左右子树包含相同数量的节点,也就是根节点为有序数组的中值. 因此,我们将数组的中值作为根节点,然后再递归分别得到左半部分数据转化的左子树和 ...
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
- Alpha阶段中间产物
空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...
- 针对“来用”团队项目之NABC分析
本项目特点之一:扩展性强 NABC分析: N(need):我们这个开发的这个软件主要是集娱乐软件和实用工具于一身的大容器,这里面有很多应用程序,针对不同用户需要,至少有一款应用程序能够满足用户的需要, ...
- lintcode-143-排颜色 II
143-排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 注意事项 You are not ...
- TCP系列09—连接管理—8、TCP Reset
我们在介绍TCP头的时候,提到过其中有个RST标志位.当一个TCP报文中这个标志位打开的时候,我们叫做reset包(严格的说应该叫做reset段,但是很多时候段包帧并不加以区分)或者简单称呼为rese ...
- 关于new delete的说明
1. 删除空指针不会有问题,因为C++的标准规定在delete时首先会判断指针是否为空,为空就不再处理,所以也就不会有问题. 2. delete一个非空指针之后,并不会将该指针自动置为空.此时如果重复 ...