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 ...
随机推荐
- vue加载Element ui地址省市区插件-- element-china-area-data
1.安装 npm install element-china-area-data -S 2.使用(引入) import { provinceAndCityData, regionData, provi ...
- 一:ActiveMQ知识整理
一:JMS概念 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消 ...
- Java 重写(Override)与重载(Overload)区别
2019-04-1217:31:19 (1)方法重载是一个类中定义了多个方法名相同,而他们的参数的数量不同或数量相同而类型和次序不同,则称为方法的重载(Overloading). (2)方法重写是在子 ...
- Mybatis插入、查询自定义的数据类型的方式
1.首先创建JavaBean对象 package com.zuo.Mybatis.bean; public class PhoneNumber { private String countryCode ...
- POJ1651 Multiplication Puzzle(相邻乘积之和最小,区间DP)
http://blog.csdn.net/libin56842/article/details/9747021 http://www.cnblogs.com/devil-91/archive/2012 ...
- jsp实现html页面静态化
一.实现原因 1.网站访问量过大,导致服务器压力加大以及数据库数据交换频繁.生成静态页面提供访问以缓解压力. 2.静态页面是动态页面的备份,若动态页面出现异常,静态页面可以暂时替代. 二.使用场合 ...
- 前端使用nginx上传文件时,进度获取不对
在使用iview时,上传文件获取进度时onUploadProgress返回数据不对. 原因是开启了nginx代理,本地上传时先传到本地nginx然后在传到服务器,导致获取进度不对 解决:在nginx的 ...
- 回溯法最优装载问题(java)
1.问题描述: 有一批共有 n 个集装箱要装上两艘载重量分别为 c1 和 c2 的轮船,其中集装箱 i 的重量为 w[i], 且重量之和小于(c1 + c2).装载问题要求确定是否存在一个合 ...
- How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
Objective The objective is to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux. This a ...
- Windows静态库和动态库的创建和使用
偶们在实际的编程开发中,经常会遇到运行时无法找到某个DLL文件或者链接时无法找到某个LIB文件.然后,我们就开始乱GOOGLE一下,然后将VS2005的设置改变一下,或许就Ok了,我们将别人开发的DL ...