vp的时候没码出来。。

我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并,

可以发现我们最多合并O(n)次。 然后写个线段树就没了。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, q, val[N], k[N];
LL prefix[N];
set<PII> seg;
char op[]; LL a[N << ], add[N << ];
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
void push(int rt, int l, int mid, int r) {
if(add[rt]) {
a[rt << ] += add[rt] * (mid - l + );
a[rt << | ] += add[rt] * (r - mid);
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
add[rt] = ;
}
}
void build(int l, int r, int rt) {
if(l == r) {
a[rt] = val[l];
return;
}
int mid = l + r >> ;
build(lson); build(rson);
a[rt] = a[rt << ] + a[rt << | ];
} void update(int L, int R, LL val, int l, int r, int rt) {
if(l >= L && r <= R) {
a[rt] += val * (r - l + );
add[rt] += val;
return;
}
int mid = l + r >> ;
push(rt, l, mid, r);
if(L <= mid) update(L, R, val, lson);
if(R > mid) update(L, R, val, rson);
a[rt] = a[rt << ] + a[rt << | ];
}
LL query(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return a[rt];
int mid = l + r >> ;
push(rt, l, mid, r);
if(R <= mid) return query(L, R, lson);
else if(L > mid) return query(L, R, rson);
else return query(L, R, lson) + query(L, R, rson);
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &val[i]);
prefix[i] = prefix[i - ] + val[i];
}
for(int i = ; i < n; i++) scanf("%d", &k[i]);
for(int i = ; i <= n; i++) seg.insert(mk(i, i));
build(, n, );
scanf("%d", &q);
while(q--) {
scanf("%s", op);
if(op[] == '+') {
int x, v; scanf("%d%d", &x, &v);
auto sg = seg.upper_bound(mk(x, inf)); sg--;
int L = sg->fi, R = sg->se;
seg.erase(sg);
if(L <= x - ) seg.insert(mk(L, x - ));
update(x, R, v, , n, );
while(R < n) {
LL v1 = query(R, R, , n, );
LL v2 = query(R + , R + , , n, );
if(v1 + k[R] <= v2) break;
LL add = v1 + k[R] - v2;
auto sg = seg.lower_bound(mk(R + , R + ));
update(sg->fi, sg->se, add, , n, );
R = sg->se;
seg.erase(sg);
}
seg.insert(mk(x, R));
} else {
int L, R; scanf("%d%d", &L, &R);
printf("%lld\n", query(L, R, , n, ));
}
}
return ;
} /*
*/

Codeforces 1136E Nastya Hasn't Written a Legend 线段树的更多相关文章

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

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

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

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

  3. codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)

    题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...

  4. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

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

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

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

  7. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  8. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

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

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

随机推荐

  1. why should the parameter in copy construction be a reference

    if not, it will lead to an endless loop!!! # include<iostream> using namespace std; class A { ...

  2. python 函数 动态参数 和嵌套

    1.动态参数 是可以接收任意的参数.一种方式, 1,位置的动态传参, 写法是: *参数名 接收的参数是tuple类型举个例子:def yue(*food): print(food)yue(" ...

  3. git的简单使用(windows)

    使用参考文档 git简易指南:http://www.bootcss.com/p/git-guide/ git官方文档:https://git-scm.com/book/zh/v1/%E8%B5%B7% ...

  4. CSS 媒体查询@media

    1. 概述 1.1 定义 @media可以针对不同的屏幕尺寸(媒体类型)设置不同的样式,在响应式页面中,@media非常有用.重置浏览器大小的过程中,页面也会根据浏览器的宽度和高度重新渲染页面. 1. ...

  5. 自定义Dialog的详细步骤(实现自定义样式一般原理)

    现在很多App的提示对话框都非常有个性,然而你还用系统的对话框样式,是不是觉得很落后呢,今天我就给大家讲讲怎样自定义自己的Dialog,学会了之后,你就会根据自家app的主题,设计出相应的Dialog ...

  6. 鼠标hover图片时遮罩层匀速上升显示内容top、定位

    1.html <div class="div1">   <div class="div11">   <p >Dolor nu ...

  7. Confluence 6 基本性能问题诊断步骤

    基本性能问题诊断步骤 开始下面的程序: 进入 Troubleshooting Confluence hanging or crashing页面找到已知的主要性能问题. 进行页面 Performance ...

  8. Confluence 6 跟踪你安装中的自定义修改

    在 Confluence 中的系统信息(System Information)部分,有一个 修改(Modification)的选项.在这个选项中列出了自你 Confluence 安装以来,你 Conf ...

  9. Confluence 6 管理插件和组件

    一个 组件(add-on)是与 Confluence 分开安装的功能,能够加强 Confluence 的功能和使用.插件(plugin)和 组件(add-on)这 2 个词通常是一起使用的. 一共有 ...

  10. npm安装依赖包 --save-dev 和 --save; package.json的devDependencies和dependencies 的区别!

    以前一直在纠结一个npm安装的包依赖管理的问题.是这样的: 我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,他们是:--save- ...