PKU 3468 A Simple Problem with Integers
题目大意:
有N,M两个数
Q 表示查询, 后面两个数a b,表示查询从a 到b计算它们的和
C 表示增加 后面三个数a,b,c 表示从a开始,一直到b,每个数都增加c
除了查询要进行输出,增加不要输出
#include<iostream> using namespace std; #define LL(x)((x)<<1) #define RR(x)((x)<<1|1) struct_Seg_Tree{ int left,right; long long sum; int add; int calmid(){ return (left+right)/2; } int caldis(){ return right-left+1; } }tt[1000000]; int val[1000002]; long long build(int l,int r,int idx){ tt[idx].left=l; tt[idx].right=r; tt[idx].add=0; if(l==r){ return tt[idx].sum=val[l]; } return tt[idx].sum=build(l,mid,LL(idx)+build(mid+1,r,RR(idx))); } void update(int l,int r,int add,int idx){ if(l<=tt[idx].left&&r>=tt[idx].right){ tt[idx].add+=add; tt[idx].sum+=(long long)tt[idx].caldis()*add;//通过乘以后来添加的数 return ; } if(tt[idx].add){ tt[LL(idx).sum]+=(long long)tt[LL(idx)].caldis()*tt[idx].add; tt[RR(idx).sum]++(long long)tt[RR(idx)].caldis()*tt[idx].add; tt[LL(idx)].add+=tt[idx].add; tt[RR(idx)].add+=tt[idx].add; tt[idx].add=0; } int mid=tt[idx].calmid(); if(l<=mid) update(l,r,add,LL(idx)); if(mid<r) update(l,r,add,RR(idx)); tt[idx].sum = tt[LL(idx)].sum + tt[RR(idx)].sum; } long long query(int l,int r,int idx){ if(l==tt[idx].left&&r==tt[idx].right){ return tt[idx].sum; } if(tt[idx].add){ tt[LL(idx)].sum += (long long )tt[LL(idx)].caldis() * tt[idx].add; tt[RR(idx)].sum += (long long)tt[RR(idx)].caldis() * tt[idx].add; tt[LL(idx)].add += tt[idx].add; tt[RR(idx)].add += tt[idx].add; tt[idx].add = 0; } int mid=tt[idx].calmid(); if(r<=mid){ return query(l,r,LL(idx)); } else if(mid<1){ return query(l,r,RR(idx)); } else{ return query(l,mid,LL(idx)+query(mid+1,r,RR(idx)); } } int main(){ int n,m; while(scanf("%d %d",&n,&m)==2){ for(int i=1;i<=n;i++) scanf("%d",&val[i]); build(1,n,1); while(m--){ char com[2]; scanf("%s",com); if(com[0]=='Q'){ int a,b; scanf("%d %d",&a,&b); pirntf("lld\n",query(a,b,1)); }else{ int a,b,c; scanf("%d %d %d",&a,&b,&c); update(a,b,c,1); } } } }
PKU 3468 A Simple Problem with Integers的更多相关文章
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- 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(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- Unity中使用射线查询MeshCollider背面的方法
之前遇到一个问题要从MeshCollider背面方向发出射线,直至检测到该射线与MeshCollider的相交点为止. 后来我用双面MeshCollider的方法解决了http://www.cnblo ...
- Python3 基础语法
编码 默认情况下,Python 3源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 标 ...
- Oracle中rownum的说明及使用技巧
一.rownum的说明 rownum是oracle特有的一个关键字. (1)对于基表,在insert记录时,oracle就按照insert的顺序,将rownum分配给每一行记录,因此在select一个 ...
- Android文件大头10G
这个玩意直接10G....记录下. C:\Users\xn\AppData\Local\Android\sdk\system-images\android-23
- Mybatis源码分析--关联表查询及延迟加载原理(二)
在上一篇博客Mybatis源码分析--关联表查询及延迟加载(一)中我们简单介绍了Mybatis的延迟加载的编程,接下来我们通过分析源码来分析一下Mybatis延迟加载的实现原理. 其实简单来说Myba ...
- 重温java基础
Java标识符 Java所有的组成部分都需要名字.类名.变量名以及方法名都被称为标识符. 关于Java标识符,有以下几点需要注意: 所有的标识符都应该以字母(A-Z或者a-z),美元符($).或者下划 ...
- 基于hadoop的BI架构
BI系统,是企业利用数据驱动运营的一个典型系统.BI系统通过发掘企业运行过程中的数据,发现企业的潜在风险.为企业的各项决策提供数据支撑. 传统的BI系统通常构建于关系型数据库之上.随着企业业务量的增大 ...
- Apache shiro集群实现 (二) shiro 的INI配置
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- not in 前面/后面存在null值时的处理
表声明 order_header表中有ship_method列: ship_method_map表中ship_method为主键列. 需求 找出order_header表中所有ship_method不 ...
- 学习TensorFlow,TensorBoard可视化网络结构和参数
在学习深度网络框架的过程中,我们发现一个问题,就是如何输出各层网络参数,用于更好地理解,调试和优化网络?针对这个问题,TensorFlow开发了一个特别有用的可视化工具包:TensorBoard,既可 ...