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. 解决mysql使用GTID主从复制错误问题

    做MySQL主从的话肯定会遇到很多同步上的问题,大多数都是由于机器宕机,重启,或者是主键冲突等引起的从服务器停止工作,这里专门收集类似问题并提供整理解决方案,仅供参考. 1.主从网络中断,或主服务器重 ...

  2. 反序列化和序列化xml使用反射处理节点的属性

    当一个xml中有大量的属性XmlAttribute需要序列化和反序列化,通常需要复制粘贴大量的如下代码,显得很丑陋,而且容易出错: XmlAttribute attr = Doc.CreateAttr ...

  3. Quartz的API简介及Jobs和Trigger介绍

    Quartz的API: 主要api: The key interfaces of the Quartz API are: Scheduler - the main API for interactin ...

  4. tips:Jquery的attr和prop的区别

    Jquery的attr和prop的区别 描述:想做一个复选框checkbox全选的功能,当勾选全选后,将子项的复选框状态设置成一致的, 但遇到了一个问题,就是attr函数并不能改变子项的checkbo ...

  5. kubernetes学习笔记之十二:资源指标API及自定义指标API

    第一章.前言 以前是用heapster来收集资源指标才能看,现在heapster要废弃了从1.8以后引入了资源api指标监视 资源指标:metrics-server(核心指标) 自定义指标:prome ...

  6. leetcode每日刷题计划-简单篇day1

    orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...

  7. python 字典中 重复值去除

    tuple_r_dict = lambda _dict: dict(val[::-1] for val in _dict.items()) # Python3.x tuple_r_dict(tuple ...

  8. log4j配置后行号乱码显示为?问号

    debug="true"  classpathref="accrual.path" > 首发于 http://blog.xfuse.cn 参考文档 htt ...

  9. __str__ 和 __repr

    #1 默认类里面默认提供的__str__方法,是返回类的内存地址class foo: def __init__(self): pass #2 修改类里面默认提供的__str__方法class fun: ...

  10. CentOS 7 无法yum安装解决方法

    1)下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7.repo 2)备份并替换系统的repo文件 .repo /etc/yum.repos.d ...