-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path174C. Range Increments.cpp
91 lines (71 loc) · 1.99 KB
/
174C. Range Increments.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Idea:
- Segment tree and BFS.
- Precalculate the indexes of each number in array `a`.
- Start with range `(1, n)`.
- Find the minimum number in it (Segment tree).
- Split the current segment to smaller segments and push them
to the queue (BFS) based on the positions of the minimum
number in the current segment.
- Repeat the procedure for the other segments until the queue
is empty.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, s, e, val, a[N], seg[N * 4];
vector<pair<int, int> > sol;
queue<pair<pair<int, int>, int> > q;
vector<vector<int> > pos;
void build(int at, int l, int r) {
if(l == r) {
seg[at] = a[l];
return;
}
int mid = (l + r) >> 1;
build(at << 1, l, mid);
build(at << 1 | 1, mid + 1, r);
seg[at] = min(seg[at << 1], seg[at << 1 | 1]);
}
int get(int at, int l, int r) {
if(l > e || r < s)
return 1e9;
if(l >= s && r <= e)
return seg[at];
int mid = (l + r) >> 1;
return min(get(at << 1, l, mid), get(at << 1 | 1, mid + 1, r));
}
int main() {
pos.resize(N);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
pos[a[i]].push_back(i);
}
build(1, 1, n);
q.push({{1, n}, 0});
while(!q.empty()) {
int l = q.front().first.first, r = q.front().first.second;
int cur = q.front().second;
q.pop();
if(!(l <= r))
continue;
s = l, e = r;
int mn = get(1, 1, n);
for(int i = 0; i < mn - cur; ++i)
sol.push_back({l, r});
int idx = lower_bound(pos[mn].begin(), pos[mn].end(), l) - pos[mn].begin();
q.push({{l, pos[mn][idx] - 1}, (mn - cur) + cur});
++idx;
while(idx < pos[mn].size() && pos[mn][idx] <= r) {
q.push({{pos[mn][idx - 1] + 1, pos[mn][idx] - 1}, (mn - cur) + cur});
++idx;
}
--idx;
q.push({{pos[mn][idx] + 1, r}, (mn - cur) + cur});
}
printf("%d\n", int(sol.size()));
for(int i = 0; i < sol.size(); ++i)
printf("%d %d\n", sol[i].first, sol[i].second);
return 0;
}