传送门

题意:

      有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. phpStudy 升级 MySQL 到 5.7.21

    1.备份原来的MySQL 我的路径是D:\phpStudy2018\PHPTutorial\MySQL\bin 修改文件名为MySQL-backup 2.下载新的MySQL 5.7.21 网址:htt ...

  2. win10下nodejs的安装及配置

    这里主要引用两篇文章,写的非常详细,也能解决你可能出现的问题 nodejs安装及配置 如何删除之前nodejs设置的 npm config set prefix .....

  3. javaweb入门-----request与response的作用

    request对象和request对象的原理 1.request和response对象request对象和request对象的原理时由服务器创建的,我们来使用它们 2.request对象是来获取请求消 ...

  4. UE4 坐标系 坐标轴旋转轴

    Pitch是围绕Y轴旋转,也叫做俯仰角. Yaw是围绕Z轴旋转,也叫偏航角. Roll是围绕X轴旋转,也叫翻滚角. UE4里,蓝图中的rotation的三个依次为roll,pitch,yaw.C++中 ...

  5. request获取url链接和参数

            //Returns the part of this request's URL from the protocol name up to the query string in th ...

  6. MySQL-5.7.21非图形化下载、安装、连接问题记录

    1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...

  7. 记一次paramiko远程连接遇到的坑

    背景:工作中遇到了一个问题,需要用到windows向windows连接(文件传发)以及,linux向windows连接(文件传发)的需求. 自然而然会考虑到用paramiko,然而paramiko我用 ...

  8. 记一次python时间格式转换遇到的坑

    需求:拿到指定格式的时间的前一天的时间,如果今天是月初,年初,自动转换,比如:输入时间是:2019-06-27 23:59:59输出时间是:2019-06-26 23:59:59 之前用datetim ...

  9. SpringMVC学习笔记之---深入使用

    SpringMVC深入使用 (一)基于XML配置的使用 (1)配置 1.SpringMVC基础配置 2.XML配置Controller,HandlerMapping组件映射 3.XML配置ViewRe ...

  10. JAVA基础知识(六)Java 静态多分派&动态单分派

    1.分派发生在编译期和运行期,编译期的分派为静态分派,运行期的为动态分派. 2.编译期是根据对象声明的类型来选择方法,运行期是根据对象实际类型来选择方法. 3.单分派和多分派取决于宗量, 方法调用者和 ...