CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移
题意:
有m个区间,n个a[ i ] , 选择若干个区间,使得整个数组中的最大值和最小值的差值最小。n<=1e5,m<=300;
思路:
可以知道每个i,如果一个区间包含这个点,就让这个区间发挥作用。枚举每个i,找到最大值即可。
当然这个复杂度不对,我们可以通过线段树保存数组的最大值和最小值,每次区间在左端点发挥作用,在右端点去掉作用。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef pair<ll,int>pli;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'
//#define R register
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
//const int mod = 1e9+7;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime---------------------*/ const int maxn = 1e5+;
int mx[maxn<<],mn[maxn<<],dif[maxn<<];
int lazy[maxn<<]; int a[maxn];
pii e[maxn];
void pushup(int rt){
mx[rt] = max(mx[rt<<], mx[rt<<|]);
mn[rt] = min(mn[rt<<], mn[rt<<|]);
dif[rt] = mx[rt] - mn[rt];
}
void build(int l,int r,int rt){
if(l == r){
mx[rt] = mn[rt] = a[l];
dif[rt] = mx[rt] - mn[rt];
return;
} int mid = (l + r) >> ;
build(l, mid, rt<<);
build(mid+, r, rt<<|);
pushup(rt);
}
vector<int>tmp,res;
void pushdown(int rt){
if(lazy[rt] == ) return;
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
mn[rt<<] += lazy[rt];
mn[rt<<|] += lazy[rt];
mx[rt<<] += lazy[rt];
mx[rt<<|] += lazy[rt];
lazy[rt] = ;
return;
}
void update(int L, int R ,int c,int l,int r,int rt){
if(l>=L && r<=R){
mx[rt] += c;
mn[rt] += c;
lazy[rt] += c;
return;
}
pushdown(rt);
int mid = (l + r) >> ;
if(mid >= L)update(L,R,c,l,mid,rt<<);
if(mid < R)update(L,R,c,mid+,r,rt<<|);
pushup(rt);
} int main(){
int n,m;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
build(,n,); for(int i=; i<=m; i++) scanf("%d%d", &e[i].fi, &e[i].se); int ans = ; for(int i=; i<=n; i++){ tmp.clear(); for(int j=; j<=m; j++){
if(e[j].fi == i){
update(e[j].fi,e[j].se,-,,n,);
}
else if(e[j].se == i-){
update(e[j].fi,e[j].se,,,n,);
}
if(e[j].fi <=i && e[j].se >= i)
tmp.pb(j);
}
int q = dif[];
if(ans < q){
ans = q;
// res.clear();
res = tmp; } } printf("%d\n", ans);
printf("%d\n", (int)res.size()); for(int i=; i<(int)res.size(); i++){
printf("%d ", res[i]);
}
puts("");
return ;
}
CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移的更多相关文章
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- CF E2 - Array and Segments (Hard version) (线段树)
题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...
- E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
题目链接: E1:http://codeforces.com/contest/1108/problem/E1 E2:http://codeforces.com/contest/1108/problem ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- POJ 1436 Horizontally Visible Segments (线段树·区间染色)
题意 在坐标系中有n条平行于y轴的线段 当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交 就视为它们是可见的 问有多少组三条线段两两相互可见 先把全部线段存下来 并按x ...
- 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 题意 ...
- Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)
The only difference between easy and hard versions is a number of elements in the array. You are giv ...
随机推荐
- Android开发——通过wifi接收IPCamera视频流
前面,我们已经了解了怎么在android app上打开关闭和扫描,搜索wifi,现在,我来写一下怎么通过连接wifi来使app获取到IPCamera摄像头的视频. 一.通过URL获取视频的地址 二.创 ...
- Vue项目的创建和UI资源
Vue项目创建打包与UI资源 1.Vue项目创建 1.1 vue-cli脚手架 vue-cli是一个基于vue的构建工具,用于搭建vue项目的环境,有着兼容,方便,快速的优点,能够完全遵循前后端分离的 ...
- Selenium+java - 下拉框处理
常见下拉框也分两种:一种是标准控件和非标准控件(一般为前端开发人员自己封装的下拉框),本篇文章中将重点讲解标准下拉框操作. 1.Select提供了三种选择某一项的方法 select.selectByI ...
- codeforces 347A - Difference Row
给你一个序列,让你求(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).值最大的一个序列,我们化简一下公式就会发现(x1 - x2) + (x2 - x3) + . ...
- CountDownLatch实现多线程并发请求
package com.test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Dat ...
- 在线图片base64编码
图片Base64编码https://oktools.net/image2base64 在线工具https://oktools.net JSON格式化https://oktools.net/json U ...
- git开发流程
典型的工作流程和做法是,由于你没有远程仓库的权限,你先在github通过fork,复制自己的一份远程仓库,然后通过clone你自己这个远程副本到本地,进行修改,修改后push到自己的githu远程副本 ...
- 洛谷P2125 题解
吐槽: 只能说这道题很数学,本数学蒟蒻推了半天没推出来,只知道要用绝对值,幸亏教练提醒,才勉强想出正解(似乎不是这样的),真的是很无语. 以上皆为吐槽本题,可直接 跳过 分析: 既然题目是要使书架上的 ...
- 【游记】NOIP2019前传
声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 比完赛后,我沉浸在胜利中长达半个月,而后才清醒过来,意识到自己需要为NOIP2019 ...
- Xlistview_聚合菜谱大全数据
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private ...