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. LTE学习笔记(一)——背景知识

    一.标准化组织 无线通信技术的演进离不开一些标准化组织. 1.ITU(International Telecommunication Union) 国际电信联盟,主要任务是制定标准,分配无线频谱资源, ...

  2. 编写高质量java代码151个建议

    http://blog.csdn.net/aishangyutian12/article/details/52699938 第一章  Java开发中通用的方法和准则 建议1:不要在常量和变量中出现易混 ...

  3. C语言数据结构基础学习笔记——栈和队列

    之前我们学过了普通的线性表,接下来我们来了解一下两种特殊的线性表——栈和队列. 栈是只允许在一端进行插入或删除的线性表. 栈的顺序存储结构也叫作顺序栈,对于栈顶指针top,当栈为空栈时,top=-1: ...

  4. C# 自定义异常的方法源码演示及说明

    内容之余,把做工程过程中较好的内容段备份一下,下边内容是关于C# 自定义异常的方法演示及说明的内容,希望能对各位朋友有一些好处. using System;using System.Collectio ...

  5. Flask与WSGI

    刚开始接触到python及Flask框架时,总是会听到 wsgi等等相关的名词,以及 项目部署时会用到nginx+gunicorn等等,但是对于一个请求从 nignx到gunicorn再到falsk框 ...

  6. python3:实现字符串的全排列(有重复字符)

    抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321. 解决方案 #字符串任意两个位置字符交换 def str_replace(str, x ...

  7. iptables随笔

    iptables 分为四表五链 四表: filter表 nat 表 mangle 表 raw 表 五链 INPUT 链 OUTPUT 链 FORWARD 链 PREROUTING(路由前) POSTR ...

  8. 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误

    转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...

  9. Unity3D AssetBundle相关

    Unity3D AssetBundle相关 首先,先看一下原理吧  Unity3D研究院之Assetbundle的原理(六十一) 其次,接着往下看:Unity3D研究院之Assetbundle的实战( ...

  10. 楚乔传 Princess Agents

    英文片名是:Princess Agents 公主特工,哈哈,女主角是公主...所有隐藏的线索都暴露了...这么搞笑呢.