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 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
随机推荐
- 【原创】贴片电容的测量方法。。。这是我从自己QQ空间转过来的,本人实操!
电容不工作一般分为3种情况,漏电.击穿.无电容.一般检测用万用表检测阻值一般调在10K-20K为测量标准,特别是贴片电容.把万用表的笔尖点在贴片电容的两侧,如下图测量: l1.jpg l2.jpg l ...
- HDU 4442 Physical Examination(关于贪心排序)
这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...
- NYOJ128前缀式计算
前缀式计算 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 先说明一下什么是中缀式: 如2+(3+4)*5这种我们最常见的式子就是中缀式. 而把中缀式按运算顺序加上括 ...
- Adobe DreamweaverCS6安装及破解(序列号+破解补丁)
一:安装 1) Adobe DreamweaverCS6中文版下载地址:Adobe DreamweaverCS6中文版下载 2)Adobe DreamweaverCS6安装及破解说明下载地址:Adob ...
- DOM Style样式对象的详细用法
DOM Style样式对象的详细用法 HTML Style样式比较复杂,相应访问.修改方法也有所差异.参考相关资料,整理如下. 典型Html文件如下,有三种定义方式. <head> ...
- 简单图片banner轮播
/**************[css]****************/ <style type="text/css"> *{margin:0px; ...
- 写代码要注意细节,无谓的找前台bug
<input type="checkbox" name="ckb" value="'+value[0]+'">'真的感觉小细节真 ...
- photoshop mac版下载及破解
1.下载 直接百度photoshop,就可以找到百度的下载源: 2.破解 http://zhidao.baidu.com/question/581955095.html
- 报错:ORA-02287: 此处不允许序号
CREATE TABLE MY_TAB (N1 NUMBER(5),N2 DATE); SELECT * FROM MY_TAB; CREATE SEQUENCE ...
- ASP.NET菜鸟之路之Application小例子
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Session介绍 Application对象用 ...