Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E
题意
给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作:
1.将a[i]+x,假如a[i]+k[i]>a[i+1],则a[i+1]要变成a[i]+k[i],直到某个a[j]+k[j]<=a[j+1]
2.询问某个区间的和
题解
利用题目性质将题目转化到你会的东西的性质
令\(t_i=k_1+..+k_{i-1},b_i=a_i-t_i\)
\(a_i \geq a_{i-1} + k_{i-1}\)
\(a_i - k_{i-1} \geq a_{i-1}\)
\(a_i - k_{i-1}-...-k_1 \geq a_i - k_{i-2}-...-k_1\)
\(a_i - t_i \geq a_{i-1} - t_{i-1}\)
\(b_i \geq b_{i-1}\)
即假如\(b_i\)小于\(b_{i-1}+x\)只需要将\(b_i\)修改成\(b_{i-1}+x\)
二分找到\(b_r\)大于\(b_{i-1}+x\),区间修改
代码
#include<bits/stdc++.h>
#define ll long long
#define M 100005
#define ls (o<<1)
#define rs (o<<1|1)
#define inf 1e17
using namespace std;
ll a[M],k[M],b[M],t[M],ly[M<<2],X[M<<2],ans;
int n,i,q;
ll x,y,l,r,mid,tp;
char S[3];
void push_up(int o){
X[o]=X[ls]+X[rs];
}
void push_down(int o,int l,int r){
int mid=(l+r)/2;
if(ly[o]!=inf){
X[ls]=ly[o]*(mid-l+1);
X[rs]=ly[o]*(r-mid);
ly[ls]=ly[rs]=ly[o];
ly[o]=inf;
}
}
void build(int o,int l,int r){
int mid=(l+r)/2;
ly[o]=inf;
if(l==r){
X[o]=b[l];return;
}
build(ls,l,mid);build(rs,mid+1,r);
push_up(o);
}
void ud(int o,int l,int r,int L,int R,ll x){
int mid=(l+r)/2;
if(L<=l&&r<=R){
ly[o]=x;X[o]=x*(r-l+1);
return;
}
push_down(o,l,r);
if(L<=mid)ud(ls,l,mid,L,R,x);
if(R>mid)ud(rs,mid+1,r,L,R,x);
push_up(o);
}
ll qy(int o,int l,int r,int L,int R){
int mid=(l+r)/2;
if(L<=l&&r<=R){
return X[o];
}
ll ans=0;
push_down(o,l,r);
if(L<=mid)ans+=qy(ls,l,mid,L,R);
if(R>mid)ans+=qy(rs,mid+1,r,L,R);
return ans; //
}
int main(){
cin>>n;
for(i=1;i<=n;i++)scanf("%lld",&a[i]);
for(i=1;i<n;i++){scanf("%lld",&k[i]);k[i]+=k[i-1];t[i]=t[i-1]+k[i];b[i]=a[i]-k[i-1];}
b[n]=a[n]-k[n-1];
build(1,1,n);
cin>>q;
while(q--){
scanf("%s%lld%lld",S,&x,&y);
if(S[0]=='s'){
printf("%lld\n",qy(1,1,n,x,y)+t[y-1]-(x>=2?t[x-2]:0));
}else{
l=x;r=n;
tp=qy(1,1,n,x,x)+y;
while(l<r){
mid=(l+r)/2;
if(qy(1,1,n,mid,mid)>=tp)r=mid;
else {
l=mid+1;
}
}
if(qy(1,1,n,l,l)>tp)l--;
//cout<<r<<endl;
ud(1,1,n,x,l,tp);
}
}
}
Codeforces Round #546 (Div. 2) E 推公式 + 线段树的更多相关文章
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #545 (Div. 2) 交互 + 推公式
https://codeforces.com/contest/1138/problem/F 题意 有一条长为t的链,一个长为c的环,定义终点为链和环相连环上的第一个点,现在有10个人在起点,你每次可以 ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- Codeforces Round #275 Div.1 B Interesting Array --线段树
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony 线段树
F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
随机推荐
- Lepus监控之Oracle配置
1.安装cx_Oracle a.官网下载客户端组件包 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpmoracle-instantclien ...
- mysql数据库优化(三)--分区
mysql的分区,分表 分区:把一个数据表的文件和索引分散存储在不同的物理文件中. 特点:业务层透明,无需任何修改,即使从新分表,也是在mysql层进行更改(业务层代码不动) 分表:把原来的表根据条件 ...
- [java,2017-05-04] 创建word文档
package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...
- Sublime Text 3激活
Sublime Text 3激活方式: 一.修改hosts文件: 1:windows系统: 找到 C:\Windows\System32\drivers\etc\hosts 这个文件, 用 ...
- Openface 入门
Openface 简单入门 背景 Openface是一个开源的人脸识别框架,同类软件产品还有 seetaface ,DeepID等,当然,如果算上商业的产品,那就更多了. Openface人脸比对结果 ...
- C#设计模式(1)——单例模式(Singleton)
单例模式即所谓的一个类只能有一个实例, 也就是类只能在内部实例一次,然后提供这一实例,外部无法对此类实例化. 单例模式的特点: 1.只能有一个实例: 2.只能自己创建自己的唯一实例: 3.必须给所有其 ...
- python大法好——Python2.x与3.x版本区别
python大法好——Python2.x与3.x版本区别 Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级. 为了不带 ...
- js:捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- 2017-11-04 Sa OCT codecombat
def hasEnemy(): e = hero.findNearestEnemy() if e: return True else: return False def enemyTooClose() ...
- typedef typename
所以根据上述两条分析, typedef typename RefBase::weakref_type weakref_type; 语句的真是面目是: typedef创建了存在类型的别名,而typen ...