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——题解的更多相关文章

  1. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  2. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  3. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  4. poj------(3468)A Simple Problem with Integers(区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...

  5. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   ...

  6. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  7. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  8. [POJ3468] A Simple Problem with Integers (Treap)

    题目链接:http://poj.org/problem?id=3468 这题是线段树的题,拿来学习treap. 不旋转的treap. #include <cstdio> #include ...

  9. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

随机推荐

  1. iOS 开发库相关(持续更新)

    01-给任意view添加毛玻璃效果 https://github.com/JagCesar/iOS-blur   02-浮动式的textfield输入框(可用于登录界面) https://github ...

  2. 金山WPS面试题

    1.windows的handle 1)是一个宏定义#define void* HANDLE 2) HANDLE提供了一种统一的方式去获得系统资源,并对其进行操作. 3) HANDLE使得程序设计的细节 ...

  3. cf#516B. Equations of Mathematical Magic(二进制,位运算)

    https://blog.csdn.net/zfq17796515982/article/details/83051495 题意:解方程:a-(a^x)-x=0 给出a的值,要求计算解(非负)的个数 ...

  4. HTML随笔3

    1. *svg(可伸缩矢量图)标签画圆,其中r表示半径,cx和cy表示其圆心的坐标 <svg><circle r="100" cx="200" ...

  5. OSG-获取OSG的源代码和第三方库并编译

    获取OSG的源代码有很多方式. 这里说下其中的两个地方,第一就是中国的OSG网站http://www.osgchina.org/,这个网站目前应该是由中国西安恒歌科技维护,同时,西安恒歌科技也是一家已 ...

  6. 一种新的自动化 UI 测试解决方案 Airtest Project

    今天分享一个自动化UI测试工具airtest——一款网易出品的基于图像识别面向游UI测试的工具,也支持原生Android App基于元素识别的UI自动化测试.主要包含了三部分:Airtest IDE. ...

  7. 【C++模版之旅】项目中一次活用C++模板(traits)的经历 -新注解

    问题与需求: 请读者先看这篇文章,[C++模版之旅]项目中一次活用C++模板(traits)的经历. 对于此篇文章提出的问题,我给出一个新的思路. talking is cheap,show me t ...

  8. JDBC中使用Properties类及配置文件的操作

    同时发布于:https://blog.csdn.net/Activity_Time/article/details/81149710 一.properties配置文件 开发中获得连接的4个参数(驱动. ...

  9. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  10. 腾讯地图和百度地图的PHP相互转换

    /** * 百度地图---->腾讯地图 * @param double $lat 纬度 * @param double $lng 经度 * @return array(); */ functio ...