poj 3466 A Simple Problem with Integers
题目链接:http://poj.org/problem?id=3468 http://poj.org/problem?id=3468 http://poj.org/problem?id=3468
思路:这是一个区间改动区间查询的题,因为题目中的给的数据比較大,那么用单个改动和查询肯定不行,所以。
。。。注意数据可能比較大,应该用__int64或long long存数据。
。
。。
。
code:
#include<stdio.h>
#include<math.h>
#define L(u) (u<<1)
#define R(u) (u<<1|1) const int M=100010; struct Node
{
__int64 l,r;
__int64 add;
long long sum;
}node[M*4]; __int64 a[M]; void pushup(__int64 u)
{
node[u].sum=node[L(u)].sum+node[R(u)].sum;
return ;
} void pushdown(__int64 u)
{
node[L(u)].add+=node[u].add;
node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add;
node[R(u)].add+=node[u].add;
node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
node[u].add=0;
} void build(__int64 u,__int64 left,__int64 right)
{
node[u].l=left;
node[u].r=right;
node[u].add=0;
if(left==right)
{
node[u].sum=a[left];
return ;
}
__int64 mid=(node[u].l+node[u].r)/2;
build(L(u),left,mid);
build(R(u),mid+1,right);
pushup(u); } void update(__int64 u,__int64 left,__int64 right,__int64 v)
{
if(left<=node[u].l&&node[u].r<=right)
{
node[u].add+=v;
node[u].sum+=(node[u].r-node[u].l+1)*v;
return ;
}
//node[u].sum+=(right-left+1)*v; //当前节点表示的区间不是查询区间的子区间
if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
__int64 mid=(node[u].l+node[u].r)/2;
if(right<=mid) update(L(u),left,right,v);
else if(left>mid) update(R(u),left,right,v);
else
{
update(L(u),left,mid,v);
update(R(u),mid+1,right,v);
}
node[u].sum=node[L(u)].sum+node[R(u)].sum;
} __int64 query(__int64 u,__int64 left,__int64 right)
{
if(left<=node[u].l&&node[u].r<=right)
{
return node[u].sum;
}
if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
__int64 mid=(node[u].l+node[u].r)/2;
if(right<=mid) return query(L(u),left,right);
else if(left>mid) return query(R(u),left,right);
else
{
return (query(L(u),left,mid)+query(R(u),mid+1,right));
}
} int main()
{
__int64 n,m,i,x,y,z;
while(scanf("%I64d%I64d",&n,&m)==2)
{
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
build(1,1,n);
char str[5];
for(i=0;i<m;i++)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%I64d%I64d%I64d",&x,&y,&z);
update(1,x,y,z);
}
else
{
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",query(1,x,y));
}
}
}
return 0;
}
poj 3466 A Simple Problem with Integers的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- 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 //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 55273 ...
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97217 ...
随机推荐
- cocos2d-x游戏开发系列教程-超级玛丽05-CMMenuScene
代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 背景 上一篇博文提到appDelegate,在该类 ...
- 基于visual Studio2013解决C语言竞赛题之0202坐标转换
题目
- fedora 设置命令别名
用命令 alias 举例: alias ggw="g++ -g -Wall" ggw 是自定义的别名,可根据需要进行修改设置,等于后面的则是别名的具体含义,在终端输入ggw就像当于 ...
- MyEclipse10+Jdk1.7+OSGI+MySql实现CRUD数据库
开发环境: Windows2008R2 64位置+MyEclipse10+jdk1.7.0_67+MySql5.5 软件安装:myeclipse-10.0-offline-installer-wind ...
- Android图像篇
Android的渲染分为2D渲染和3D渲染两种,当中2D渲染的引擎为Skia.3D渲染的引擎是OpenGL ES.眼下.Android支持OpenGL ES1.0和OpenGL ES 2.0两种标准. ...
- 【Web】java异常处理
J2EE中一般对异常状况的处理都可以用两种情况对其进行相应处理. 1. 通常情况下,一般异常处理可以选择用throw.throws从底层一直往上面抛,直到抛到Action,让其将异常显示在页面上面进行 ...
- DZNEmptyDataSet框架简介
给大家推荐一个设置页面加载失败时显示加载失败等的框架. 下载地址:DZNEmptyDataSet https://github.com/dzenbot/DZNEmptyDataSet 上效果首先在你的 ...
- 10_9 java笔记
java中所有的关键字都是小写的注意main虽然被编译器识别,但是它并不是关键字包:(名字小写) 单级包:liyi 多级包:cn.itcast path 和classpath的区别:path环境变量里 ...
- python读写xml
来自http://blog.csdn.net/liuyuehui110/article/details/7287897 备份防止链接失效 一.XML的读取. 在 NewEdit 中有代码片段的功能,代 ...
- django perm用法
定义用户model时可以给用户分配权限: class Meta: permissions = ( ("can_mark", "Can mark"), ...