题目链接:

http://codeforces.com/contest/1136/problem/E

题意:

初始有a数组和k数组

有两种操作,一,求l到r的区间和,二,$a_i\pm x$

并且会有一个连锁反应

$$while\left ( a_{i+1}<a_i+k_i \right )a_{i+1}=a_i+k_i,i++ $$

数据范围:

$2 \leq n \leq 10^{5}$
$-10^{9} \leq a_i \leq 10^{9}$
$-10^{6} \leq k_i \leq 10^{6}$
$1 \leq q \leq 10^{5}$
$1 \leq i \leq n$,$0 \leq x \leq 10^{6}$
$1 \leq l \leq r \leq n$


分析:

对于每次修改,我们可以用二分查找到连锁的末尾。

而对于一个被修改后的区间$(i,r)$的元素$a_x$,它由两部分组成$a_x=a_i+\sum_{j=i}^{x-1}k_j$

两部分的值都可以轻易算出,然后用两颗线段树分别记录两部分的区间和(一颗线段树也行)。

用到前缀和的前缀和,还有懒惰标记

具体实现见ac代码

ac代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
const ll INF=1e18;
ll sum1[maxn],sum2[maxn],treea[4*maxn],treeb[4*maxn],lazya[4*maxn],lazyb[4*maxn];
int a[maxn];
void bulida(int l,int r,int rt)
{
int md=(r+l)/2;
if(r==l)
{
treea[rt]=a[l];
return;
}
bulida(l,md,rt*2);
bulida(md+1,r,rt*2+1);
treea[rt]=treea[rt*2]+treea[rt*2+1];
}
void pushdowna(int l,int r,int rt)
{
int md=(l+r)/2;
if(lazya[rt]!=-INF)
{
treea[rt*2]=(md-l+1)*lazya[rt];
treea[rt*2+1]=(r-md-1+1)*lazya[rt];
lazya[rt*2]=lazya[rt*2+1]=lazya[rt];
lazya[rt]=-INF;
}
}
ll quera(int l,int r,int nowl,int nowr,int rt)
{
if(r<nowl||l>nowr)return 0;
int md=(nowr+nowl)/2;
if(l<=nowl&&r>=nowr)return treea[rt];
pushdowna(nowl,nowr,rt);
return quera(l,r,nowl,md,rt*2)+quera(l,r,md+1,nowr,rt*2+1);
}
void updataa(ll x,int l,int r,int nowl,int nowr,int rt)
{
if(r<nowl||l>nowr)return ;
int md=(nowr+nowl)/2;
if(l<=nowl&&r>=nowr)
{
treea[rt]=(nowr-nowl+1)*x;
lazya[rt]=x;
return ;
}
pushdowna(nowl,nowr,rt);
updataa(x,l,r,nowl,md,rt*2);
updataa(x,l,r,md+1,nowr,rt*2+1);
treea[rt]=treea[rt*2]+treea[rt*2+1];
} void pushdownb(int l,int r,int rt)
{
int md=(l+r)/2;
if(lazyb[rt]!=-INF)
{
treeb[rt*2]=sum2[md-1]-sum2[l-2]+(l-md-1)*sum1[lazyb[rt]-1];
treeb[rt*2+1]=sum2[r-1]-sum2[md+1-2]+(md+1-r-1)*sum1[lazyb[rt]-1];
lazyb[rt*2]=lazyb[rt*2+1]=lazyb[rt];
lazyb[rt]=-INF;
}
}
void updatabb(ll x,int pos,int nowl,int nowr,int rt)
{
int md=(nowr+nowl)/2;
if(nowl==nowr)
{
treeb[rt]=x;
return ;
}
pushdownb(nowl,nowr,rt);
if(pos>=md+1)updatabb(x,pos,md+1,nowr,rt*2+1);
else updatabb(x,pos,nowl,md,rt*2);
treeb[rt]=treeb[rt*2]+treeb[rt*2+1];
}
ll querb(int l,int r,int nowl,int nowr,int rt)
{
if(r<nowl||l>nowr)return 0;
int md=(nowr+nowl)/2;
if(l<=nowl&&r>=nowr)return treeb[rt];
pushdownb(nowl,nowr,rt);
return querb(l,r,nowl,md,rt*2)+querb(l,r,md+1,nowr,rt*2+1);
}
void updatab(ll x,int l,int r,int nowl,int nowr,int rt)
{
if(r<nowl||l>nowr)return ;
int md=(nowr+nowl)/2;
if(l<=nowl&&r>=nowr)
{
treeb[rt]=sum2[nowr-1]-sum2[nowl-2]+(nowl-nowr-1)*sum1[x-1];
lazyb[rt]=x;
return ;
}
pushdownb(nowl,nowr,rt);
updatab(x,l,r,nowl,md,rt*2);
updatab(x,l,r,md+1,nowr,rt*2+1);
treeb[rt]=treeb[rt*2]+treeb[rt*2+1];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n-1; i++)
{
int x;
scanf("%d",&x);
sum1[i]=sum1[i-1]+x;
sum2[i]=sum2[i-1]+sum1[i];
}
for(int i=0; i<4*maxn; i++)lazya[i]=lazyb[i]=-INF;
bulida(1,n,1);
int T;
scanf("%d",&T);
while(T--)
{
getchar();
char key;
scanf("%c",&key);
if(key=='s')
{
int l,r;
scanf("%d %d",&l,&r);
printf("%lld\n",quera(l,r,1,n,1)+querb(l,r,1,n,1));
}
else if(key=='+')
{
ll x,add;
scanf("%lld %lld",&x,&add);
add=quera(x,x,1,n,1)+querb(x,x,1,n,1)+add;
int st=x,en=n;
while(st!=en)
{
int md=(st+en)/2;
if(sum1[md+1-1]-sum1[x-1]+add>=querb(md+1,md+1,1,n,1)+quera(md+1,md+1,1,n,1))st=md+1;
else en=md;
}
updataa(add,x,st,1,n,1);
updatab(x,x+1,st,1,n,1);
updatabb(0,x,1,n,1);
}
}
return 0;
}

  

codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)的更多相关文章

  1. cf1136E. Nastya Hasn't Written a Legend(二分 线段树)

    题意 题目链接 Sol yy出了一个暴躁线段树的做法. 因为题目保证了 \(a_i + k_i <= a_{i+1}\) 那么我们每次修改时只需要考虑取max就行了. 显然从一个位置开始能影响到 ...

  2. Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]

    题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...

  3. Codeforces 1136E Nastya Hasn't Written a Legend 线段树

    vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...

  4. Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)

    题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后, ...

  5. CF1136E Nastya Hasn't Written a Legend(线段树)

    还能说什么呢,简直太妙了. $$a_{i+1}<a_i+k_i$$ $$a_{i+1}-k_i-k_{i-1}-\cdots-k_1<a_i+k_i-k_i-k_{i-1}-\cdots- ...

  6. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  7. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  8. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  9. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

随机推荐

  1. Python:tesserocr 在 windows 下的安装及简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...

  2. 第一册:lesson 115.

    原文:Knock,Knock! question:What does Jim have to drink? Isn't there anyone at home? I'll knock again , ...

  3. c# 正则表达式替换字符串中常见的特殊字符

    第一种,若字符串中含有字母,则使用以下方法 public static string RemoveSpecialCharacterToupper(string hexData) { //下文中的‘\\ ...

  4. Python基础学习01

    1.编译型解释型语言区别: 编译型:一次性将全部代码编译成二进制文件,代表c,c++ 优点:执行效率高 缺点:开发速度慢,不能跨平台 解释型:当程序运行时,从上至下一行一行执行,解释成二进制去执行 优 ...

  5. C#的一些获取时间的例子

    从周一到周日的顺序,获取排序数值: int i = DateTime.Now.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6; 获取某日起,星期一日期 ...

  6. EF 外键不显示、如何让外键显示!增、删、改 操作时,外键不显示,只显示导航属性!

    一.问题描述:EF 外键不显示.如何让外键显示!增.删.改 操作时,外键不显示,只显示导航属性! EF 添加.增加.插入数据时,外键不显示! 二.解决方案:在根据数据库生成模型的时候,选中“在模型中” ...

  7. SpringBoot2 java配置方式 Configuration和PropertySource结合读取配置文件

    JdbcConfig.java Configuration是配置文件 PropertySource 引入配置文件 value读取配置文件内容 package cn.itcast.config; imp ...

  8. JAVA IO流编程 实现文件的写入、写出以及拷贝

    一.流的概念 流:数据在数据源(文件)和程序(内存)之间经历的路径. 输入流:数据从数据源(文件)到程序(内存)的路径. 输出流:数据从程序(内存)到数据源(文件)的路径. 以内存为参照,如果数据向内 ...

  9. 如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值

    方法一:通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice servlet中 String noticeId=request.getParameter("n ...

  10. Java基础小知识笔记

    1. Integer转进制的一个类2. toBinaryString,toOctalString,toHexString.(转为二进制,八进制,十六进制的方法)3. 如果·数据的大小没有超过byte/ ...