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 第 ...
随机推荐
- MVC下的Area区域知识点
新建area区域 1.如果与根目录下的url相同,那么需要在RouteConfig.cs中 public static void RegisterRoutes(RouteCollection rout ...
- 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)
分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...
- List和Turple
List 格式:classmates = ['Michael', 'Bob', 'Tracy'] 读取list长度用:len(classmetes) 索引:索引正向从0开始,逆向从-1开始 在末尾增加 ...
- 《Spark 官方文档》在Mesos上运行Spark
本文转自:http://ifeve.com/spark-mesos-spark/ 在Mesos上运行Spark Spark可以在由Apache Mesos 管理的硬件集群中运行. 在Mesos集群中使 ...
- [Clr via C#读书笔记]Cp8方法
Cp8方法 构造器 作用就是初始化所有成员字段:.ctor:派生类和基类都有自己的构造函数.默认有一个无参数的构造函数,值字段初始化为0,引用字段初始化为null:可以有多个构造器: 值类型的初始化其 ...
- Python3 Tkinter-Menu
1.创建 from tkinter import * root=Tk() menubar=Menu(root) def hello(): print('Hello Menu!') for item i ...
- mac上golang编译出现clang错误
错误现象 几周前,突然发现我的go 项目编译开始报一种以前从来没有出现过的错误: # runtime/cgo clang: warning: argument unused during compil ...
- Eclipse 安装SVN、Maven插件
1先安装subeclipse插件就是svn svn - http://subclipse.tigris.org/update_1.6.x 我这里是灰色的说明我安装过了这里只是截图说明下,我就不继续安装 ...
- POJ 1696 Space Ant(凸包变形)
Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scie ...
- POJ 1113 Wall(计算几何の凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...