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 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...
随机推荐
- css sprites-简单实例让你快速掌握css sprites精髓
这段时间有几次接触到了css sprites的概念,一个就是在用css做滑动门的时候,另外一个就是在用YSlow分析网站性能的时候,于是对css sprites这个概念产生了浓厚的兴趣.在网上查找了很 ...
- linux清空文件等有用的指令
1). > filename 2). :> filename 3). echo "" > filename (文件大小被截为1字节) 4). ...
- AutoCompleteTextView控件的使用
public class MainActivity extends Activity { //[0]声明AutoCompleteTextView要显示的数据 private static final ...
- iOS开发之APP推送设置WIFI
在iOS开发过程中,有时需要连接网络.当访问请求,检测到网络不可用时,需要提示用户手动进行设置网络并告知用户操作路径设置可用的网络. 只需一行代码即可实现: - (void)viewDidLoad { ...
- C++时间函数模板
//测时间 class Timer { private: clock_t _start; clock_t _end; public: Timer() { start(); } void start() ...
- Mvc4_@RenderBody()和@RenderSection()
@RenderBody():呈现子页的主体内容 @RenderSection():呈现特别的节部分. HelperResult RenderSection(string name, bool requ ...
- ubuntu server nginx 安装与配置
ubuntu server nginx 安装与配置 一:关于nginx http://wiki.ubuntu.org.cn/Nginx http://nginx.org/cn http://wiki. ...
- as3中的多线程
从fp11.4开始支持worker技术, 即as3中的线程概念, 到了fp11.5, flascc中开始支持pthread家族来创建线程. 总的来说, as3中有两种创建线程的方法: 1.直接在as3 ...
- IIS 安装 pydio
Introduction In this how-to, we’ll see the installation of Pydio (Put Your Data In Orbit), which is ...
- ArcMap上发布地图服务前,“将图形转为要素的选项”时报“输出名称无效”错误
发布ArcMap服务时,由于矢量图中包含“文本标注”. 发布矢量图服务时,报了一个“00017: 数据框中至少有一个包含图形的已启用注记组”的错误,如下图: 官网给出的解决办法如下:http://re ...