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 ...
随机推荐
- 关于C++中不同类之间的赋值问题——存疑
operator=不能重载为全局函数.理由如下 void operator=(int i , A& a) { a.a = i } ; 那么将会出现 99 = a 这种代码,但是99不是左值, ...
- [SoapUI] 通过编程的方式设置当前的Environment
testRunner.testCase.testSuite.project.setActiveEnvironment("Live")
- 一个新手后端需要了解的前端核心知识点之position(一)
以下内容是基于观看慕课网视频教程总结的知识点,边打代码边总结,符合自己的思维习惯.不是针对新手入门 我做程序的初衷是想做一个网站出来.HTML语言当然重要啊,缺什么就百度什么,很浪费时间,还是好好的打 ...
- c语言条件编译#ifdef与#if defined
c语言条件编译#ifdef与#if defined c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...
- Sed命令n,N,d,D,p,P,h,H,g,G,x解析3
摘自:https://blog.csdn.net/WMSOK/article/details/78463199 Sed命令n,N,d,D,p,P,h,H,g,G,x解析 2017年11月06日 23: ...
- HBase批量插入的简单代码
由于项目需要从HBase里读取数据,进行MapReduce之后输出到HDFS中. 为了测试方便,我这里写了一个批量插入HBase数据的测试代码.采用的Maven工程. 打算,今后的所有用到的小测试例子 ...
- KNN算法python实现
1 KNN 算法 knn,k-NearestNeighbor,即寻找与点最近的k个点. 2 KNN numpy实现 效果: k=1 k=2 3 numpy 广播,聚合操作. 这里求距离函数,求某点和集 ...
- Android Logging
Here is HauteLook’s logger class from our Android Code Library. It displays class name, method name ...
- android在更新ADT以后报java.lang.NoClassDefFound的解决办法
Android不是很熟.从网上找到了想要的代码后兴高采烈的导入workspace中,直接开Run. but在真机的时候Console.LogCat下都没显示什么有用的东西,就是写NDK编译完Nativ ...
- 编写高质量代码改善C#程序的157个建议——建议151:使用事件访问器替换公开的事件成员变量
建议151:使用事件访问器替换公开的事件成员变量 事件访问器包含两部分内容:添加访问器和删除访问器.如果涉及公开的事件字段,应该始终使用事件访问器.代码如下所示: class SampleClass ...