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
Source

上图最后一行打错了,应该似乎求a[l]到a[r]的和。
线段树做法太裸,直接贴代码:
program rrr(input,output);
type
treetype=record
l,r:longint;
sum,d:int64;
end;
var
a:array[..]of treetype;
c:array[..]of longint;
n,q,i,x,y,d:longint;
ch:char;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;a[k].d:=;
if l=r then begin a[k].sum:=c[l];exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);build(i+,mid+,r);
a[k].sum:=a[i].sum+a[i+].sum;
end;
procedure pushdown(k:longint);
var
i:longint;
begin
if a[k].l=a[k].r then a[k].d:=;
if a[k].d= then exit;
i:=k+k;a[i].sum:=a[i].sum+(a[i].r-a[i].l+)*a[k].d;a[i].d:=a[i].d+a[k].d;
inc(i);a[i].sum:=a[i].sum+(a[i].r-a[i].l+)*a[k].d;a[i].d:=a[i].d+a[k].d;
a[k].d:=;
end;
function ask(k:longint):int64;
var
mid:longint;
ans:int64;
begin
pushdown(k);
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].sum);
mid:=(a[k].l+a[k].r)>>;ans:=;
if x<=mid then ans:=ask(k+k);
if mid<y then ans:=ans+ask(k+k+);
exit(ans);
end;
procedure change(k:longint);
var
mid,i:longint;
begin
pushdown(k);
if (x<=a[k].l) and (a[k].r<=y) then begin a[k].sum:=a[k].sum+d*(a[k].r-a[k].l+);a[k].d:=d;exit; end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<=mid then change(i);
if mid<y then change(i+);
a[k].sum:=a[i].sum+a[i+].sum;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,q);
for i:= to n do read(c[i]);readln;
build(,,n);
for i:= to q do
begin
read(ch,x,y);
if ch='Q' then writeln(ask())
else begin read(d);change(); end;
readln;
end;
close(input);close(output);
end.
下面是树状数组做法:
代码(实测比线段树快1倍):
program rrr(input,output);
var
a,b:array[..]of int64;
n,q,i:longint;
c:char;
ans,x,y,z:int64;
procedure adda(k,x:int64);
begin
while k<=n do begin a[k]:=a[k]+x;k:=k+k and (-k); end;
end;
procedure addb(k,x:int64);
begin
while k<=n do begin b[k]:=b[k]+x;k:=k+k and (-k); end;
end;
function suma(k:longint):int64;
begin
ans:=;
while k> do begin ans:=ans+a[k];k:=k-k and (-k); end;
exit(ans);
end;
function sumb(k:longint):int64;
begin
ans:=;
while k> do begin ans:=ans+b[k];k:=k-k and (-k); end;
exit(ans);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,q);
fillchar(a,sizeof(a),);
fillchar(b,sizeof(b),);
for i:= to n do begin read(z);adda(i,z); end;readln;
for i:= to q do
begin
read(c,x,y);
if c='Q' then writeln(suma(y)+sumb(y)*y-suma(x-)-sumb(x-)*(x-))
else begin read(z);adda(x,-z*(x-));addb(x,z);adda(y,z*y);addb(y,-z); end;
readln;
end;
close(input);close(output);
end.
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 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- 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(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- 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:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
随机推荐
- P3084 [USACO13OPEN]照片Photo
题目描述 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N. 于是约翰拍摄了M(1 <= M <= 100,00 ...
- 八,ESP8266 文件保存数据(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7533845.html 应该是LUA介绍8266的最后一篇,,,,,,下回是直接用SDK,,然后再列个12345...... ...
- 网络对抗技术 2017-2018-2 20152515 Exp3 免杀原理与实践
基础问题回答 (1)杀软是如何检测出恶意代码的? 答:分析恶意程序的行为特征,分析其代码流将其性质归类于恶意代码. (2)免杀是做什么? 答:一般是对恶意软件做处理,让它不被杀毒软件所检测,也是渗透测 ...
- 网络对抗技术 2017-2018-2 20152515 Exp1 PC平台逆向破解 笔记
Exp1 PC平台逆向破解 1.堆栈不可保护: ROP 2.alsr 随机化: 填充NOPS "\90" 3.不加堆栈保护 shellcode: 1.不依赖外部函数 2.不含\00 ...
- 20155305乔磊《网络对抗》逆向及Bof基础
20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...
- 20155337 《网络对抗》 Exp2 后门原理与实践
20155337 <网络对抗> Exp2 后门原理与实践 一.基础问题回答 - 例举你能想到的一个后门进入到你系统中的可能方式? 在Unix里,login程序通常用来对telnet来的用户 ...
- dxp altium pcb里面如果想让重叠的两个元件不报错怎么设置?
dxp的设置是Design Rules里面有个Placement选项,把第一个的钩去掉即可.
- 在服务器运行一个jar包,不用时终止它
1.打成jar包后,输入命令 nohup java -jar floodlight.jar >log.txt >& &//nohup 不生成 nohup.out的方法noh ...
- P4385 [COCI2009]Dvapravca
首先特判掉蓝点数量\(<2\)的情况.没有蓝点答案就是\(n\),有一个蓝点可以枚举一个红点,选择过这个蓝点和红点的一条线和在无穷远处的平行线(即这条线对应的两个半平面). 这里认为过一个点是与 ...
- springtest mapper注入失败问题解决 {@org.springframework.beans.factory.annotation.Autowired(required=true)}
花费了一下午都没有搜索到相关解决方案的原因,一是我使用的 UnsatisfiedDependencyException 这个比较上层的异常(在最前面)来进行搜索, 范围太广导致没有搜索到,而且即便是有 ...