POJ 3468 A Simple Problem with Integers 【线段树】
题目链接
http://poj.org/problem?id=3468
思路
线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
int n, q;
LL anssum;
struct Node
{
LL l, r;
LL addv, sum;
}tree[maxn << 2];
void maintain(int id)
{
if (tree[id].l >= tree[id].r)
return;
tree[id].sum = tree[id << 1].sum + tree[id << 1 | 1].sum;
}
void pushdown(int id)
{
if (tree[id].l >= tree[id].r)
return;
if (tree[id].addv)
{
int tmp = tree[id].addv;
tree[id << 1].addv += tmp;
tree[id << 1 | 1].addv += tmp;
tree[id << 1].sum += (tree[id << 1].r - tree[id << 1].l + 1) * tmp;
tree[id << 1 | 1].sum += (tree[id << 1 | 1].r - tree[id << 1 | 1].l + 1) * tmp;
tree[id].addv = 0;
}
}
void build(int id, LL l, LL r)
{
tree[id].l = l;
tree[id].r = r;
tree[id].addv = 0;
tree[id].sum = 0;
if (l == r)
{
tree[id].sum = 0;
return;
}
LL mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
maintain(id);
}
void updateAdd(int id, LL l, LL r, LL val)
{
if (tree[id].l >= l && tree[id].r <= r)
{
tree[id].addv += val;
tree[id].sum += (tree[id].r - tree[id].l + 1) * val;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
updateAdd(id << 1, l, r, val);
if (mid < r)
updateAdd(id << 1 | 1, l, r, val);
maintain(id);
}
void query(int id, LL l, LL r)
{
if (tree[id].l >= l && tree[id].r <= r)
{
anssum += tree[id].sum;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
query(id << 1, l, r);
if (mid < r)
query(id << 1 | 1, l, r);
maintain(id);
}
int main()
{
scanf("%d %d", &n, &q);
build(1, 1, n);
for (LL i = 1; i <= n; i++)
{
LL num;
cin >> num;
updateAdd(1, i, i, num);
}
char id;
LL x, y;
LL val;
while (q--)
{
scanf(" %c", &id);
if (id == 'C')
{
scanf("%lld %lld %lld", &x, &y, &val);
updateAdd(1, x, y, val);
}
else if (id == 'Q')
{
scanf("%lld %lld", &x, &y);
anssum = 0;
query(1, x, y);
cout << anssum << endl;
}
}
}
POJ 3468 A Simple Problem with Integers 【线段树】的更多相关文章
- 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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- 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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
随机推荐
- layui表格渲染中模板的使用举例
实例一: { field: 'status', align: 'center', title: '活动状态', templet: function (d) { if (d.status == &quo ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
- CI框架 简介
CI工作流程: 所有的入口都从根目录下的index.php进入,确定应用所在目录后,加载 codeigniter/CodeIgniter.php 文件,该文件会顺序加载以下文件执行整个流 ...
- 在iframe的父级作用域操作,ifame中的元素。。
frames["iframe的name"].SchDatas SchDatas为方法名js中 frames["iframe的name"].document.ge ...
- 浅谈HTTP缓存以及后端,前端如何具体实现HTTP缓存
<浅谈HTPP缓存>原版: https://juejin.im/post/5bdeabbbe51d4505466cd741?utm_source=gold_browser_extensio ...
- 移动web开发经验总结(转)
1.<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-sca ...
- matlab小数分数转换
>> format short %%%% MATLAB默认格式,小数显示 >> format rat %%%%% 转成分数表示
- div块级元素获取焦点
在做弹出层时需要对div获取失去焦点 focus blur只是针对form表单控件的,而对于 span , div , li 之类的,则没办法触发它们的动作 几个事件(摘自w3c). blur事件: ...
- c++ 模板仿函数初探
一直以来对于C++的使用基本上都是C with class,对于各种尖括号的模板都是敬而远之,最近忽然觉得该好好看看模板了.于是就有了这篇blog. 本文以一个查找问题为例来说明模板仿函数. 在C中, ...
- 第二章----python函数
第一节:调用函数 1.函数是什么? 函数是组织好的,可以重复利用的. 2.为什么要用到函数? 提高应用的模块性,提高重复利用率.指的是:多个文件中可能都要用到该函数,直接拿来调用就行,不用在重复写一个 ...