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 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
随机推荐
- CentOS7搭建SAMBA服务器实现与WIN10匿名共享文件
1.安装SAMBA yum -y install samba samba-client samba-common 2.修改文件打开数 vi /etc/security/limits.conf 最后添加 ...
- java中的IO一
一.IO操作的目标 IO的流向 二.IO的分类方法 1.第一种分法:输入流.输出流 2.第二种分法:字节流.字符流 3.第三种分法:节点流.处理流 三.IO当中的核心类 核心类中的核心方法 Input ...
- thinkphp中ajax用户名校验
ajax实在是太神奇了,刚刚接触,不足之处,请大家指正. 采用Ajax方式进行页面无刷新提示,来检测用户名是否存在. 搭建一个thinkphp的环境,在index.html中,ajax代码如下: &l ...
- uva 11673 Garbage Remembering Exam (概率)
题目链接: http://vjudge.net/problem/viewProblem.action?id=42000 该过程为随即过程,因此总期望值等于个单词对应的期望值,即它们wasted的概率 ...
- 【USACO 2.4.5】分数化小数
[描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...
- C# ,asp.net 获取当前,相对,绝对路径(转)
C# ,asp.net 获取当前,相对,绝对路径 一.C#获取当前路径的方法: . System.Diagnostics.Process.GetCurrentProcess().MainModule. ...
- 在sublimetext上打造一个兼容virtualenv的web&python开发环境
利用Sublimetext3&virtualenv 打造一个Web&Python IDE 注: 环境:window|python3;以下使用的sublimetext插件均用packag ...
- windows下Django 部署到Apache24的配置
1.首先下载最新版Apachehttp://httpd.apache.org/download.cgi#apache24,目前官方以不提供windows msi安装包,下载好的直接解压至C盘即可,ap ...
- php in_array比较原理和类型比较问题
in_array 是PHP 的检查数组中是否存在某个值 的函数,里面有三个参数 bool in_array ( mixed $needle , array $haystack [, bool $str ...
- 浅谈Android序列化
序列化原因 序列化的原因基本可以归纳为以下三种情况: 永久性保存对象,保存对象的字节序列到本地文件中: 对象在网络中传递: 对象在IPC间传递. --- --- 序列化方法 在Android系统中关于 ...