POJ - 3468 A Simple Problem with Integers(线段树区间更新,区间查询)
1、给出了一个序列,你需要处理如下两种询问。
"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
"Q a b" 询问[a, b]区间中所有值的和。
2、线段树单点更新太费时,所以使用区间更新
3、
#include <cstdio> #define L(root) ((root) << 1)
#define R(root) (((root) << 1) + 1) const int MAXN = ;
int numbers[MAXN]; struct st
{
// 区间范围
int left, right;
// 更新值、区间总和
long long delta, sum;
} st[MAXN * ]; // 建树代码基本不变
void build(int root, int l, int r)
{
st[root].left = l, st[root].right = r, st[root].delta = ;
if (l == r)
{
st[root].sum = numbers[l];
return;
} int m = l + ((r - l) >> );
build(L(root), l, m);
build(R(root), m + , r);
st[root].sum = st[L(root)].sum + st[R(root)].sum;
} long long query(int root, int l, int r)
{
// 如查询区间恰等于节点区间,直接返回该区间总和即可
if (st[root].left == l && st[root].right == r)
{
return st[root].sum;
} // 否则需将当前区间的“缓冲”值更新下去并修正各节点区间的总和
if (st[root].delta)
{
st[L(root)].delta += st[root].delta;
st[R(root)].delta += st[root].delta;
st[L(root)].sum += st[root].delta * (st[L(root)].right - st[L(root)].left + );
st[R(root)].sum += st[root].delta * (st[R(root)].right - st[R(root)].left + );
st[root].delta = ;
} int m = st[root].left + ((st[root].right - st[root].left) >> );
if (r <= m)
{
return query(L(root), l, r);
}
else if (l > m)
{
return query(R(root), l, r);
}
else
{
return query(L(root), l, m) + query(R(root), m + , r);
}
} void update(int root, int l, int r, long long v)
{
// 如变更区间恰等于节点区间,只修正当前节点区间即可
if (st[root].left == l && st[root].right == r)
{
st[root].delta += v;
st[root].sum += v * (r - l + );
return;
} // 否则需向下修正相关节点区间
if (st[root].delta)
{
st[L(root)].delta += st[root].delta;
st[R(root)].delta += st[root].delta;
st[L(root)].sum += st[root].delta * (st[L(root)].right - st[L(root)].left + );
st[R(root)].sum += st[root].delta * (st[R(root)].right - st[R(root)].left + );
st[root].delta = ;
} int m = st[root].left + ((st[root].right - st[root].left) >> );
if (r <= m)
{
update(L(root), l, r, v);
}
else if (l > m)
{
update(R(root), l, r, v);
}
else
{
update(L(root), l, m, v);
update(R(root), m + , r, v);
}
// 同时一定要记得修正当前节点区间的总和
st[root].sum = st[L(root)].sum + st[R(root)].sum;
} int main()
{
int N, Q;
while (scanf("%d%d", &N, &Q) != EOF)
{
for (int i = ; i <= N; ++i)
{
scanf("%d", &numbers[i]);
} build(, , N); char cmd;
int l, r;
long long v;
while (Q--)
{
scanf(" %c", &cmd);
scanf("%d%d", &l, &r);
switch (cmd)
{
case 'Q':
printf("%lld\n", query(, l, r));
break; case 'C':
scanf("%lld", &v);
if (v)
{
update(, l, r, v);
}
break;
}
}
} return ;
}
POJ - 3468 A Simple Problem with Integers(线段树区间更新,区间查询)的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- 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 ...
随机推荐
- 在mysql的操作界面中,如何清屏幕
1.快捷键:Ctrl+L2.通过执行SHELL命令: \! clear实际上 \! 用来执行操作系统的shell命令,不仅是clear,其他命令也可以.shell命令执行完成后,会返回mysql Us ...
- zoj 1109 Language of FatMouse(map)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- ACboy needs your help(分组背包)
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he ...
- BZOJ1777: [Usaco2010 Hol]rocks 石头木头
n<=10000的树,节点有初始石头数<=1000,进行这样的游戏:两人轮流行动,我先手,每次可以选一个节点(≠1)把不超过m<=1000个石头移到父亲,最后所有石头都在节点1,没法 ...
- linux命令2——进程相关
(1)ps -ef :可以看到内核的线程.
- virtualBox下Centos系统扩展磁盘空间
(1)查看空间容量: 打开windows命令终端.然后打开virtualbox安装目录,找到VBoxManage.exe,拖动到终端里面.输入命令:list hdds,回车. 我安装的位置是 : C: ...
- jxls使用模版导出Excel
/** * 使用模版导出Excel */ @SuppressWarnings({ "unchecked", "deprecation" } ...
- python学习之-- redis模块管道/订阅发布
redis 模块操作剩余其他常用操作 delete(*names):删除任意的数据类型exists(name):检测redis的name是否存在keys(pattern='*'):根据模型获取redi ...
- PAT (Advanced Level) 1037. Magic Coupon (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- [bzoj2595][WC2008]游览计划/[bzoj5180][Baltic2016]Cities_斯坦纳树
游览计划 bzoj-2595 wc-2008 题目大意:题目链接.题目连接. 注释:略. 想法:裸题求斯坦纳树. 斯坦纳树有两种转移方式,设$f[s][i]$表示联通状态为$s$,以$i$为根的最小代 ...