POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】
一、题目
二、分析
裸的线段树区间查询+修改。
三、AC代码
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; #define ll long long
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int MAXN = 1e5 + ;
ll add[MAXN << ], sum[MAXN << ]; void PushUp(int rt)
{
sum[rt] = sum[rt << ] + sum[rt << | ];
return;
} void PushDown(int ln, int rn, int rt)
{
if(add[rt])
{
sum[rt << ] += (ll) ln * add[rt];
sum[rt << | ] += (ll) rn * add[rt];
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
add[rt] = ;
}
return;
} void Build(int l, int r, int rt)
{
add[rt] = ;
if(l == r)
{
scanf("%lld", &sum[rt]);
return;
}
int mid = (l + r) >> ;
Build(lson);
Build(rson);
PushUp(rt);
return;
} void Update(int L, int R, int c, int l, int r, int rt)
{
if(L <= l && r <= R)
{
add[rt] += c;
sum[rt] += (ll)(r - l + ) * c;
return;
}
int mid = (l + r) >> ;
PushDown(mid - l + , r - mid, rt);
if(L <= mid)
{
Update(L, R, c, lson);
}
if(R > mid)
{
Update(L, R, c, rson);
}
PushUp(rt);
return;
} ll Query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
return sum[rt];
int mid = (l + r) >> ;
PushDown(mid - l + , r - mid, rt);
ll ans = ;
if(L <= mid)
{
ans += Query(L, R, lson);
}
if(R > mid)
{
ans += Query(L, R, rson);
}
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
int N, Q;
scanf("%d %d", &N, &Q);
Build(, N, );
char s[];
for(int i = ; i < Q; i++)
{
int L, R, C;
scanf("%s", s);
if(s[] == 'Q')
{
scanf("%d %d", &L, &R);
printf("%I64d\n", Query(L, R, , N, ));
}
else
{
scanf("%d %d %d", &L, &R, &C);
Update(L, R, C, , N, );
}
}
}
POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】的更多相关文章
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 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 [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- 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 ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [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 ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
随机推荐
- uc_authcode()
//解用户标识码public static function setAuthCode($auth){ //对 base64 编码的 data 进行解码. $auth = base64_decode($ ...
- 原型工具Axure vs Mockplus ——表格对比 , 你选谁?
现如今原型设计能力是越来越多的UI/UX.产品经理.提案者和互联网创业者必不可少的技能之一,所以在这里着重向大家介绍这两款非常棒的原型设计工具在表格功能上到底有哪些不同. Axure大家比较熟悉,Mo ...
- MemoryUsage:监测java虚拟机内存使用
通过MemoryUsage可以查看Java 虚拟机的内存池的内存使用情况.MemoryUsage类有四个值(均以字节为单位): ===Init=== java虚拟机在启动的时候向操作系统请求的初始内存 ...
- 彻底修改Eclipse的默认编码
引用各位前辈经验得到彻底修改eclipse默认编码的方法. 单在eclipse里设置编码方式非常复杂且容易遗漏,全部修改后,有些代码生成模板内的${encode}变量仍为原编码方案,经过查阅许多资料得 ...
- windows server2012安装mysql时一直停留在start server的解决方法
安装的时候,starting server 卡住 原因分析 这个问题小编安装mysql时也碰到过,出现这个问题是my.ini文件没有复制成功了,我们只要在mysql安装目录把把目录中的备份的my-sm ...
- [GO]使用select实现斐波那契
package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...
- [GO]使用bufio的文件读取方式
package main import ( "os" "fmt" "bufio" "io" ) func ReadFil ...
- intellJ IDE 15 生成 serialVersionUID
这个Inspections的位置不好找,建议搜索Serialization issues 然后勾选两项 serialzable class without "serialVersionUID ...
- Ajax之XMLHttpRequest
XMLHttpRequest对象 XMLHttpRequest 提供客户端同http服务器通讯的协议 一:创建 IE : http_request = new ActiveXObject(" ...
- 编写高质量代码改善C#程序的157个建议——建议71:区分异步和多线程应用场景
建议71:区分异步和多线程应用场景 初学者有时候会将异步和多线程混为一谈.如果对它们之间的区别不是很清楚,很容易写出下面这样的代码: private void buttonGetPage_Click( ...