POJ 3468 区间更新,区间求和(经典)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 72265 | Accepted: 22299 | |
| 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
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n; struct node{
int l, r;
__int64 val, sum;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=;
if(l==r){
scanf("%I64d",&a[root].sum);
return;
}
build(l,mid,ll);
build(mid+,r,rr);
a[root].sum=a[ll].sum+a[rr].sum;
} void down(int root){
if(a[root].val&&a[root].l!=a[root].r) {
a[ll].val+=a[root].val;
a[rr].val+=a[root].val;
a[ll].sum+=(__int64)(a[ll].r-a[ll].l+)*a[root].val;
a[rr].sum+=(__int64)(a[rr].r-a[rr].l+)*a[root].val;
a[root].val=;
}
} void update(int l,int r,__int64 val,int root){
if(a[root].l==l&&a[root].r==r){
a[root].val+=val;
a[root].sum+=(__int64)(a[root].r-a[root].l+)*val;
return;
}
down(root);
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else {
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
a[root].sum=a[ll].sum+a[rr].sum;
} __int64 query(int l,int r,int root){
if(a[root].l==l&&a[root].r==r){
return a[root].sum;
}
down(root);
if(r<=a[ll].r) return query(l,r,ll);
else if(l>=a[rr].l) return query(l,r,rr);
else return query(l,mid,ll)+query(mid+,r,rr);
} void out(int root){
if(a[root].l==a[root].r) {
printf("%I64d ",a[root].sum); return;
}
down(root);
out(ll);
out(rr);
}
main()
{
int i, j, k;
int q;
while(scanf("%d %d",&n,&q)==){
build(,n,);
char s[];
__int64 w;
int l, r;
while(q--){
scanf("%s",s);
if(strcmp(s,"Q")==){
scanf("%d %d",&l,&r);
printf("%I64d\n",query(l,r,));
}
else{
scanf("%d %d %I64d",&l,&r,&w);
update(l,r,w,);
// out(1);
}
}
}
}
POJ 3468 区间更新,区间求和(经典)的更多相关文章
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu6070(分数规划/二分+线段树区间更新,区间最值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- poj 3468 线段树区间更新/查询
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树
区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...
- 线段树区间更新+区间求和模板(数组实现)洛谷p3372,p3373
模板题目1:https://www.luogu.org/problemnew/show/P3372 懒惰标记讲解:https://www.cnblogs.com/wushengyang/p/11194 ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- NBOJv2 1004 蛤玮打扫教室(线段树区间更新区间最值查询)
Problem 1004: 蛤玮打扫教室 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %l ...
- 【DFS序+线段树区间更新区间求最值】HDU 5692 Snacks
http://acm.hdu.edu.cn/showproblem.php?pid=5692 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...
随机推荐
- UIButton(在代码中使用)
- (void)viewDidLoad { [super viewDidLoad]; // 1.1 创建按钮对象 // UIButton *button = [[UIButton alloc] ini ...
- hdu5823(反演dp)
听说3^n也能水过去.. 其实应该是个经典题,求图染色这个np问题. 把问题拆成独立集来进行dp可以在3^n之内水过去. 拆成独立集的时候就发现,等价与一个经典的反演dp问题 然后复杂度就变成了 n* ...
- C# Socket编程(4)初识Socket和数据流
经过前面基础知识作为背景,现在对Socket编程进行进一步的学习.在System.Net.Socket命名空间提供了Socket类,利用该类我们可以直接编写Socket的客户端和服务的的程序.但是直接 ...
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
- [ 转]Android快速开发–使用ORMLite操作数据库
OrmLite是一个数据库操作辅助的开源框架,主要面向Java语言.在Android面向数据库开发中,是一个比较流行的开源框架,方便操作而且功能强大,今天来学习一下,最近的项目中也有所涉及,写个博客来 ...
- Hive优化
hive.optimize.cp=true:列裁剪hive.optimize.prunner:分区裁剪hive.limit.optimize.enable=true:优化LIMIT n语句hive.l ...
- java static 关键字
static 修饰成员函数:(静态函数) 1)静态函数可以用类名和对象进行调用. 2)直接访问静态成员,但不能访问非静态成员变量. 3)非静态函数可以直接访问静态与非静态的成员.(非静态函数只能由对象 ...
- 能源项目xml文件标签释义--default-lazy-init
1.spring的default-lazy-init参数 spring在启动的时候,会默认加载会默认加载整个对象实例图,从初始化ACTION配置.到 service配置到dao配置.乃至到数据库连接. ...
- Bootstrap标签
Bootstrap是Twitter推出的一个用于前端开发的开源工具包.用外链加载的方式可以将Bootstrap链接进来 常用方式: <link rel="stylesheet" ...
- Qt之QSequentialAnimationGroup
简述 QSequentialAnimationGroup类提供动画的串行组. QSequentialAnimationGroup是一个串行运行动画的QAnimationGroup,在另一个动画播放结束 ...