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 per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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 1ljrjn1≤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=1nbimini=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.

Input

The first line of the input contains two integers nn and mm (1n105,0m3001≤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 (106ai106−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 (1ljrjn1≤lj≤rj≤n), where ljlj and rjrj are the ends of the jj-th segment.

Output

In the first line of the output print one integer dd — the maximum possible value maxi=1nbimini=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 (0qm0≤q≤m) — the number of segments you apply.

In the third line print qq distinct integers c1,c2,,cqc1,c2,…,cq in any order (1ckm1≤ck≤m) — indices of segments you apply to the array aa in such a way that the value maxi=1nbimini=1nbimaxi=1nbi−mini=1nbi of the obtained array bb is maximum possible.

If there are multiple answers, you can print any.

Examples
input
Copy
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
output
Copy
6
2
4 1 
input
Copy
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
output
Copy
7
2
3 2 
input
Copy
1 0
1000000
output
Copy
0
0

Note

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:

  

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<vector>
 6 #include<queue>
 7 #include<cmath>
 8 #include<set>
 9 #define INF 0x3f3f3f3f
10 #define LL long long
11 using namespace std;
12 const LL MOD = 1e9+7;
13 const int MAXN = 1e5+10;
14 const int MAXM = 333;
15 vector<int>Le[MAXN];
16 vector<int>Ri[MAXN];
17 int num[MAXN];
18 int ans_q[MAXM];
19 int N, M;
20 struct data
21 {
22     int l, r;
23 }q[MAXM];
24 
25 void update(int L, int R, int v)
26 {
27     for(int i = L; i <= R; i++){
28         num[i]+=v;
29     }
30 }
31 
32 int main()
33 {
34     int index;
35     int maxx = -INF, minn = INF;
36     scanf("%d %d", &N, &M);
37     for(int i = 1; i <= N; i++){
38         scanf("%d", &num[i]);
39         maxx = max(maxx, num[i]);
40         if(minn > num[i]){minn = num[i]; index = i;}
41     }
42 
43     for(int i = 1; i <= M; i++){
44         scanf("%d %d", &q[i].l, &q[i].r);
45         Le[q[i].l].push_back(i);
46         Ri[q[i].r+1].push_back(i);
47     }
48 
49     int ans = maxx-minn;
50     int tp;
51     for(int i = 1; i <= N; i++){
52         for(int ed = 0; ed < Ri[i].size(); ed++){
53             tp = Ri[i][ed];
54             update(q[tp].l, q[tp].r, 1);
55         }
56 
57         for(int st = 0; st < Le[i].size(); st++){
58             tp = Le[i][st];
59             update(q[tp].l, q[tp].r, -1);
60         }
61 
62         if(!Le[i].empty() || !Ri[i].empty()){
63             maxx = -INF;minn = INF;
64             for(int i = 1; i <= N; i++){
65                 maxx = max(maxx, num[i]);
66                 minn = min(minn, num[i]);
67             }
68             if(ans < maxx-minn){
69                 ans = maxx-minn;
70                 index = i;
71             }
72         }
73     }
74     if(ans <= -INF){
75         puts("0");
76         puts("0");
77     }
78     else{
79         printf("%d\n", ans);
80         int cnt = 0;
81         for(int i = 1; i <= M; i++){
82             if(q[i].l <= index && q[i].r >= index){
83                 ans_q[++cnt] = i;
84             }
85         }
86         printf("%d\n", cnt);
87         for(int i = 1; i <= cnt; i++){
88             printf("%d ", ans_q[i]);
89         }
90         puts("");
91     }
92     return 0;
93 }

(2)可持续化线段树的区间更新(即 lazy tag)

  AC code:

  

  1 ////可持久化线段树优化的区间更新
  2 #include <bits/stdc++.h>
  3 #define INF 0x3f3f3f3f
  4 #define LL long long
  5 using namespace std;
  6 const int MAXN = 1e5+10;
  7 const int MAXM = 303;
  8 
  9 int N, M;
 10 int a[MAXN], L[MAXM], R[MAXM], ans[MAXN];
 11 vector<int>add[MAXN], sub[MAXN];
 12 
 13 struct node
 14 {
 15     int l, r;
 16     int maxx, minn, lazy;
 17 }tree[MAXN<<2];
 18 
 19 void push_up(int k)
 20 {
 21     tree[k].maxx = max(tree[k<<1].maxx, tree[k<<1|1].maxx);
 22     tree[k].minn = min(tree[k<<1].minn, tree[k<<1|1].minn);
 23 }
 24 
 25 void push_down(int k)
 26 {
 27     if(tree[k].l != tree[k].r){
 28         tree[k<<1].maxx += tree[k].lazy; tree[k<<1].minn += tree[k].lazy;
 29         tree[k<<1|1].maxx+=tree[k].lazy; tree[k<<1|1].minn+=tree[k].lazy;
 30         tree[k<<1].lazy += tree[k].lazy;
 31         tree[k<<1|1].lazy += tree[k].lazy;
 32     }
 33     tree[k].lazy = 0;
 34 }
 35 
 36 
 37 void build(int k, int l, int r)
 38 {
 39     tree[k].l = l;
 40     tree[k].r = r;
 41     if(l == r){
 42         tree[k].maxx = a[l];
 43         tree[k].minn = a[l];
 44         tree[k].lazy = 0;
 45         return;
 46     }
 47     int mid = (l+r)>>1;
 48     build(k<<1, l, mid);
 49     build(k<<1|1, mid+1, r);
 50     push_up(k);
 51 }
 52 
 53 void update(int k, int l, int r, int x)
 54 {
 55     if(tree[k].lazy) push_down(k);
 56     tree[k].maxx+=x;
 57     tree[k].minn+=x;
 58     if(tree[k].l == l && tree[k].r == r){
 59         tree[k].lazy += x;
 60         return;
 61     }
 62     int mid = (tree[k].l + tree[k].r)>>1;
 63     if(r <= mid){
 64         update(k<<1, l, r, x);
 65     }
 66     else if(l > mid){
 67         update(k<<1|1, l, r, x);
 68     }
 69     else{
 70         update(k<<1, l, mid, x);
 71         update(k<<1|1, mid+1, r, x);
 72     }
 73     push_up(k);
 74 }
 75 
 76 
 77 int query_max(int k, int l, int r)
 78 {
 79     int maxx;
 80     if(tree[k].lazy) push_down(k);
 81     if(tree[k].l == l && tree[k].r == r) return tree[k].maxx;
 82     int mid = (tree[k].l + tree[k].r)>>1;
 83     if(r <= mid) maxx = query_max(k<<1, l, r);
 84     else if(l > mid) maxx = query_max(k<<1|1, l, r);
 85     else{
 86         maxx = max(query_max(k<<1, l, mid), query_max(k<<1|1, mid+1, r));
 87     }
 88     return maxx;
 89 }
 90 
 91 
 92 int query_min(int k, int l, int r)
 93 {
 94     int minn;
 95     if(tree[k].lazy) push_down(k);
 96     if(tree[k].l == l && tree[k].r == r) return tree[k].minn;
 97     int mid = (tree[k].l + tree[k].r)>>1;
 98     if(r <= mid) minn = query_min(k<<1, l, r);
 99     else if(l > mid) minn = query_min(k<<1|1, l, r);
100     else
101         minn = min(query_min(k<<1, l, mid), query_min(k<<1|1, mid+1, r));
102     return minn;
103 }
104 
105 int main()
106 {
107     scanf("%d %d", &N, &M);
108     for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
109 
110     for(int i = 1; i <= M; i++){
111         scanf("%d%d", &L[i], &R[i]);
112 
113         sub[ L[i] ].push_back(i);
114         add[ R[i] ].push_back(i);
115     }
116 
117     build(1, 1, N);
118 
119     int d = -1, p;
120     for(int i = 1; i <= N; i++){
121 
122         for(int j = 0; j < add[i-1].size(); j++){
123             int id = add[i-1][j];
124             update(1, L[id], R[id], 1);
125         }
126 
127         for(int j = 0; j < sub[i].size(); j++){
128             int id = sub[i][j];
129             update(1, L[id], R[id], -1);
130         }
131 
132         int det = query_max(1, 1, N) - query_min(1, 1, N);
133         //printf("det:%d\n", det);
134         if(det > d){
135             d = det;
136             p = i;
137         }
138     }
139 
140     printf("%d\n", d);
141     int t = 0;
142     for(int i = 1; i <= M; i++){
143         if(L[i] <= p && p <= R[i])
144             ans[t++] = i;
145     }
146 
147     printf("%d\n", t);
148     for(int i = 0; i < t; i++){
149         printf("%d ", ans[i]);
150     }
151     puts("");
152 
153     return 0;
154 
155 }

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/10360144.html