poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目: id=3468" target="_blank">poj 3468 A Simple Problem with Integers
题意:给出n个数。两种操作
1:l -- r 上的全部值加一个值val
2:求l---r 区间上的和
分析:线段树成段更新,成段求和
树中的每一个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每一个值要加的值
对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val 。
以下的区间标记num += val
对于求和操作。每次进行延迟更新。把num值分别更新到两个子区间。sum值更新,然后num值变为0
注意:这个题目会超int 。
所以
AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N = 110000;
const long long inf = 0x3f3f3f3f;
struct Node
{
long long l,r;
long long num,sum;
};
Node tree[5*N];
long long a[N];
long long cnt ;
void build(long long o,long long l,long long r)
{
tree[o].l = l,tree[o].r = r;
tree[o].num = 0;
if(l==r)
{
tree[o].sum = a[cnt++];
return ;
}
long long mid = (l+r)/2;
build(o+o,l,mid);
build(o+o+1,mid+1,r);
tree[o].sum = tree[o+o].sum + tree[o+o+1].sum;
}
void push_update(long long o)
{
if(tree[o].num!=0)
{
tree[o].sum += tree[o].num * (tree[o].r-tree[o].l+1);
tree[o+o+1].num += tree[o].num;
tree[o+o].num += tree[o].num;
tree[o].num = 0;
}
}
void update(long long o,long long l,long long r,long long val)
{
if(l==tree[o].l && r == tree[o].r)
{
tree[o].num += val;
return ;
}
tree[o].sum += (val*(r-l+1)); //维护前面的
long long mid = (tree[o].l+tree[o].r) / 2;
if(r<=mid)
update(o+o,l,r,val);
else if(l>mid)
update(o+o+1,l,r,val);
else
{
update(o+o,l,mid,val);
update(o+o+1,mid+1,r,val);
}
}
long long query(long long o,long long l,long long r)
{
if(tree[o].l==l && tree[o].r == r)
{
return tree[o].sum+(r-l+1)*tree[o].num;
}
push_update(o); //维护后面的
long long mid = (tree[o].l+tree[o].r)/2;
if(r<=mid)
return query(o+o,l,r);
else if(l>mid)
return query(o+o+1,l,r);
else
return query(o+o,l,mid) + query(o+o+1,mid+1,r);
}
int main()
{
//freopen("Input.txt","r",stdin);
long long n,m;
while(~scanf("%lld%lld",&n,&m))
{
cnt = 0;
for(long long i=0;i<n;i++)
scanf("%lld",&a[i]);
build(1,1,n);
while(m--)
{
getchar();
char c;
long long x,y;
scanf("%c",&c);
scanf("%lld%lld",&x,&y);
//printf("*****************************************\n");
if(c=='Q')
{
printf("%lld\n",query(1,x,y));
}
else
{
long long val;
scanf("%lld",&val);
update(1,x,y,val);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers 【线段树-成段更新】的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
随机推荐
- C# api基础1
1.创建一个简单的api 目录结构 再创建一个controller用来测试 public class DefaultController : ApiController { public string ...
- HDU 1426 Sudoku Killer (回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...
- Codeforces 1025D Recovering BST
这个题被wa成傻逼了.... ma[i][j]表示i,j能不能形成一条直接作为排序二叉树的边,n^3更新维护ma即可,按说应该是要爆复杂度的,数据玄学吧.. #include<iostream& ...
- What makes grep consider a file to be binary?
grep -a worked for me: $ grep --help [...] -a, --text equivalent to --binary-files=text
- 解密Java内存溢出之持久代
垃圾回收是Java程序员了解最少的一部分.他们认为Java虚拟机接管了垃圾回收,因此没必要去担心内存的申请,分配等问题.但是随着应用越来越复杂,垃圾回收也越来越复杂,一旦垃圾回收变的复杂,应用的性能将 ...
- Maximum Size Subarray Sum Equals k -- LeetCode
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- 数论day2——离散对数、元根
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=60802734 1 离散对数 离散对数定义 大步小 ...
- 【bzoj4403】【序列统计】不降转升+组合数添项合并
(上不了p站我要死了,侵权度娘背锅) Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input ...
- linux-配置字符串-grep
grep -rn "hello,world!" * * : 表示当前目录所有文件,也可以是某个文件名 -r 是递归查找 -n 是显示行号 -R 查找所有文件包含子目录 -i 忽略大 ...
- tmux用法
列出所有的tmux session,一个session是多个窗口的集合 tmux list-session 创建tmux窗口, tmux new -s server server为tmux的sess ...