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 ...
随机推荐
- Android 利用 AsyncTask 异步读取网络图片
1.新建Android工程AsyncLoadPicture 新建布局文件activity_main.xml主界面为一个GridView,还有其子项布局文件gridview_item.xml 2.功能主 ...
- MVC中路由器程序
MVC中路由器程序编写方式如下例子 把地址:/home/add?id=1 改写成:/home/add/1 把地址:/home/edit?id=1&sid=2 改写成:/home/edit/1_ ...
- 部署站点支持Https访问的方法
1.申请公钥和私钥,放到服务器 2.编辑default配置文件 改为 加上证书路径 ps:泛域名支持admin.xxx.com.demo.xxx.com等等,而免费的Let's Encrypt仅支持w ...
- [thinkphp] MD!! 数组构造的好好的,硬是有一个值无法写入数据库
我都要抓狂了,buildsql()方法又用不了,最后决定看runtime里面的文件.先删掉所有的runtime,然后提交一次,就可以在runtime里面看到对应解析后的文件,这样应该可以知道问题在哪. ...
- (20)C#泛型
泛型的定义:通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程时一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. 优点: 1.省去了拆箱.装箱 2.提高安全性 3. ...
- 华农oj 2192: hzk又在打人【CRT合并/待补】
2192: hzk又在打人 Time Limit: 12 Sec Memory Limit: 512 MB Submit: 52 Solved: 1 [Submit][Status][Web Boar ...
- Codeforces Beta Round #25 (Div. 2 Only) A. IQ test【双标记/求给定数中唯一的奇数或偶数】
A. IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I 题意 定义两个长度相等的字符串之间的距离为: 把两个字符串中所有同一种字符变成另外一种,使得两个 ...
- localize by triangle note
1.当拟合结果不为1时,取和上次相近的作为结果2.python画三角拟合图bug (3.减小三角拟合波动:对激光雷达数据进行滤波等处理)(4.在计算三角起始和结束位置时,添加用距离值过滤) theta ...
- 前端html第三方登录
首先推荐一下,这个博客主的文章:https://www.cnblogs.com/v-weiwang/p/5732423.html 很不错,基本靠他的博客. 我这里记一点自己调试过程中的报错等: 1,微 ...