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 ...
随机推荐
- [置顶] High Performance Canvas Game for Android
Rule #0 为移动平台进行优化 为移动平台进行优化是十分重要的,因为移动平台的性能大概只有桌面平台的1/10左右(*1),它通常意味着: 更慢的CPU速度,这意味着不经过优化的JavaScript ...
- linux常用命令加实例大全
目 录引言 1一.安装和登录 2(一) login 2(二) shutdown 2(三) halt 3(四) reboot 3(五) ...
- uvalive 2322 Wooden Sticks(贪心)
题目连接:2322 Wooden Sticks 题目大意:给出要求切的n个小木棍 , 每个小木棍有长度和重量,因为当要切的长度和重量分别大于前面一个的长度和重量的时候可以不用调整大木棍直接切割, 否则 ...
- FLASH ROM与EEPROM的区别
EEPROM,虽然也叫“非易失性数据存储器”,但它不能直接参与ALU运算,只是用于掉电不丢失的数据存储. EEPROM和片内RAM 类似,也属于数据存储器,它的特点是数据掉电可保持,而程序存储器一般指 ...
- 如何使用notepad++搭配MinGW配置编译C/C++
最经开始学C++了,平时学习不喜欢用IDE,一直以来都喜欢使用sublimetext写代码.所以在网上找了一下如何配置sublimetext编译C/C++.不过简单配置之后,只有输出,要想进行输出操作 ...
- [Swust OJ 138]--文件压缩
题目链接:http://acm.swust.edu.cn/problem/138/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- pay包注释(一)
lovep2c项目pay模块注释: views.py: def create_user_no(email): return md5(email).hexdigest().upper() + &q ...
- PHP脚本实现凯撒加(解)密
原文:PHP脚本实现凯撒加(解)密 今天在看某ctf时候遇到一题凯撒加密的题,然后看到write up里有这样一句 顿时感觉这题目有点坑啊,这要不写个脚本来跑要推到啥时候啊,于是又了本文: <? ...
- HDU 1025 DP + 二分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...
- Mysql 连接查询 Mysql支持的连接查询有哪些
CREATE TABLE `chx` ( `id` VARCHAR(20) NOT NULL, `name` VARCHAR(50) DEFAULT NULL, `name2` CHAR( ...