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. 使用Maven

    MyEclipse2015自带有Maven,但是建个工程老出错. (Eclipse J2EE也自带有) 1.下载maven.官网 http://maven.apache.org/download.cg ...

  2. node.js打印function

    var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...

  3. clear 属性

    clear属性:规定元素的哪一侧不允许有其他的浮动元素 Example: <html> <head> <style type="text/css"&g ...

  4. dll 恐怖的代码调整

    总结一波这998的不得不提的调整代码的心得. 调整代码的背景:现有wps美化代码分散在各个插件里面,导致每次修改一小部分代码,都要全新编译,并且只能跟版本发,所以决定将wps的美化代码整合成一个插件d ...

  5. canal 结合 kafka 入门

    1.kafka的安装: 略 2.cannal  配置 使用卡夫卡: 修改  /home/admin/canal-server/conf/canal.properties 2.1 修改canal.ser ...

  6. mysql数据库支持 emoji表情

    关键点: 1. 修改mysql数据库表ALTER table <tablename>  modify <clolum>  char(100) character set utf ...

  7. MySQL master-slave主从复制环境搭建初试

    环境为centos 7.2+mysql 5.7,网上教程很多,原理也不复杂(深知自己踩的坑还不够) 正常情况下,配置起来比较简单.另外,根据个人感受,MySQL的复制感觉要比SQL Server的复制 ...

  8. R语言-箱型图&热力图

    1.箱型图 boxplot()函数 > metals<-read.csv("metals.csv",header=TRUE) #读取文件和列名 > boxplot ...

  9. python入门 -- 学习笔记4

    习题38:列表的操作 当你看到像 mystuff.append('hello') 这样的代码时,你事实上已经在 Python 内部激发了一个连锁反应.以下是它的工作原理: 1. Python 看到你用 ...

  10. cdnbest里站点域名不同步到节点,报400错误的一般原因

    报400错误一般是站点里的域名没有同步到节点上面的原因,产生的原因一般是下面两点原因: 1.检查节点列表如下图所示的状态是否打钩,这是节点和主控的通信状态,打叉表示连接有问题 这里打叉的几种原因(1) ...