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 推公式 + 线段树的更多相关文章

  1. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  2. 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 ...

  3. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  4. Codeforces Round #545 (Div. 2) 交互 + 推公式

    https://codeforces.com/contest/1138/problem/F 题意 有一条长为t的链,一个长为c的环,定义终点为链和环相连环上的第一个点,现在有10个人在起点,你每次可以 ...

  5. 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 ...

  6. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 推导式_字典_enumerate

    字典推导式_enumerate: ''' 功能: 枚举, 拿出iter的每一个元素和索引(可以设置start改变) 组队放入一个元祖中返回 参数:iterable, start(指定索引开始的位置) ...

  2. git 退回到前面某一版本的具体操作方式

  3. cookie的中文乱码问题【URL编码解码】

    先搞明白为什么会乱码,为什么要转码: 在tomcat 8 之前,cookie中不能直接存储中文数据.需要将中文数据转码,一般采用URL编码(%E3).在tomcat 8 之后,cookie支持中文数据 ...

  4. 杂谈2.cpp

    Ostream类定义的插入运算符(<<)使数据插入到输出流,istream定义的抽取运算符(>>)能够从输入流中抽取信息 Cin和cout都是智能对象,能够根据程序上下文将信息 ...

  5. Android 梯形进度条、下载进度条;

    额,Gif有点卡: 梯形.矩形.圆角.背景色.前景色.进度条中的文字都可以改: <?xml version="1.0" encoding="utf-8"? ...

  6. SHFileOperation 解决double-null terminated

    void rubyTools::funStrToWstr(string str, wstring& strw) { const char* pData = str.c_str(); int l ...

  7. ATS配置自定义日志

    修改records.config,开启日志自定义功能 更改日志目录,默认日志存放在/var/log/trafficserver: CONFIG proxy.config.log.logfile_dir ...

  8. python 正则表达式 RE模块汇总记录

    re.compile(pattern, flags=0) re.search(pattern, string, flags=0) re.match(pattern, string, flags=0) ...

  9. python——元组和字典类型简明理解

    元组类型: 元祖创建: 不需要括号可以但是一个元素就当成了字符串类型了 >>> tup1="a"; >>> type(tup1) <cla ...

  10. [译]与TensorFlow的第一次接触(三)之聚类

    转自 [译]与TensorFlow的第一次接触(三)之聚类 2016.08.09 16:58* 字数 4316 阅读 7916评论 5喜欢 18 前一章节中介绍的线性回归是一种监督学习算法,我们使用数 ...