forked from shayanh/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentlazy.cpp
70 lines (57 loc) · 1.82 KB
/
segmentlazy.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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define VAL(x) #x << " = " << x << " "
#define SQR(a) ((a) * (a))
#define SZ(x) ((int) x.size())
#define ALL(x) x.begin(), x.end()
#define CLR(x, a) memset(x, a, sizeof x)
#define FOREACH(i, x) for(__typeof((x).begin()) i = (x).begin(); i != (x).end(); i ++)
#define FOR(i, n) for (int i = 0; i < (n); i ++)
#define X first
#define Y second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 300 * 1000 + 10;
int min_val[4 * MAXN], rgt_min[4 * MAXN], add[4 * MAXN];
inline void shift(int x) {
int lc = x + x + 0, rc = x + x + 1;
if (add[x]) {
min_val[lc] += add[x];
min_val[rc] += add[x];
add[lc] += add[x];
add[rc] += add[x];
}
add[x] = 0;
}
void update2(int lo, int hi, int s, int e, int x, int delta) {
if (lo == s && hi == e) {
min_val[x] += delta;
add[x] += delta;
return ;
}
shift(x);
int mid = (s + e) / 2;
if (lo < mid) update2(lo, min(mid, hi), s, mid, x + x + 0, delta);
if (hi > mid) update2(max(lo, mid), hi, mid, e, x + x + 1, delta);
int lc = x + x + 0, rc = x + x + 1;
min_val[x] = min(min_val[lc], min_val[rc]);
if (min_val[rc] <= min_val[lc]) rgt_min[x] = rgt_min[rc] + mid - s;
else rgt_min[x] = rgt_min[lc];
}
pii get2(int lo, int hi, int s, int e, int x) {
if (lo == s && hi == e) return mp(min_val[x], rgt_min[x]);
shift(x);
int mid = (s + e) / 2;
pii tmp1 = mp(INF, -1), tmp2 = mp(INF, -1);
if (lo < mid) tmp1 = get2(lo, min(mid, hi), s, mid, x + x + 0);
if (hi > mid) tmp2 = get2(max(lo, mid), hi, mid, e, x + x + 1);
if (tmp2.X <= tmp1.X) return mp(tmp2.X, tmp2.Y + (lo < mid ? mid - lo : 0));
else return mp(tmp1.X, tmp1.Y);
}
int main () {
ios::sync_with_stdio(false);
return 0;
}