传送门

题意:

      有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) 利用线段树进行区间转移的更多相关文章

  1. 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 ...

  2. CF E2 - Array and Segments (Hard version) (线段树)

    题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...

  3. 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 ...

  4. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  5. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  6. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

  7. POJ 1436 Horizontally Visible Segments (线段树&#183;区间染色)

    题意   在坐标系中有n条平行于y轴的线段  当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交  就视为它们是可见的  问有多少组三条线段两两相互可见 先把全部线段存下来  并按x ...

  8. 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 题意 ...

  9. 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 ...

随机推荐

  1. IIS网站服务器性能优化攻略

    Windows Server自带的互联网信息服务器(Internet Information Server,IIS)是架设网站服务器的常用工具,它是一个既简单而又麻烦的东西,新手都可以使用IIS架设一 ...

  2. Redis的HelloWorld

    1.安装完成的Redis: linux安装的应用默认会在:usr/local/bin. 1.redis-benchmark:性能测试工具,是redis提供的一个高并发程序,可以在自己本机运行,看看自己 ...

  3. 【Android Studio】类名字右下角红色的 J 【待解决】

    问题如下图所示: 正在寻找结解决方法--

  4. 【SVN】SVN Working copy is too old

    前天在使用 SVN 客户端 CornerStone 的时候遇到了这个问题,代码不能提交了…… 遇到这个问题的时候怎么办? 解决办法: 找到报错对应的文件夹,里面有个 .svn 的文件夹,去掉再 com ...

  5. 在Docker中部署Spring Boot项目

    想要获取更多文章可以访问我的博客 - 代码无止境. 微服务现在在互联网公司可谓非常流行了,之前找工作的的时候很多HR电话约面试的时候都会问对微服务是否有过接触.而微服务和Docker可以非常完美的结合 ...

  6. 使用Java 编写FTP中的长传文件和下载文件

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式,),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方法:通过JDK自带的API实现 ...

  7. spring实战学习笔记(一)spring装配bean

    最近在学习spring boot 发现对某些注解不是很深入的了解.看技术书给出的实例 会很疑惑为什么要用这个注解? 这个注解的作用?有其他相同作用的注解吗?这个注解的运行机制是什么?等等 spring ...

  8. python面试总结1(基础章节)

    python语言基础 语言特点 python是静态还是动态类型?是强类型还是弱类型 动态强类型语言 动态还是静态指的是编译期还是运作期确定类型 强类型指的是不会发生隐式类型转换 python作为后端语 ...

  9. Unity进阶之ET网络游戏开发框架 06-游客登录

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  10. linux+jenkins+python+svn 自动化测试集成之路

    本文背景: 背景1---个人基础: 本机win7上安装pycharm,使用python搭建API自动化测试框架,本地运行Pass.本机上搭建jenkins,创建测试任务,定时构建Pass. 背景2-- ...