#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long lint;
struct sti{int s,e,x;}a[100005], b[100005];
int atrack[100005], btrack[100005];
int fa[100005], fb[100005];
int n;
lint dp[100005][2],l;
bool cmp1(sti a, sti b){return a.s == b.s?(a.e < b.e) : (a.s < b.s);}
bool cmp2(sti a, sti b){return a.e == b.e?(a.s < b.s) : (a.e < b.e);}
lint f(int pos, int piv){
if(dp[pos][piv]) return dp[pos][piv];
lint res = 0;
if(piv == 0){
for (int i=pos+1; a[pos].s == a[i].s && i<n; i++) {
if(a[pos].e < a[i].e) res = max(res,f(btrack[fa[i]],1));
}
return dp[pos][piv] = res + l + abs(a[pos].e - a[pos].s);
}
else{
for (int i=pos+1; b[pos].e == b[i].e && i<n; i++) {
if(b[pos].s < b[i].s) res = max(res,f(atrack[fb[i]],0));
}
return dp[pos][piv] = res + l + abs(b[pos].e - b[pos].s);
}
}
int main(){
scanf("%d %lld",&n,&l);
for (int i=0; i<n; i++) {
scanf("%d %d",&a[i].s,&a[i].e);
a[i].x = i;
b[i] = a[i];
}
sort(a,a+n,cmp1);
sort(b,b+n,cmp2);
for (int i=0; i<n; i++) {
fa[i] = a[i].x;
fb[i] = b[i].x;
atrack[a[i].x] = i;
btrack[b[i].x] = i;
}
lint res = 0;
for (int i=0; i<n; i++) {
res = max(res,f(i,0));
res = max(res,f(i,1));
}
printf("%lld",res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6560 KB |
Output is correct |
2 |
Correct |
0 ms |
6560 KB |
Output is correct |
3 |
Correct |
0 ms |
6560 KB |
Output is correct |
4 |
Correct |
0 ms |
6560 KB |
Output is correct |
5 |
Correct |
0 ms |
6560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
6560 KB |
Output is correct |
2 |
Correct |
136 ms |
6560 KB |
Output is correct |
3 |
Correct |
208 ms |
6560 KB |
Output is correct |
4 |
Correct |
408 ms |
6560 KB |
Output is correct |
5 |
Correct |
276 ms |
6560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6560 KB |
Output is correct |
2 |
Correct |
0 ms |
6560 KB |
Output is correct |
3 |
Correct |
0 ms |
6560 KB |
Output is correct |
4 |
Correct |
0 ms |
6560 KB |
Output is correct |
5 |
Correct |
0 ms |
6560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6560 KB |
Output is correct |
2 |
Correct |
0 ms |
6560 KB |
Output is correct |
3 |
Correct |
4 ms |
6560 KB |
Output is correct |
4 |
Correct |
0 ms |
6560 KB |
Output is correct |
5 |
Correct |
0 ms |
6560 KB |
Output is correct |
6 |
Correct |
0 ms |
6560 KB |
Output is correct |
7 |
Correct |
0 ms |
6560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6560 KB |
Output is correct |
2 |
Correct |
8 ms |
6560 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
6556 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |