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 ...
随机推荐
- #error : Xiron Platform Abstraction Layer - Win32 - Microsoft Visual Studio versions above 2010 (10.0) are not supported! 解决方案
OpenNI1.5 VS2013配置环境后,编译会出现这个错误: 错误 error C1189: #error : Xiron Platform Abstraction Layer - Win32 - ...
- git提交项目常用命令及git分支的用法
1.第一步首先从git托管平台clone项目,我这里就使用idea为例: 填写git的url与存放本地目录名及项目名 2.如果你对项目进行了一些修改,就可以执行git命令,进行提交. 有两种方 ...
- .NET Core 网络数据采集 -- 使用AngleSharp做html解析
有这么一本Python的书: <<Python 网络数据采集>> 我准备用.NET Core及第三方库实现里面所有的例子. 这是第一部分, 主要使用的是AngleSharp: ...
- delphi 验证码识别(XE8源码)
如题:源码下载
- 克拉默法则(Cramer's Rule)的证明
克拉默法则: 先说一下为什么要写这个,作为一个大一新生,必须要学的就包括了线性代数,而且线性代数等数学知识对计算机专业也有很大帮助.但是在学习过程中遇到一个讲解的不清楚的知识点(Cramer's Ru ...
- python笔记八(切片)
一.切片 首先我们要记得在Python中可以用于切片的对象有 列表.元组.字符串. 切片操作就是直接从列表.元组或字符串中,选择出我们想要的内容,这些操作非常简洁实用. >>> L ...
- Bootstrap3 代码-变量
通过 <var> 标签标记变量. y = mx + b <var>y</var> = <var>m</var><var>x< ...
- springMVC源码分析--ModelAndViewContainer和ModelMap
ModelAndViewContainer主要是用来返回Model对象的,在ModelAndViewContainer中有defaultModel和redirectModel, defaultMode ...
- 为什么内部类访问的外部变量需要使用final修饰
因为生命周期的原因.方法中的局部变量,方法结束后这个变量就要释放掉,final保证这个变量始终指向一个对象.首先,内部类和外部类其实是处于同一个级别,内部类不会因为定义在方法中就会随着方法的执行完毕而 ...
- proc文件系统探索 之 根目录下的文件[1]
2.1根目录下的文件2.1.1lock文件内核锁,记录与被打开的文件有关的锁信息. 该文件显示当前被内核锁定的文件.该文件包含的内容是内核调试数据,根据使用的系统的这些数据会变化很大.一个/proc/ ...