POJ3468 A Simple Problem with Integers 题解&代码
来源:互联网 发布:跟淘宝一样的购物平台 编辑:程序博客网 时间:2024/05/18 01:26
题意:给出n个数排成一列,有q个操作,分为Q和C,Q操作[l,r]询问区间[l,r]的区间和,C操作[l,r]对[l,r]区间同时增加x,按题意输出就行了
题解:线段树,主要是好久没写又错了好长一段时间…万年LL我就不多说了,看错范围觉得sum[]不需要LL,另外重点是query和insert不可能越界访问,所以就算区间对于这组数据是无效区间,只要访问到对应区间也一定要pushdown…忘了结果纠结了好久,小数据还找不出问题
#include<iostream>#include<cstdio>#define LL long long#define lson (o<<1)#define rson ((o<<1)|1)using namespace std;const int maxn = 100005;int n,q,l,r,c,a[maxn],lazy[4*maxn];LL sum[4*maxn];char s[5];void build(int o,int l,int r){ if(l==r) { sum[o]=(LL)a[l]; return; } int mid = (l+r)/2; build(lson,l,mid); build(rson,mid+1,r); sum[o]=sum[lson]+sum[rson];}void pushdown(int o,int l,int r){ if(lazy[o] && l!=r) { int mid = (l+r)/2; lazy[lson]+=lazy[o]; lazy[rson]+=lazy[o]; sum[lson]+=(LL)lazy[o]*(LL)(mid-l+1); sum[rson]+=(LL)lazy[o]*(LL)(r-mid); } lazy[o]=0;}void Insert(int o,int l,int r,int L,int R,int c){ pushdown(o,l,r); if(l>=L && r<=R) { lazy[o]+=c; sum[o]+=(LL)(r-l+1)*(LL)lazy[o]; pushdown(o,l,r); return; } if(l>R || r<L)return; int mid = (l+r)/2; Insert(lson,l,mid,L,R,c); Insert(rson,mid+1,r,L,R,c); sum[o]=sum[lson]+sum[rson];}LL query(int o,int l,int r,int L,int R){ pushdown(o,l,r); if(l>=L && r<=R)return sum[o]; if(l>R || r<L)return 0; int mid = (l+r)/2; LL ret=query(lson,l,mid,L,R)+query(rson,mid+1,r,L,R); sum[o]=sum[lson]+sum[rson]; return ret;}int main(void){ scanf("%d%d",&n,&q); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); build(1,1,n); for(int i = 0; i < q; i++) { scanf("%s%d%d",s,&l,&r); if(s[0]=='C') { scanf("%d",&c); Insert(1,1,n,l,r,c); } else printf("%lld\n",query(1,1,n,l,r)); } return 0;}
0 0
- POJ3468 A Simple Problem with Integers 题解&代码
- poj3468 A Simple Problem with Integers
- POJ3468:A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468-A Simple Problem with Integers
- POJ3468--A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- [POJ3468]A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- hadoop调试技巧--解除已经绑定的端口
- Android自定义没有资源文件的圆形进度条ProgressBar
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- (一)Mina源码解析之整体架构
- android安卓手机分屏多窗口实现方法
- POJ3468 A Simple Problem with Integers 题解&代码
- itoa函数的用法
- ListView的优化整理
- PHP 实现文件的writeInt writeUTF readInt readUTF
- java final
- smart学习之旅
- MySQL创建存储过程
- RabbitMQ安装
- RPC——远程过程调用协议