ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000. The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000. Each of the next Q lines represents an operation. "C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000. "Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
#include <iostream>
#include <cstdio>
#define LL long long using namespace std; int n, q; //线段树
const int maxn = 100000;
struct node
{
int lt, rt;
LL val, add;
}tree[4*maxn]; //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = 0;//每段的初值,根据题目要求
tree[id].add = 0;
if (lt == rt)
{
scanf("%I64d", &tree[id].val);
//tree[id].add = ??;
return;
}
int mid = (lt + rt) >> 1;
Build(lt, mid, id << 1);
Build(mid + 1, rt, id << 1 | 1);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} void PushDown(int id, int pls)
{
tree[id<<1].add += tree[id].add;
//tree[id<<1].val += (pls-(pls>>1))*tree[id].add;
tree[id<<1].val += (tree[id<<1].rt-tree[id<<1].lt+1)*tree[id].add;
tree[id<<1|1].add += tree[id].add;
//tree[id<<1|1].val += (pls>>1)*tree[id].add;
tree[id<<1|1].val += (tree[id<<1|1].rt-tree[id<<1|1].lt+1)*tree[id].add;
tree[id].add = 0;
} //增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
tree[id].add += pls;
tree[id].val += pls * (tree[id].rt-tree[id].lt+1);
return;
}
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
if (lt <= mid)
Add(lt, rt, id<<1, pls);
if (rt > mid)
Add(lt, rt, id<<1|1, pls);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} LL Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
LL ans = 0;
if (lt <= mid)
ans += Query(lt, rt, id<<1);
if (rt > mid)
ans += Query(lt, rt, id<<1|1);
return ans; } int main()
{
//freopen("in.txt", "r", stdin);
char op;
int a, b, k;
while (scanf("%d%d", &n, &q) != EOF)
{
Build(1, n, 1);
for (int i = 0; i < q; ++i)
{
getchar();
op = getchar();
getchar();
scanf("%d%d", &a, &b);
if (op == 'Q')
printf("%I64d\n", Query(a, b, 1));
else
{
scanf("%d", &k);
Add(a, b, 1, k);
}
}
}
return 0;
}
ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)的更多相关文章
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- [poj3468]A Simple Problem with Integers_线段树
A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- 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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
随机推荐
- IDEA小技巧-随时更新
© 版权声明:本文为博主原创文章,转载请注明出处 1.设置删除一行快捷键 File->Settings->keymap->Delete Line 2.设置代码提示快捷键 File-& ...
- caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...
- easyNetq demo
本demo包含一个类库,2个console程序 1.新建类库 MQHelper,控制台程序 consumer和proc ,控制台程序引用MQHelper 2.使用nuget安装easynwtq 和 ...
- EF获取DbContext中已注册的所有实体类型
/// <summary> /// 获取DbContext中已注册的实体类型 /// </summary> /// <typeparam name="T&quo ...
- Android - 使用messager实现进程间通信(服务器端→客户端,客户端→服务器端双向)
之前看了一篇,然后不自己动手肯定是不行的,然后自己又写了一遍. 背景: 一般使用messenger进行进程间通信的时候,我们只能进行单方向通信.但是有没有办法让服务器端和客户端进行双向通信呢? 解决思 ...
- 玩家下线(GS部分)
玩家下线,之前一直感觉这个过程有点复杂 else if (stat == link_stat::link_disconnected || stat == link_stat::link_connect ...
- EasyRTMP Android安卓手机直播推流摄像头偏暗的问题解决
在我们测试EasyRTMP Android安卓手机推流的过程中发现有些设备预览时,明显偏暗!在稍微暗点的环境中几乎很难看清东西-额,这是怎么回事呢?又是安卓设备的兼容性问题,头疼! !!!好吧,停止抱 ...
- 九度OJ 1171:C翻转 (矩阵计算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4649 解决:1530 题目描述: 首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作 ...
- 九度OJ 1144:Freckles(斑点) (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1538 解决:760 题目描述: In an episode of the Dick Van Dyke show, little Richi ...
- 九度OJ 1028:继续畅通工程 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...