0%

[題解]線段覆蓋長度

a160. 線段覆蓋長度

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
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 10005
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n;
pii a[N];

signed main(){
Orz;
while(cin>>n){
rep(i,0,n-1)cin>>a[i].x>>a[i].y;
sort(a,a+n);

int sum = a[0].y - a[0].x,R = a[0].y;
for(int i=1;i<n;i++){
if(a[i].y > R){
sum += (a[i].y-a[i].x)-(R-a[i].x)*((R-a[i].x)>0);
R = a[i].y;
}
}
cout<<sum<<endl;
}
}