题意:给你一个长度为n的序列和m组区间操作,每组区间操作可以把区间[l, r]中的数字都-1,请选择一些操作(可以都不选),使得序列的最大值和最小值的差值尽量的大。

思路:容易发现如果最大值和最小值都在某个操作区间里,那么这个操作没有意义,因为差值没变,所以我们可以想到暴力枚举每一个位置,假设这个位置的数是最小的,那么就把所有与他相关的区间操作都执行,执行完后找到当前序列的最大值更新答案即可。

E1数据很小,直接3重循环暴力枚举就可以过了,复杂度为O(n * n * m)。

对于E2,很明显如果每次操作完了从头到尾循环一遍找最大值很费时间,看标题也能想到最大值要用线段树来维护(2333),但是就算用线段树找最大值,复杂度还是O(n * m * logn)。

我们可以直观感受一下,对于每个枚举的位置,每次都暴力的把可以的区间操作加上,再暴力的还原这些操作,非常的浪费,所有我们可以从这里优化。

假设一个操作区间为[l, r],那么实际这个区间操作可以使[l, r]区间之中的值变得更小,在这个范围之外,这个操作是多余的。所以我们可以用差分的思想,我们记录一下这个区间对答案影响的开始位置和结束位置,扫描到区间开始时在线段树中加上该区间的影响,到区间末尾时消去该区间的影响。

因为每个操作只会添加和消去一次, 总复杂度为O(n * logn + m * logn), 其中的n * logn 是线段树的建树时间。

代码:

#include <bits/stdc++.h>
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define pii pair<int, int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 100010;
int a[maxn]; struct node{
int mx, add;
int l, r;
}; vector<int> st[maxn], ed[maxn]; node tr[maxn * 4];
pii b[310];
vector<int> res;
void pushdown(int o) {
if(tr[o].add != 0) {
tr[ls(o)].add += tr[o].add;
tr[rs(o)].add += tr[o].add;
tr[ls(o)].mx += tr[o].add;
tr[rs(o)].mx += tr[o].add;
tr[o].add = 0;
}
} void maintain(int o) {
tr[o].mx = max(tr[ls(o)].mx,tr[rs(o)].mx);
}
void build(int o, int l, int r) {
if(l == r) {
tr[o].mx = a[l];
tr[o].add = 0;
tr[o].l = l;
tr[o].r = r;
return;
}
tr[o].l = l;
tr[o].r = r;
int mid = (l + r) >> 1;
build(ls(o), l, mid);
build(rs(o), mid + 1, r);
maintain(o);
} void update(int o, int l, int r, int ql, int qr, int add) {
if(l >= ql && r <= qr) {
tr[o].mx += add;
tr[o].add += add;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(mid >= ql) update(ls(o), l, mid, ql, qr, add);
if(mid < qr) update(rs(o), mid + 1, r, ql, qr, add);
maintain(o);
} int query(int o, int l, int r, int ql ,int qr) {
if(l >= ql && r <= qr) {
return tr[o].mx;
}
int ans = -INF;
pushdown(o);
int mid = (l + r) >> 1;
if(ql <= mid) ans = max(ans, query(ls(o), l, mid, ql, qr));
if(qr > mid) ans = max(ans, query(rs(o), mid + 1, r, ql, qr));
return ans;
} int main() {
int n, m, ans = 0;
int mx = - INF, mi = INF;
// freopen("in.txt","r",stdin);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
mx = max(mx, a[i]);
mi = min(mi, a[i]);
}
ans = mx - mi;
build(1, 1, n);
for(int i = 1; i <= m; i++) {
scanf("%d%d",&b[i].first, &b[i].second);
st[b[i].first].push_back(b[i].second);
ed[b[i].second].push_back(b[i].first);
}
int pos = 0, tmp;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < st[i].size(); j++) {
update(1, 1, n, i, st[i][j], -1);
}
tmp = query(1, 1, n, 1, n) - query(1, 1, n, i, i);
if(tmp > ans) {
ans = tmp;
pos = i;
}
for(int j = 0; j < ed[i].size(); j++) {
update(1, 1, n, ed[i][j], i, 1);
}
}
printf("%d\n", ans);
for(int i = 1; i <= m; i++) {
if(b[i].first <= pos && b[i].second >= pos)
res.push_back(i);
}
printf("%d\n",res.size());
for(int i = 0; i < res.size(); i++)
printf("%d ",res[i]);
}

  

Codeforces 1108E (Array and Segments) 线段树的更多相关文章

  1. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

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

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  6. Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)

    看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...

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

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

  8. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  9. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

随机推荐

  1. Swift中如何使用 #if DEBUG

    Swift暂时还不支持大多数的预处理宏操作,但是可以支持“#if/#else/#endif”语句. 下面进行简单的设置使 #if DEBUG 有效,更详细的内容见:http://stackoverfl ...

  2. 卸载全部appx应用(包括应用商店)

    在PowerShell中粘贴: Get-AppXPackage | Remove-AppxPackage

  3. Asp.net 使用 Jsonp

    简介 由于JavaScript的安全机制,ajax不支持跨域调用.所以出现了jsonp. 实现 服务器 public string Jsonp(string name) { string result ...

  4. web.xml & web-fragment.xml (Servlet 2.3, 2.4, 2.5 + 3.0)模板

    转自:http://jlcon.iteye.com/blog/890964 web.xml v2.3 <?xml version="1.0" encoding="I ...

  5. Dubbo与Zookeeper

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  6. oracle+110个常用函数

    1.ASCII  返回与指定的字符对应的十进制数;  SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from du ...

  7. java bean Format注解用法

    @NumberFormat(style=Style.NUMBER)    private int number; @DateTimeFormat(pattern="yyyy-MM-dd&qu ...

  8. Java基础--CountDownLatch

    CountDownLatch是线程同步辅助类,它允许一个或多个线程wait直到countdown被调用使count为0. CountDownLatch是在java1.5被引入,存在于java.util ...

  9. bootstrap简单的签收页面

    http://aqvatarius.com/themes/atlant/html/ui-icons.html <%@ Page Language="C#" AutoEvent ...

  10. 32位模式下C/C++程序可用最大内存

    关于32位程序申请大内存问题(1.6G). 我在win7 64系统上面测试Visual studio 10 int* Test=new int[1024*1024*200]; int* Test2=n ...