首页 技术 正文
技术 2022年11月10日
0 收藏 622 点赞 5,122 浏览 1798 个字

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 110077   Accepted: 34272
Case Time Limit: 2000MS

Description

You have N integers, A1A2, … , 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 A1A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of AaAa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of AaAa+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
题意:n个数字,q次操作,每次可以区间增减,或者查询区间的和
 #include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXN 100100
#define LL long long
LL sum[MAXN<<];
LL add[MAXN<<];
int n,q;
char s[];
void putup(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void putdown(int rt,int m)
{
if (add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
sum[rt<<] += (m-(m>>))*add[rt];
sum[rt<<|] += (m>>)*add[rt];
add[rt] = ;
}
}
void build(int l,int r,int rt)
{
add[rt] = ;
if (l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
putup(rt);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if (L<=l && r<=R)
{
add[rt] += c;
sum[rt] += (LL)c*(r-l+);
return ;
}
putdown(rt,r-l+);
int m = (l+r)>>;
if (L<=m) update(lson,L,R,c);
if (R>m) update(rson,L,R,c);
putup(rt);
}
LL query(int l,int r,int rt,int L,int R)
{
if (L<=l && r<=R)
{
return sum[rt];
}
putdown(rt,r-l+);
LL ret = ;
int m = (l+r)>>;
if (L<=m) ret += query(lson,L,R);
if (R>m) ret += query(rson,L,R);
return ret;
}
int main()
{
scanf("%d%d",&n,&q);
build(,n,);
while (q--)
{
int x,y,z;
scanf("%s",s);
if (s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(,n,,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(,n,,x,y));
}
}
return ;
}
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297