POJ3468:A Simple Problem with Integers——题解
http://poj.org/problem?id=3468
实现一个线段树,能够做到区间修改和区间查询和。
明显板子题。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
inline ll read(){
ll X=,w=; char ch=;
while(ch<'' || ch>'') {w|=ch=='-';ch=getchar();}
while(ch>='' && ch<='') X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
ll tree[],b[];
ll lazy[];
void build(int a,int l,int r){
lazy[a]=;
if(l==r){
tree[a]=b[l];
return;
}
int mid=(l+r)/;
build(a*,l,mid);
build(a*+,mid+,r);
tree[a]=tree[a*]+tree[a*+];
return;
}
inline void push(int a,int l,int mid,int r){
lazy[a*+]+=lazy[a];
lazy[a*]+=lazy[a];
tree[a*+]+=lazy[a]*(r-mid);
tree[a*]+=lazy[a]*(mid-l+);
lazy[a]=;
return;
}
ll wen(int a,int l,int r,int l1,int r1){
if(l1<=l&&r1>=r){
return tree[a];
}
if (l1>r||r1<l) return ;
int mid=(l+r)/;
push(a,l,mid,r);
return wen(a*,l,mid,l1,r1)+wen(a*+,mid+,r,l1,r1);
}
void gai(int a,int l,int r,int l1,int r1,int add){
if (l1>r||r1<l) return;
if(l1<=l&&r1>=r){
lazy[a]+=add;
tree[a]+=add*(r-l+);
return;
}
int mid=(l+r)/;
push(a,l,mid,r);
gai(a*,l,mid,l1,r1,add);
gai(a*+,mid+,r,l1,r1,add);
tree[a]=tree[a*]+tree[a*+];
return;
}
int main(){
int m=read();
int ha=read();
for(int i=;i<=m;i++){
b[i]=read();
}
build(,,m);
for(int i=;i<=ha;i++){
char w;
cin>>w;
if(w=='C'){
int a1=read();
int a2=read();
int a3=read();
gai(,,m,a1,a2,a3);
}else{
int x=read();
int y=read();
printf("%lld\n",wen(,,m,x,y));
}
}
return ;
}
POJ3468:A Simple Problem with Integers——题解的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- [POJ3468] A Simple Problem with Integers (Treap)
题目链接:http://poj.org/problem?id=3468 这题是线段树的题,拿来学习treap. 不旋转的treap. #include <cstdio> #include ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
随机推荐
- package.json中的devDependencies和dependencies有啥区别?
如果你的项目是发布到npm的一个包, 那么这个包的package.json中的dependencies中的依赖是会被下载下来到这个包的node_modules文件夹中的(如果你的项目本身没有这个依赖) ...
- asp.net core2.1项目应用Ant Design(一)
无意中发现了Ant Design这个组件库后,深深被他丰富的组件吸引了,大家感兴趣的可以去官网感受下,组件的应用和效果真是的太强大了,对于我们这些小公司,无自主研发前端团队的来说,无疑特别方便:htt ...
- 前端开发工程师 - 02.JavaScript程序设计 - 期末考试
期末考试客观题 期末考试主观题 https://www.15yan.com/story/aY0HWAQ7oNU/ 1(8分) 函数myType用于根据输入参数返回相应的类型信息. 语法如下: ...
- 【wx:for】小程序列表渲染的使用说明
wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件. 默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item,即: {{index}} . {{it ...
- Spring Cloud(十一):服务网关 Zuul(过滤器)【Finchley 版】
Spring Cloud(十一):服务网关 Zuul(过滤器)[Finchley 版] 发表于 2018-04-23 | 更新于 2018-05-07 | 在上篇文章中我们了解了 Spring ...
- 安迪的第一个字典 (Andy's First Dictionary,UVa10815)
题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...
- New Year and Domino:二维前缀和
题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...
- Linux内核设计笔记11——定时器
定时器与时间管理笔记 内核中的时间 时钟中断:内核中的系统定时器以某种频率触发中断,该频率可以通过编程预定. 节拍率HZ:时钟中断的频率称为节拍率. 节拍:相邻两次中断的时间间隔称为节拍,1/节拍率. ...
- Java IO(文件操作工具类)
FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...
- 2.安装hdfs yarn
下载hadoop压缩包设置hadoop环境变量设置hdfs环境变量设置yarn环境变量设置mapreduce环境变量修改hadoop配置设置core-site.xml设置hdfs-site.xml设置 ...