比赛 |
2022级DP专题练习赛8 |
评测结果 |
AAAAAAAAAAAAAAA |
题目名称 |
Springboards |
最终得分 |
100 |
用户昵称 |
yrtiop |
运行时间 |
1.638 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2023-03-01 19:34:28 |
显示代码纯文本
#include <bits/stdc++.h>
using i64 = long long;
const int maxn = 1e5 + 5;
const i64 INF = 1e16;
struct node {
int x1,y1,x2,y2,id;
node() {
x1 = y1 = x2 = y2 = id = 0;
}
node(int x1,int y1,int x2,int y2):x1(x1),y1(y1),x2(x2),y2(y2){};
} a[maxn];
int n,cnt;
i64 m,c[maxn << 2],f[maxn];
struct BIT {
i64 c[maxn << 2];
BIT() {
for(int i = 0;i < (maxn << 2);++ i)
c[i] = INF;
}
int lowbit(int x) {
return x & -x;
}
void add(int x,i64 y) {
for(;x <= cnt;x += lowbit(x))
c[x] = std::min(c[x] , y);
return ;
}
i64 query(int x) {
i64 ans = INF;
for(;x;x -= lowbit(x))
ans = std::min(ans , c[x]);
return ans;
}
void clear(int x) {
for(;x <= cnt;x += lowbit(x))
c[x] = INF;
return ;
}
} tr;
void solve(int l,int r) {
if(l >= r)
return ;
int mid = (l + r) >> 1;
solve(l , mid);
std::sort(a + l , a + mid + 1 , [&](const node& p,const node& q) {
return (p.x2 ^ q.x2) ? p.x2 < q.x2 : p.y2 < q.y2;
});
int i = l;
for(int k = mid + 1;k <= r;++ k) {
for(;i <= mid&&a[i].x2 <= a[k].x1;++ i)
tr.add(a[i].y2 , f[a[i].id] - c[a[i].x2] - c[a[i].y2]);
f[a[k].id] = std::min(f[a[k].id] , c[a[k].x1] + c[a[k].y1] + tr.query(a[k].y1));
}
for(;i > l;-- i)
tr.clear(a[i - 1].y2);
solve(mid + 1 , r);
return ;
}
int main() {
freopen("usaco_Jan_boards.in","r",stdin);
freopen("usaco_Jan_boards.out","w",stdout);
scanf("%lld %d",&m,&n);
for(int i = 1;i <= n;++ i) {
i64 x,y,l,r;
scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
c[++ cnt] = x;
c[++ cnt] = y;
c[++ cnt] = l;
c[++ cnt] = r;
a[i] = node(x , y , l , r);
a[i].id = i;
}
c[++ cnt] = 0;
std::sort(c + 1 , c + 1 + cnt);
cnt = std::unique(c + 1 , c + 1 + cnt) - c - 1;
for(int i = 0;i <= n;++ i) {
a[i].x1 = std::lower_bound(c + 1 , c + 1 + cnt , a[i].x1) - c;
a[i].y1 = std::lower_bound(c + 1 , c + 1 + cnt , a[i].y1) - c;
a[i].x2 = std::lower_bound(c + 1 , c + 1 + cnt , a[i].x2) - c;
a[i].y2 = std::lower_bound(c + 1 , c + 1 + cnt , a[i].y2) - c;
}
std::sort(a + 1 , a + 1 + n , [&](const node& p,const node& q) {
return (p.x1 ^ q.x1) ? p.x1 < q.x1 : p.y1 < q.y1;
});
for(int i = 1;i <= n;++ i)
f[i] = 2 * m;
solve(0 , n);
i64 ans = 2 * m;
for(int i = 0;i <= n;++ i)
ans = std::min(ans , m - c[a[i].x2] + m - c[a[i].y2] + f[a[i].id]);
printf("%lld\n",ans);
return 0;
}