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 ...
随机推荐
- Android开发之TextView排版问题
下面直接是关于解决该问题的代码(根据别人的代码进行了修正以及测试,保证可以修改字体尺寸.颜色.根据padding调整,如果需要支持其他的格式可以将对应的属性添加至Paint类型的对象中): 1 p ...
- 在Update Panel 控件里面添加 File Upload 控件 上传文件
Detail Information:http://www.codeproject.com/Articles/482800/FileplusUploadplusinplusUpdateplusPane ...
- if语句之有房么?有钱么?有能力么?
思路:1.如果有房,可以谈谈 2.如果没有房,问第二个条件有钱么,如果有,可以谈谈 3.如果没有房没有钱,则问第三个条件有能力么,如果有,可以谈谈 4.如果以上三个条件都没有,则拜拜 Console. ...
- reason: 'unable to dequeue a cell with identifier Cell
今天在cell重用的时候出现一下错误 reason: 'unable to dequeue a cell with identifier Cell - must register ...
- linux嵌入式: 实现自己的tree命令
//# cat treecmd.c #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #inclu ...
- CentOS 6 用SVN自动提交文件到web服务器
关于 svn 的安装 参考:[转]Linux(centOS6.5)下SVN的安装.配置及开机启动 经过两天的各种尝试总算解决了,总结如下: 1.在建立库时注意 要让库的名称和 要同步的 web目录名 ...
- 使用apache benchmark(ab) 测试报错汇总
1.socket: Too many open files (24) 解决方法: [root@zabbix ~]# ulimit -a core file size (blocks, -c) 0 da ...
- CSS高级技巧 图标字体ICONFONT的使用方法视频
图标字体 iconfont 这是一种字体,它跟svg 有很大 相似点 它是矢量的,放大缩小不失真的.很且很小. 我们把它成字看来. 字体 在 从ie4就开始支持的. 兼容性很好 唯一麻烦的地方 ...
- 使用Maven打包项目并上传到Linux服务器
Maven打包: 项目右键Run as-->Maven build...--> 出来下面的界面,注意红色部分的填写,Goals填写package表示打包,下面的Skip Tests表示打 ...
- MongoDB 操作手冊CRUD查询指针
枚举遍历指针 概述 前面已经讲过,db.collection.find()假设没有指定给一个var声明的变量.将自己主动枚举前20条记录. 手动枚举指针 在mongo控制台中.将查询赋给一个var声明 ...