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 ...
随机推荐
- Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Errcode: 17)
这个错误时在Mysql主从配置产生的,最后找到这个Mysql的一个bug http://bugs.mysql.com/bug.php?id=62055 bug的主要原因是:打开文件的函数中指定打开模式 ...
- js基础系列框架:JS重要知识点(转载)
这里列出了一些JS重要知识点(不全面,但自己感觉很重要).彻底理解并掌握这些知识点,对于每个想要深入学习JS的朋友应该都是必须的. 讲解还是以示例代码搭配注释的形式,这里做个小目录: JS代码预解析原 ...
- python 爬虫2 Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. import url ...
- What is special about /dev/tty?
ls -la /dev/tty shows the output: crw-rw-rw- 1 root tty 5, 0 Dec 14 22:21 /dev/tty The 'c' means it' ...
- 新MBP使用git命令时启用xcode的终端log
Last login: Mon Oct 22 12:41:33 on consoleuser:~ me$ git Agreeing to the Xcode/iOS license requires ...
- OpenGL ES andoid学习————1
package com.xhm.getaccount; import javax.microedition.khronos.egl.EGLConfig; import javax.microediti ...
- 基于ormlite创建数据库存储数据案例
一直不知道安卓创建数据库存储数据,以前遇到过,但是没有深入研究,今天仔细的看了一下,学习到了一点知识 直接看代码了 public class DatabaseHelper extends OrmLit ...
- iOS学习笔记(二)——Hello iOS
前面写了iOS开发环境搭建,只简单提了一下安装Xcode,这里再补充一下,点击下载Xcode的dmp文件,稍等片刻会有图一(拖拽Xcode至Applications)的提示,拖拽至Applicatio ...
- Sass mixin 使用css border属性画三角形
To be finished. //triangle@mixin css-triangle ($direction: "down", $size: 20px, $color: #0 ...
- ZOJ 2059 The Twin Towers(双塔DP)
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...