Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)
题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i]。有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质。比如a[i]加上x之后,a[i + 1]<a[i] + k[i],那么a[i + 1]就变成a[i] + k[i],否则不变。同理,若a[i + 2]小于了现在的a[i + 1] + k[i + 1],那么a[i + 2]也变成a[i + 1] + k[i + 1],一直保持这个性质。第二章操作,询问数组a的区间[l, r]的区间和。
思路:容易发现,假设更改了x位置之后,恰好到位置y不需要更改元素,那么及其之后的位置肯定就不用更改了。所以,在查找这个位置的时候,我们可以二分查找。找到之后,对区间[x, y]进行操作。我们可以发现,区间[x, y]的数有规律。假设x位置的数是a[x],那么a[x + 1]是a[x] + k[x], a[x + 2]是a[x] + k[x + 1] + k[x + 2],以此类推。这个区间的和可以分为2部分:[y - x + 1]个a[x],和关于k的部分。可以发现,k[x]出现了(r - l + 1)次,k[x + 1]出现了(r - l)次,以此类推。那么怎么O(1)获得这个东西呢?我们先预处理k数组的前缀和(假设前缀和数组是b),那么我们再求b的前缀和(假设是数组c),那么c[i]就是出现了i次k[1],i - 1次k[2],以此类推。那么区间[x, y]中关于k的和就可以得出了:c[y] - c[x - 1] - [y - x + 1] * b[x]。前面c[y] - c[x - 1]可以O(1)算出,而后面的部分比较麻烦。怎么维护后面[y - x + 1] * b[x]这个部分呢?我们发现a[x]正好出现[y - x + 1]次,这样我们可以把a[x] - b[x]用一个懒标记维护,下放标记的时候区间的和为c[y] - c[x - 1] + (r - l + 1) * val (val是a[x] - b[x])。
代码:
#include <bits/stdc++.h>
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define LL long long
using namespace std;
const int maxn = 100010;
const LL INF = 1e18;
LL a[maxn], b[maxn], c[maxn];
int n, m, now;
struct SegementTree{
LL sum, tot;
int l, r;
};
SegementTree tr[maxn * 4]; LL cal(int l, int r) {
return (c[r] - c[l]) - (r - l) * (b[l]);
}
void pushup(int x) {
tr[x].sum = tr[ls(x)].sum + tr[rs(x)].sum;
} void maintain(int x,LL tot) {
if(tot == -INF) return;
int l = tr[x].l, r = tr[x].r;
tr[x].sum = (r - l + 1) * tot + (c[r] - c[l - 1]);
tr[x].tot = tot;
} void pushdown(int x) {
maintain(ls(x), tr[x].tot);
maintain(rs(x), tr[x].tot);
tr[x].tot = -INF;
} void build(int x, int l, int r) {
tr[x].l = l, tr[x].r = r;
tr[x].tot = -INF;
if(l == r) {
tr[x].sum = a[l];
return;
}
int mid = (l + r) >> 1;
build(ls(x), l, mid);
build(rs(x), mid + 1, r);
pushup(x);
} LL query(int x, int l, int r, int ql, int qr) {
if(l >= ql && r <= qr) {
return tr[x].sum;
}
int mid = (l + r) >> 1;
LL ans = 0;
pushdown(x);
if(ql <= mid) ans += query(ls(x), l, mid, ql, qr);
if(qr > mid) ans += query(rs(x), mid + 1, r, ql, qr);
return ans;
} void update(int x, int l, int r, int ql, int qr, LL val) {
if(l >= ql && r <= qr) {
maintain(x, val);
return;
}
int mid = (l + r) >> 1;
pushdown(x);
if(mid >= ql) update(ls(x), l, mid, ql, qr, val);
if(mid < qr) update(rs(x), mid + 1, r, ql, qr, val);
pushup(x);
} int main() {
int x, y;
char s[5];
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 2; i <= n; i++) {
scanf("%lld", &b[i]);
b[i] += b[i - 1];
}
for (int i = 2; i <= n; i++)
c[i] = c[i - 1] + b[i];
build(1, 1, n);
scanf("%d", &m);
while(m--) {
scanf("%s%d%d", s + 1, &x, &y);
if(s[1] == '+') {
int l = x, r = n;
LL tmp = query(1, 1, n, x, x);
while(l < r) {
int mid = (l + r + 1) >> 1;
LL tmp1 = query(1 , 1, n, mid, mid);
if(tmp + y + b[mid] - b[x] > tmp1)
l = mid;
else r = mid - 1;
}
now = x;
update(1, 1, n, x, l, tmp + y - b[x]);
} else {
printf("%lld\n", query(1, 1, n, x, y));
}
}
}
Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)的更多相关文章
- Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]
题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...
- Codeforces 1136E Nastya Hasn't Written a Legend 线段树
vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...
- codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)
题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...
- Codeforces 719E (线段树教做人系列) 线段树维护矩阵
题面简洁明了,一看就懂 做了这个题之后,才知道怎么用线段树维护递推式.递推式的递推过程可以看作两个矩阵相乘,假设矩阵A是初始值矩阵,矩阵B是变换矩阵,求第n项相当于把矩阵B乘了n - 1次. 那么我们 ...
- [Codeforces 464E] The Classic Problem(可持久化线段树)
[Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...
- 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 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- 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 ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
随机推荐
- 开发框架-APP:Hybird App
ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...
- Find substring with K distinct characters
Given a string and number K, find the substrings of size K with K distinct characters. If no, output ...
- CefSharp v62修改,支持.net4.0
吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎 ...
- class<T>和 class<?>类型 有什么区别
平时看java源代码的时候,如果碰到泛型的话,我想? T K V E这些是经常出现的,但是有时想不起来代表什么意思,今天整理下: ? 表示不确定的java类型. T 表示java类型. K V 分别代 ...
- IE浏览器中overflow:hidden无效,内层元素超出外层div的解决方法
原文地址:http://www.xin126.cn/show.asp?id=2624 在用css布局的时候,用IE浏览器(ie6.ie7.ie8)预览,有时候会出现内层元素(内部DIV.图片等)超出外 ...
- 用HTML5播放IPCamera视频
HTML5增加了vedio标签,能直接播放视频,但是播放的格式是有限的. 这里将IPCamera的视频转换OGG格式,再通过HTML5网页播放,播放还是很清晰的.IPCamera要支持RTSP等视频流 ...
- Linux下Nagios的安装与配置 及遇到的坑
原文http://www.jianshu.com/p/7bc822fa8278 不愿意看前5.6c部分可以直接跳到最后看命令. 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能 ...
- 开发组件:tmpfs
[Linux]tmpfs简介及增加方式 https://blog.csdn.net/nextaction/article/details/57076924
- kubernetes学习 做持久化存储
本节演示如何为 MySQL 数据库提供持久化存储,步骤: 1.创建 PV 和 PVC 2.部署 MySQL 3.向 MySQL 添加数据 4.模拟节点宕机故障,Kubernetes 将 MySQL 自 ...
- Web 跨域请求
在前端开发过程中,难免和服务端产生数据交互.一般情况我们的请求分为这么几种情况: 1. 只关注发送,不关注接收 2.不仅要发送,还要关注服务端返回的信息 a. 同域请求 ...