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 ...
随机推荐
- nginx安全日志分析脚本的编写
https://blog.csdn.net/nextdoor6/article/details/51914966
- 陕西师范大学第七届程序设计竞赛网络同步赛 I 排队排队排队【数组任一位可以移动到队头,最少移动几次增序/数组指针操作】
链接:https://www.nowcoder.com/acm/contest/121/I来源:牛客网 题目描述 ACM竞赛队内要开运动会啦!!!! 竞赛队内的一群阳光乐观积极的队员们迅速的在操场上站 ...
- 51nod 1007 正整数分组【01背包变形】
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...
- spark-join算子
- 树链剖分【P3833】 [SHOI2012]魔法树
Description Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点,每个节点u的 ...
- 分析dump
一.使用jmap工具生成dump文件 二.MAT工具的下载和安装 三.使用MAT工具进行内存泄露分析 -- Step 1 : ps –ef | grep <process> (whic ...
- POJ 3735 Training little cats(矩阵乘法)
[题目链接] http://poj.org/problem?id=3735 [题目大意] 有一排小猫,给出一系列操作,包括给一只猫一颗花生, 让某只猫吃完所有的花生以及交换两只猫的花生, 求完成m次操 ...
- 动态路由协议(2)--rip
1.设置pc ip 网关 192.168.1.1 192.168.1.254 192.168.4.1 192.168.4.254 2.设置路由器 (1)设置接口ip Router(config-/ R ...
- JAVA常见算法题(二十四)
package com.xiaowu.demo; //一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Demo24 { public ...
- JAVA常见算法题(二十三)
package com.xiaowu.demo; /** * 给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. * * * @author WQ * */ public clas ...