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$的所有点中权值最小的 ...
随机推荐
- ASP.NET Web Pages:全局页面
ylbtech-.Net-ASP.NET Web Pages:全局页面 1.返回顶部 1. ASP.NET Web Pages - 全局页面 本章介绍全局页面 AppStart 和 PageStart ...
- [转]winform CEF
下载地址: http://opensource.spotify.com/cefbuilds/index.html 来自: NanUI 作者
- 关于json_encode()的使用注意
json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...
- unity3d之Editor的Assembly-CSharp.dll文件路径
在Editor中与自己project中使用的Mono与Managed文件夹路径区别: Editor中:在unity安装路径[AppDir]下: 自己project中:在project的路径下,由bui ...
- ETL项目场景
1.基础数据的维护,基本都是人工实现 2.慢慢基于文件进行导入 3.专业的数据交换平台 ================================= Kettle:数据导入不是采取数据库模式,因 ...
- VB ListView罗列图片
一开始听到job的要求,还真不知道如何下手,用Gridview?好像有做不到把图片show出来的.在网上搜索一遍,发现原来是ListView与Imglist的结合应用. 看设计: Private ...
- [Flutter] 一些面试可能会问基础知识
1. Flutter 是什么? Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面. Flutter可以与现有的代码一起工作.在全世界,Flutter正在被 ...
- thinkphp5隐藏apache下的index.php
在应用入口文件同级目录添加.htaccess文件,内容如下: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews Re ...
- FlexPaper及二次开发
Flexpaper二次开发入门教程 http://ajava.org/course/web/?page=2
- aix系统使用随笔
在 Aix操作系统 中,常用的文档编辑命令是 vi.下面,我们就来学习一下有关vi的使用决窍. 在vi中,必须牢记它是有两个状态的 ---- 输入状态与命令状态.由输入状态切换 到命令状态,必须ESC ...