POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468
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>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; LL sum[MAXN*], addv[MAXN*]; void push_up(int u)
{
sum[u] = sum[u*] + sum[u*+];
} void push_down(int u, int l, int r)
{
if(addv[u]!=)
{
sum[u*] += 1LL*(r-l++)/*addv[u];
sum[u*+] += 1LL*(r-l+)/*addv[u];
addv[u*] += addv[u];
addv[u*+] += addv[u];
addv[u] = ;
}
} void add(int u, int l, int r, int x, int y, int val)
{
if(x<=l && r<=y)
{
addv[u] += val;
sum[u] += 1LL*(r-l+)*val;
return;
} push_down(u, l, r);
int mid = (l+r)/;
if(x<=mid) add(u*, l, mid, x, y, val);
if(y>=mid+) add(u*+, mid+, r, x, y, val);
push_up(u);
} LL query(int u, int l, int r, int x, int y)
{
if(x<=l && r<=y)
return sum[u]; push_down(u, l, r);
LL ret = ;
int mid = (l+r)/;
if(x<=mid) ret += query(u*, l, mid, x, y);
if(y>=mid+) ret += query(u*+, mid+, r, x, y);
return ret;
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(sum, , sizeof(sum));
memset(addv, , sizeof(addv));
char op[]; int a, b, c;
for(int i = ; i<=n; i++)
{
scanf("%d", &a);
add(, , n, i, i, a);
} for(int i = ; i<=m; i++)
{
scanf("%s", op);
if(op[]=='C')
{
scanf("%d%d%d", &a, &b, &c);
add(, , n, a, b, c);
}
else
{
scanf("%d%d", &a, &b);
printf("%lld\n", query(, , n, a, b));
}
}
}
}
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 ...
- 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 ...
- [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 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 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 ...
- 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 ...
- 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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- Flash学习笔记(01)
一:动画基础原理 二:Flash的动画的制作方式 三:元件与实例的关系 四:Flash实例 可以做出网上常见的网页动画 Flash能为我们做什么 1.网站.贺卡.配乐短片.短剧.游戏.教学 ...
- PS学习笔记(02)
书籍推荐: <设计之下>这本APP设计书字里行间都透露出了真实,作者能将其工作流程和方法分享出来,实在值得尊敬.通过这本书全面了解了真实的设计工作是怎么做的,今后可以用到自己的工作中.赞! ...
- bzoj2973 入门oj4798 石头游戏
我们人为地搞出来一个全能神,每次调用他他可以给一个节点 \(1\) 个石头. 这样,当前的状态就可以由上一秒的状态搞过来,这就像是一个递推.用矩阵加速. #include <iostream&g ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- [luoguP1352] 没有上司的舞会(DP)
传送门 树上的dp,从底向上dp就行. 设dp[u][0]表示不选节点 u 的最大值,dp[u][1]表示选节点 u 的最大值. 则状态转移方程为: dp[u][0] = ∑max(dp[v][1], ...
- xftp向ubuntu传输文件错误
xftp向ubuntu传输文件错误原因: 登陆用户对文件夹没有权限. 解决方法:授予权限 chmod 777 该目录名
- php之ThinkPHP的memcached类的修改
php之ThinkPHP的memcached类的修改 在Think\Cache\Driver\Memcached.class.php中,增加方法获取错误信息的方法,方便调试, public funct ...
- socket相关
socket层 图示,没有找到socket,那么socket层在哪儿呢? 看图: socket是什么 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Sock ...
- linux日志服务器审计客户端history记录
https://blog.csdn.net/yanggd1987/article/details/70255179
- RedirectAttributes
RedirectAttributes是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的 他有两种带参的方式: 第一种: attr.addAttribute(&q ...