Meaning of the questions:
Length 1 E . 9 1E9 is ( 1 , - 1 ) (1, -1) sequence index from 0 0 to 1 E . 9 - 1 1e9-1, known n- n-intervals of 1 1, the other is - 1 -1, the number of intervals and the presence of Q > 1 > 1 (guaranteed [Sigma 1 ≤ I ≤ n- R & lt [ I ] - L [ I ] + 1 ≤ 1 E . 7 Σ1≤i≤nr [I] -l [ i] + 1≤1e7).
You n represents a n-1 consecutive segments now ask you a total sequence length of the number of sub-sections in the range of greater than 0 to 0 ~ 1e9-1.
answer
- As the number of possible end points up to point range . 3 E . 7 3E7
- F [ I] represented by the first I I intervals right answer to the right end point and the maximum section
- G [ I ] represented by the first I I answer intervals left end of the left point and the maximum section
- f[i]=max(0,f[i−1]−(l[i]−r[i−1]−1))+r[i]−l[i]+1
- g[i]=max(0,g[i+1]−(l[i+1]−r[i]−1))+r[i]−l[i]+1
- If F [ I ] + G [ I + . 1 ] ≥ L [ I + . 1 ] - R & lt [ I ] - . 1 , description answers about the endpoint may span [ R & lt [ I ] + . 1 , L [ I + . 1 ] - . 1 ] , then merge these considerations
- Finished out above, the question then becomes no more than give you a length3e7's(1, -1) sequences, and asked how many intervals greater than 1
- Fenwick tree time O ( n- * L O G n- ) , stableT
- Consider optimization:
- Good use of nature: each query and the gap is equal to 1 last query
- From left to right enumerate the left point, the right is greater than the current value of the statistics of the number of
- Add tags, tags left, stability
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
const int M = 4e7 + 10;
typedef long long ll;
int l[N], r[N], L[N], R[N];
ll num[M];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
l[0] = r[0] = L[0] = R [ 0 ] = - 1 , l [n + 1 ] = r [n + 1 ] = 1E9;
int len = 0 ;
for ( int i = 1 ; i <= n; i ++ ) {
len + = R [i] - L [i] + 1 ;
R [i] = min (r [i] + len, L [i + 1 ] - 1 );
len - = L [i + 1 ] - r [i] - 1 ;
if (len < 0 )
len = 0 ;
}
len= 0;
for (int i = n; i >= 1; i--) {
len += r[i] - l[i] + 1;
L[i] = max(l[i] - len, r[i - 1] + 1);
len -= l[i] - r[i - 1] - 1;
if (len < 0)
len = 0;
}
int now = 2e7 + 10;
ll sum = 0;
num[now] = 1;
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = max(L[i], R[i - 1] + 1); j <= R[i]; j++) {
if (j >= l[i] && j <= r[i]) {
sum += num[now];
num[++now]++;
} else {
sum -= num[--now];
num[now]++;
}
ans += sum;
}
}
cout << ans << endl;
return 0;
}
Reference blog:
https://blog.csdn.net/qq_40791842/article/details/96736137
https://blog.csdn.net/qq_40871466/article/details/97104326
https://blog.csdn.net/toohandsomeieaseid/article/details/98848517