C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; #define maxn 100005
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R struct Tree//op等于0的时候子树不需要更新,op等于1需要更新
{
int L, R, op;//需要向下更新的值为e
long long sum, e;//因为区间和比较大,所以使用long long 保存
int Mid(){return (L+R)/2;}
int Len(){return (R-L+1);}
}tree[maxn*4];
long long val[maxn]; void Down(int root)//向下更新
{
if(tree[root].op && tree[root].L != tree[root].R)
{
tree[root].op = false;
tree[root<<1].op = tree[root<<1|1].op = true;
tree[root<<1].e += tree[root].e;
tree[root<<1|1].e += tree[root].e; tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e; tree[root].e = 0;
}
}
void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].op = false; if(L == R)
{
tree[root].sum = val[L];
return ;
} Build(Lson);
Build(Rson); tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
void Insert(int root, int L, int R, long long e)
{
Down(root); tree[root].sum += (R-L+1) * e; if(tree[root].L == L && tree[root].R == R)
{
tree[root].e = e, tree[root].op = true;
return ;
} if(R <= tree[root].Mid())
Insert(root<<1, L, R, e);
else if(L > tree[root].Mid())
Insert(root<<1|1, L, R, e);
else
{
Insert(Lson, e);
Insert(Rson, e);
}
}
long long Query(int root, int L, int R)
{
Down(root); if(tree[root].L == L && tree[root].R == R)
return tree[root].sum;
if(R <= tree[root].Mid())
return Query(root<<1, L, R);
else if(L > tree[root].Mid())
return Query(root<<1|1, L, R);
else
return Query(Lson) + Query(Rson);
} int main()
{
int i, N, Q; while(scanf("%d%d", &N, &Q) != EOF)
{
for(i=1; i<=N; i++)
scanf("%I64d", &val[i]);
Build(1, 1, N); int a, b; char s[10];
long long c; while(Q--)
{
scanf("%s%d%d", s, &a, &b); if(s[0] == 'C')
{
scanf("%I64d", &c);
Insert(1, a, b, c);
}
else
{
long long ans = Query(1, a, b);
printf("%I64d\n", ans);
}
}
} return 0;
} /*
5 2
1 2 3 4 5
C 1 5 5
Q 1 1
*/
C - A Simple Problem with Integers - poj 3468(区间更新)的更多相关文章
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- [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 (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- 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(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- (简单) 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 ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
随机推荐
- Python开发【第十三篇】:jQuery--无内容点击-不进去(一)
Python开发[第十三篇]:jQuery--无内容点击-不进去(一)
- GIT学习(一)-->Git产生的历史原因
首先要说:Git是目前世界上最先进的分布式版本控制系统(没有之一). git的作者:Linus,林纳斯(下图就是,先膜拜一下,因为成就可不止这一点) 他是为何要写git的呢?话说 Linus虽然创建了 ...
- html.day01
1.web标准: 1. 结构 (xhtml) 2. 表现(css) 3.行为(js) html 超文本标记语言 xhtml (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...
- javadoc in archive eclipse.
Open Windows -> Preferences. 2.Select jre from Installed JREs then hit Edit botton on the right. ...
- oracle批量转库工作,比较快捷的方式
select 'create table ' || t.TABLE_NAME || ' as select * from ODS_ZMGLXT.'|| t.TABLE_NAME ||'; ' fr ...
- Css四种样式
1. 2 3 4 5 6.
- UITouch触摸事件
UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...
- MySQL 连接
MySQL 连接 使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@ ...
- 【NOIP2014】赛后总结
noip考完了,心中所牵挂的一下子就消散了,感觉浑身很轻松. 说实话,我参加noip有好几次了,这应该会是我的最后一次,尽管如此,无论是在考试的前几天还是在考试的时候,心中都没有太多的紧张. 我在no ...
- PHP获取函数返回值的引用
通过在函数前添加&可以获取函数返回值的引用,如:function &test(){return 10;}