Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2
2 seconds
256 megabytes
standard input
standard output
The only difference between easy and hard versions is a number of elements in the array.
You are given an array aa consisting of nn integers. The value of the ii-th element of the array is aiai.
You are also given a set of mm segments. The jj-th segment is [lj;rj][lj;rj], where 1≤lj≤rj≤n1≤lj≤rj≤n.
You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array a=[0,0,0,0,0]a=[0,0,0,0,0] and the given segments are [1;3][1;3] and [2;4][2;4] then you can choose both of them and the array will become b=[−1,−2,−2,−1,0]b=[−1,−2,−2,−1,0].
You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array aa and obtain the array bb then the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbiwill be maximum possible.
Note that you can choose the empty set.
If there are multiple answers, you can print any.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
The first line of the input contains two integers nn and mm (1≤n≤105,0≤m≤3001≤n≤105,0≤m≤300) — the length of the array aaand the number of segments, respectively.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106), where aiai is the value of the ii-th element of the array aa.
The next mm lines are contain two integers each. The jj-th of them contains two integers ljlj and rjrj (1≤lj≤rj≤n1≤lj≤rj≤n), where ljlj and rjrj are the ends of the jj-th segment.
In the first line of the output print one integer dd — the maximum possible value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi if bb is the array obtained by applying some subset of the given segments to the array aa.
In the second line of the output print one integer qq (0≤q≤m0≤q≤m) — the number of segments you apply.
In the third line print qq distinct integers c1,c2,…,cqc1,c2,…,cq in any order (1≤ck≤m1≤ck≤m) — indices of segments you apply to the array aa in such a way that the value maxi=1nbi−mini=1nbimaxi=1nbi−mini=1nbi of the obtained array bb is maximum possible.
If there are multiple answers, you can print any.
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
6
2
4 1
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
7
2
3 2
1 0
1000000
0
0
In the first example the obtained array bb will be [0,−4,1,1,2][0,−4,1,1,2] so the answer is 66.
In the second example the obtained array bb will be [2,−3,1,−1,4][2,−3,1,−1,4] so the answer is 77.
In the third example you cannot do anything so the answer is 00.
题意概括:
给出一段长度为 N 的初始序列,以及 M 个区间,要求选择若干区间(选择该区间则对该区间的数进行 -1 操作),使得序列中的最大值和最小值的差值最大。
解题思路:
选择区间的三种情况:
(1)该区间只包含最后的最小值,则选择该区间会优化最大差值,必选。
(2)该区间包含最后的最小值和最大值,对差值无影响,选不选无所谓。
(3)该区间未包含最后的最小值,有可能会减小最大差值,不可选。
但是我们并不知道最后的最小值是哪个?所以需要枚举一遍,假设每个值都有可能成为最后的最小值,最后取最优的解。
既然假设了当前的这位数为会成为最后的最小值,那么根据上面三种区间的选取情况,我们要选择相对应的区间,这样才能达到目前这一位作为最小值的最优解。
如果每到一位都暴力一遍所有区间,这样复杂度太大了,我们可以用一个 Left [ k ] 动态链表 和一个 Right [ k ] 动态链表,分别记录以 k 位开始的区间有哪些和以 k 位结束的区间有哪些?
这样更新的时候只需要操作这些与当前位相关的区间即可。
这里的区间更新有两种方案:
(1)直接暴力更新
AC code:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = 1e5+;
const int MAXM = ;
vector<int>Le[MAXN];
vector<int>Ri[MAXN];
int num[MAXN];
int ans_q[MAXM];
int N, M;
struct data
{
int l, r;
}q[MAXM]; void update(int L, int R, int v)
{
for(int i = L; i <= R; i++){
num[i]+=v;
}
} int main()
{
int index;
int maxx = -INF, minn = INF;
scanf("%d %d", &N, &M);
for(int i = ; i <= N; i++){
scanf("%d", &num[i]);
maxx = max(maxx, num[i]);
if(minn > num[i]){minn = num[i]; index = i;}
} for(int i = ; i <= M; i++){
scanf("%d %d", &q[i].l, &q[i].r);
Le[q[i].l].push_back(i);
Ri[q[i].r+].push_back(i);
} int ans = maxx-minn;
int tp;
for(int i = ; i <= N; i++){
for(int ed = ; ed < Ri[i].size(); ed++){
tp = Ri[i][ed];
update(q[tp].l, q[tp].r, );
} for(int st = ; st < Le[i].size(); st++){
tp = Le[i][st];
update(q[tp].l, q[tp].r, -);
} if(!Le[i].empty() || !Ri[i].empty()){
maxx = -INF;minn = INF;
for(int i = ; i <= N; i++){
maxx = max(maxx, num[i]);
minn = min(minn, num[i]);
}
if(ans < maxx-minn){
ans = maxx-minn;
index = i;
}
}
}
if(ans <= -INF){
puts("");
puts("");
}
else{
printf("%d\n", ans);
int cnt = ;
for(int i = ; i <= M; i++){
if(q[i].l <= index && q[i].r >= index){
ans_q[++cnt] = i;
}
}
printf("%d\n", cnt);
for(int i = ; i <= cnt; i++){
printf("%d ", ans_q[i]);
}
puts("");
}
return ;
}
(2)可持续化线段树的区间更新(即 lazy tag)
AC code:
////可持久化线段树优化的区间更新
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
const int MAXM = ; int N, M;
int a[MAXN], L[MAXM], R[MAXM], ans[MAXN];
vector<int>add[MAXN], sub[MAXN]; struct node
{
int l, r;
int maxx, minn, lazy;
}tree[MAXN<<]; void push_up(int k)
{
tree[k].maxx = max(tree[k<<].maxx, tree[k<<|].maxx);
tree[k].minn = min(tree[k<<].minn, tree[k<<|].minn);
} void push_down(int k)
{
if(tree[k].l != tree[k].r){
tree[k<<].maxx += tree[k].lazy; tree[k<<].minn += tree[k].lazy;
tree[k<<|].maxx+=tree[k].lazy; tree[k<<|].minn+=tree[k].lazy;
tree[k<<].lazy += tree[k].lazy;
tree[k<<|].lazy += tree[k].lazy;
}
tree[k].lazy = ;
} void build(int k, int l, int r)
{
tree[k].l = l;
tree[k].r = r;
if(l == r){
tree[k].maxx = a[l];
tree[k].minn = a[l];
tree[k].lazy = ;
return;
}
int mid = (l+r)>>;
build(k<<, l, mid);
build(k<<|, mid+, r);
push_up(k);
} void update(int k, int l, int r, int x)
{
if(tree[k].lazy) push_down(k);
tree[k].maxx+=x;
tree[k].minn+=x;
if(tree[k].l == l && tree[k].r == r){
tree[k].lazy += x;
return;
}
int mid = (tree[k].l + tree[k].r)>>;
if(r <= mid){
update(k<<, l, r, x);
}
else if(l > mid){
update(k<<|, l, r, x);
}
else{
update(k<<, l, mid, x);
update(k<<|, mid+, r, x);
}
push_up(k);
} int query_max(int k, int l, int r)
{
int maxx;
if(tree[k].lazy) push_down(k);
if(tree[k].l == l && tree[k].r == r) return tree[k].maxx;
int mid = (tree[k].l + tree[k].r)>>;
if(r <= mid) maxx = query_max(k<<, l, r);
else if(l > mid) maxx = query_max(k<<|, l, r);
else{
maxx = max(query_max(k<<, l, mid), query_max(k<<|, mid+, r));
}
return maxx;
} int query_min(int k, int l, int r)
{
int minn;
if(tree[k].lazy) push_down(k);
if(tree[k].l == l && tree[k].r == r) return tree[k].minn;
int mid = (tree[k].l + tree[k].r)>>;
if(r <= mid) minn = query_min(k<<, l, r);
else if(l > mid) minn = query_min(k<<|, l, r);
else
minn = min(query_min(k<<, l, mid), query_min(k<<|, mid+, r));
return minn;
} int main()
{
scanf("%d %d", &N, &M);
for(int i = ; i <= N; i++) scanf("%d", &a[i]); for(int i = ; i <= M; i++){
scanf("%d%d", &L[i], &R[i]); sub[ L[i] ].push_back(i);
add[ R[i] ].push_back(i);
} build(, , N); int d = -, p;
for(int i = ; i <= N; i++){ for(int j = ; j < add[i-].size(); j++){
int id = add[i-][j];
update(, L[id], R[id], );
} for(int j = ; j < sub[i].size(); j++){
int id = sub[i][j];
update(, L[id], R[id], -);
} int det = query_max(, , N) - query_min(, , N);
//printf("det:%d\n", det);
if(det > d){
d = det;
p = i;
}
} printf("%d\n", d);
int t = ;
for(int i = ; i <= M; i++){
if(L[i] <= p && p <= R[i])
ans[t++] = i;
} printf("%d\n", t);
for(int i = ; i < t; i++){
printf("%d ", ans[i]);
}
puts(""); return ; }
Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】的更多相关文章
- CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移
传送门 题意: 有m个区间,n个a[ i ] , 选择若干个区间,使得整个数组中的最大值和最小值的差值最小.n<=1e5,m<=300; 思路: 可以知道每个i,如果一个区间包含这个 ...
- CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)
参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- Codeforces Round #535 (Div. 3) 解题报告
CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...
- Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)
https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...
- Codeforces Round #535(div 3) 简要题解
Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
随机推荐
- 点击checkbox全选,其它被选中,再点击取消
<input type="checkbox" value="" id="checkall" name="" siz ...
- 使用powershell 执行脚本,windows默认不允许任何脚本运行
使用如下命令让PowerShell运行在无限制的环境之下: Set-ExecutionPolicy Unrestricted
- css3打包后自动追加前缀插件:autoprefixer
用vue-cli构建的项目脚手架已经帮你把autoprefixer的配置做好了,自己不需要做什么改动就会自动加前缀: 下面一起看看涉及到autoprefixer这个插件的一些配置: 1,postcss ...
- javascript检查数据中是否存在相同的元素
这里是两个用于数组中查找重复元素的demo,可以看看啦 <!DOCTYPE html><html lang="en"><head> <me ...
- Bzoj1496: [NOI2006]千年虫
题面 传送门 Sol 左右可以分开搞 然后就是要形成一个类似梳子的东西 设\(f[0/1][i][j]\) \(0\)凹,\(1\)凸,\(i\)为行,可以滚一维,\(j\)为该行长度 \(f[0][ ...
- iview框架modal中嵌套modal
modal的使用是平级的,后面的会覆盖前面,如下<modal>111</modal><modal>222</modal>内容为222的弹框会在内容为11 ...
- 28_Future模式1
[Future模式] Future模式类似商品订单.比如在网购时,当看中一件商品时,就可以提交订单,当订单处理完成后,在家里等待商品送货上门即可.或者类似我们发送Ajax请求的时候,页面是异步的进行后 ...
- C中atoi和strcpy的自定义实现
这是两道经常考到的笔试题,看似简单的实现,其实专注到细节,还是有很多需要注意扣分的地方. atoi实现: #include <iostream> #include<ctype.h&g ...
- Android菜单(动画菜单、360波纹菜单)
Android菜单(动画菜单.360波纹菜单) 前言:Android菜单常用集合:FragmentTabHost系统菜单.上移式菜单.360波纹菜单.展开式菜单.详解注释,可直接拿来用! 效果: ...
- 解决 sourcetree git ssh私钥问题
最近遇到个问题,git仓库用gitbash,更新和提交都是没问题的,用界面化工具SourceTree就一直提示下面信息: git -c diff.mnemonicprefix=false -c cor ...