Discretization
The aim is to map discrete individual large section to a smaller interval, enhance space efficiency, used to find the relative position of the current position of the source sequence number in
for(int i=1;i<=n;++i){
t[i] = a[i];
}
sort(t+1,t+1+n);
end = unique(t+1,t+1+n)-t-1; // unique 返回去重后数组尾地址, end 为数组尾下标
for(int i=1;i<=n;++i){
a[i] = lower_bond(t+1,t+m+1,a[i])-t;// 在t中二分查找a[i] 再减去首地址 即可得到相对大小
}
Topic: luogo P1496 Huoshaochibi
- Title effect: Gives \ (n-\) line segments, the length required to cover
- Ideas: first of all the left and right endpoints discrete, ordered into several sections, with a differential array record a valid location, the difference array is updated prefix and the interval greater than zero
#include<bits/stdc++.h>
#define ll long long
#define FOR(i,n) for(int i =1; i <= n;++i )
#define FOR0(i,n) for(int i =0; i < n;++i )
#define inf 0x3f3f3f3f
#define EPS (1e-8)
#define ALL(T) T.begin(),T.end()
using namespace std;
const int maxn = 5e5+40;
int n;
int t[maxn],a[maxn],b[maxn],add[maxn];
int main(){
cin >> n;
FOR(i,n){
cin >> a[i] >> b[i];
t[i*2-1] = a[i];
t[i*2] = b[i];
}
sort(t+1,t+1+n+n);
int m = unique(t+1,t+1+n+n)-t-1;
for(int i=1;i<=n;++i){
a[i] = lower_bound(t+1,t+m+1,a[i])-t;
b[i] = lower_bound(t+1,t+1+m,b[i])-t;
add[a[i]]++; //原从 a[i] 到 b[i] 加 1
add[b[i]]--;
}
int temp=0,ans=0;
FOR(i,m){
temp+=add[i];
// cout << add[i] << ' '<< t[i] << endl;
if(temp > 0){
// 当前区间可用,更新右端点
ans = ans + t[i+1] - t[i];
}
}
cout << ans << endl;
return 0;
}
Feeling with the prefix and is very clever.
Title
discrete blog