More than 2019 cattle off summer school camp (second field) J Subarray

The meaning of problems: length 1E9 $ $ $ A $ interval subscript $ [0,1e9-1] $, $ n-number of input intervals $, $ [l_i, r_i] $ Class interval is 1, the balance -1, and asked how many intervals greater than 0.
solution: read the blog from the big brother, the point can produce only a maximum contribution of $ 3e7 $ months, which means first seeking a prefix and then painted map should look like.

Worst is the case, it can be influential only $ 3e7 $ points (possibly segmented) , then the question is, how to obtain this $ 3e7 $ points.
From the blog Gangster

Why? Very simple in the eyes of big brother, I drew a diagram to understand it.

Obviously a front of $ f [i] $ add back $ g [i + 1] $ than the length between the two large sections joined together on the matter.(I really too dishes)
Then after processing, the processing is equivalent to

a prefix, and such, there are several requirements for all positions in front of him he is smaller than a prefix. If the range is a little on the small tree with an array of requirements about gone, $ 3e7log (3e7) $ apparently timed out.
See this prefix and, before and after item maximum error is only $ 1 $, upper and lower bounds biggest difference does not exceed $ 3e7 $, and then make a prefix and this sum. A number indicates the number of occurrences of one array, and $ sum [m] = sum [ m-1] + b [m] $, and updates the prefix, the answer is $ ans + = sum [m- 1] $.
The middle there are some details to be processed

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121


using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> P;
typedef long double ld;

#define VNAME(value) (#value)
#define bug printf("*********n");
#define debug(x) cout<<"["<<VNAME(x)<<" = "<<(x)<<"]"<<endl;
#define mid (l+r)/2
#define chl 2*k+1
#define chr 2*k+2
#define lson l,mid,chl
#define rson mid+1,r,chr
#define mem(a, b) memset(a,b,sizeof(a));

const long long mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int INF = 0x7fffffff;
const LL inf = 0x3f3f3f3f;
const double eps = 1e-8;

void () {
#ifndef ONLINE_JUDGE
freopen("../data.in", "r", stdin);
#endif
}

#ifndef ONLINE_JUDGE
clock_t start = clock();
#endif
int n;
LL l[maxn], r[maxn];
LL f[maxn], g[maxn];
struct node {
LL l, r, x;
} dat[maxn * 5];
LL sum[maxn * 30 + 20], b[maxn * 30 + 20];

int main() {
fin();
大专栏  2019牛客暑期多校训练营(第二场)J Subarray">scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld%lld", &l[i], &r[i]);
}
f[1] = r[1] - l[1] + 1;
for (int i = 2; i <= n; i++) {
f[i] = max(0LL, f[i - 1] - (l[i] - r[i - 1] - 1)) + r[i] - l[i] + 1;
}
g[n] = r[n] - l[n] + 1;
for (int i = n - 1; i >= 1; --i) {
g[i] = max(0LL, g[i + 1] - (l[i + 1] - r[i] - 1)) + r[i] - l[i] + 1;
}
int i = 1;
LL ans = 0;

while (i <= n) {
int j = i + 1;
LL mi = 0, mx = 0, pos = 0;
//mi 下界,mx 上界
while (j <= n && g[j] + f[j - 1] >= l[j] - r[j - 1] - 1) {
j++;
}
j--;
int t = i, num = 1;
// [i,j] 区间是相互影响的
for (; t <= j; t++) { // 把每一段处理到 dat里面
if (num == 1)dat[num].l = 0;
else if (l[t] - r[t - 1] == 1) dat[num].l = pos + 1;
else dat[num].l = pos;
pos += r[t] - l[t] + 1;
dat[num].r = pos;
dat[num++].x = 1;
mx = max(mx, pos);
if (t != j) {
dat[num].r = pos - 1;
pos -= l[t + 1] - r[t] - 1;
dat[num].l = pos + 1;
dat[num++].x = 0; // 0 表示下降 ,1 表示上升
mi = min(pos, mi);
} else {
dat[num].r = pos - 1;
dat[num].l = max(mi, pos - ((int) 1e9 - 1 - r[t]));
dat[num++].x = 0;
}
}
dat[0].r = min(mx, l[i]);
dat[0].l = 1;
dat[0].x = 0;
for (int k = 0; k <= mx - mi + 200; k++)b[k] = sum[k] = 0;
assert(mx - mi < maxn * 30);
for (int k = 0; k < num; ++k) {
dat[k].l += -mi; //全部向上移动一个下届,保证最小值等于0
dat[k].r += -mi;
if (dat[k].x == 1) {
for (int m = dat[k].l; m <= dat[k].r; ++m) {
b[m]++;
sum[m] = sum[m - 1] + b[m];
if (m >= 1)ans += sum[m - 1];
}
} else {
LL tmp = 0;
if (dat[k].l > 0)tmp = sum[dat[k].l - 1];
for (int m = dat[k].l; m <= dat[k].r; ++m) {
if (m >= 1)ans += tmp;
tmp = sum[m]; //如果是下降的 ,从小往上处理是不能把当前更新加进去因当前这个在他后面
b[m]++;
sum[m] = sum[m - 1] + b[m];
}
}
}
i = j + 1;
}
printf("%lldn", ans);
#ifndef ONLINE_JUDGE
cout << "RUNTIME:" << (1.0 * clock() - start) / 1000 << "ms" << endl;
#endif
return 0;
}


给几组数据你去试试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
5 9
12 17

3
3 5
6 6
7 9

3
3 5
8 8
9 10

1
999999998 999999999
1
2
3
4
5
6
7
200

76

49

4

Guess you like

Origin www.cnblogs.com/lijianming180/p/12026724.html