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 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...
随机推荐
- bootstrap学习笔记<四>(table表格)
表格 bootstrap为table表格定制多个常用样式:基本样式,隔行变色样式,带边框样式,荧光棒样式,紧凑样式,响应样式. ☑ .table:基础表格 ☑ .table-striped:斑马线 ...
- django的安装和搭建
一.先下载pyton,配置下python的环境变量,这个很重要,然后下载django,解压到与python同一个根目录底下,进入django目录,运行python setup.py install安装 ...
- phpStorm如何在Console控制台执行php文本,而不是浏览器中
如何在Console控制台执行php文本 phpStorm默认会在浏览器中执行脚本 默认的配置 配置PHP脚本 扩展,配置项目运行
- hdu 1573 X问题
数论题,本想用中国剩余定理,可是取模的数之间不一定互质,用不了,看到网上有篇文章写得很好的:数论——中国剩余定理(互质与非互质),主要是采用合并方程的思想: 大致理解并参考他的代码后便去试试hdu上这 ...
- eclipse_中的注释_快捷键
eclipse 中的注释 快捷键 把要注释的代码选中,按Ctrl+Shift+/ /* */ 形式的 ctrl+/ //形式的 取消代码注释: 把要注释的代码选中,按Ctrl+Shift+\ /* ...
- 【转】java多态详解
1. Java中除了static和final方法外,其他所有的方法都是运行时绑定的.private方法都被隐式指定为final的,因此final的方法不会在运行时绑定.当在派生类中重写基 ...
- SVN使用汇总
SVN项目管理文件夹:Tag/Branch/Trunk Trunk:在我经历的开发中,新建Trunk意味着对旧Trunk的一个保留,同时在新的Trunk中可以进行新功能的开发及对已有功能进行完善: B ...
- visual studio 自带单元测试demo
0) 创建类,编写方法类1) 在方法点击鼠标右键,在运行测试(T)和调试测试(D)之间会有一个 <创建单元测试>选项.有则进入2,没有则看1.01.0) 菜单栏 工具-->自定义-- ...
- Rocketmq-尝试理解
普通的信息发送和消费 首先要启动nameserver和broker,nameserver是一个几乎无状态节点.broker分为master和slave,master和slave的对应关系通过指定相同的 ...
- redirect模块的秘密
所有的redirect记录都在config/url_directs下面, 但是某个node/edit的redirect记录只包含redirect到自己的记录,且不验证url的合理性. 现在比如,a跳转 ...