【POJ 3468 A Simple Problem with Integers】 线段树
来源:互联网 发布:采购软件视频 编辑:程序博客网 时间:2024/06/05 17:14
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 115961 Accepted: 36028
Case Time Limit: 2000MS
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.
线段树 & 比较巧妙的是用了另一个数组记录节点区间的更新,查询从而更高效了
AC代码:
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 4e5 + 10;typedef long long LL;LL n,m,a[MAX],sum[MAX],x[MAX];LL init(int l,int r,int k){ if(l == r) return sum[k] = a[l]; int o = l + r >> 1; return sum[k] += init( l,o,k * 2) + init(o + 1,r,k * 2 + 1);}void ch(int L,int R,int l,int r,int k,int w){ if(l > R || r < L) return ; else if(l <= L && R <= r){ x[k] += w; return ; } int o = L + R >> 1; ch(L,o,l,r,k * 2,w),ch(o + 1,R,l,r,k * 2 + 1,w); sum[k] += (LL)(min(R,r) - max(L,l) + 1) * w;}LL qu(int L,int R,int l,int r,int k){ if(l > R || r < L) return 0; else if(l <= L && R <= r) return sum[k] + (LL)x[k] * (R - L + 1); int o = L + R >> 1; return qu(L,o,l,r,k * 2) + qu(o + 1,R,l,r,k * 2 + 1) + (LL)(min(R,r) - max(L,l) + 1) * x[k];}int main(){ scanf("%lld %lld",&n,&m); for(int i = 1; i <= n; i++) scanf("%lld",&a[i]); init(1,n,1); while(m--){ char s[2]; int l,r,o; scanf("%s %d %d",s,&l,&r); if(s[0] == 'C') scanf("%d",&o),ch(1,n,l,r,1,o); else printf("%lld\n",qu(1,n,l,r,1)); } return 0;}
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers (线段树)
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- poj 3468 A Simple Problem with Integers 基础线段树
- POJ 3468 A Simple Problem with Integers 线段树
- POJ 3468 A Simple Problem with Integers【线段树】
- 【线段树】北大 poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers 线段树!!!!
- poj 3468 A Simple Problem with Integers【线段树】
- poj 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers 线段树
- poj 3468 A Simple Problem with Integers[线段树 ]
- POJ 3468 A Simple Problem with Integers 线段树
- poj 3468 A Simple Problem with Integers(线段树区区)
- 线段树 POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers----线段树
- POJ 3468 A Simple Problem with Integers(线段树)
- poj-3468-A Simple Problem with Integers(线段树)
- Copying Books (二分)
- 心情
- Spring Cloud学习系列第二章:使用Feign调用服务
- 【算法系列】——Java实现插入&&希尔排序
- 如何在CSDN博客中的所贴的代码进行【代码块】显示
- 【POJ 3468 A Simple Problem with Integers】 线段树
- 杂谈
- poj 2566 Bound Found 尺取法 / lower_bound
- Django建教育平台(六)--首页和登录页面配置
- UVALive 6886 Golf Bot 桶排+剪枝
- CentOS 使用yum命令安装出现错误提示:Loaded plugins: fastestmirror, refresh-packagekit, security
- SpringBoot环境搭建eclipse版和IntelliJ IDEA版
- 各种排序
- 最大流增广路