[NOI1998] 免费的馅饼(三维偏序转二维偏序)
https://www.luogu.com.cn/problem/P7302
接完 i i i 能去接 j j j 的充要条件是什么?
- t i ≤ t j t_i\le t_j ti≤tj
- ∣ p i − p j ∣ ≤ 2 ( t j − t i ) |p_i-p_j|\le 2(t_j-t_i) ∣pi−pj∣≤2(tj−ti)
绝对值的套路就是拆掉
- p i + 2 t i ≤ p j + 2 t j p_i+2t_i\le p_j+2t_j pi+2ti≤pj+2tj
- 2 t i − p i ≤ 2 t j − p j 2t_i-p_i\le 2t_j-p_j 2ti−pi≤2tj−pj
此时是一个三维偏序问题,我们可以直接cdq。但我们考虑把 2 , 3 2,3 2,3 式相加:
4 t i ≤ 4 t j 4t_i\le 4t_j 4ti≤4tj,发现 1 1 1 条件满足了。变成二维偏序问题
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define debag(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 200010
struct node {
int a, b, w;
}a[N];
int n, m, i, j, k, T;
int b[N], t, p, w, ans;
bool cmp(node x, node y) {
if(x.a == y.a) return x.b < y.b;
return x.a < y.a;
}
struct Binary_tree {
int cnt[N];
void add(int x, int k) {
while(x < N) cnt[x] = max(cnt[x], k), x += x & -x;
}
int qry(int x) {
int ans = 0;
while(x) ans = max(ans, cnt[x]), x -= x & -x;
return ans;
}
}Bin;
signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T = read();
// while(T--) {
//
// }
n = read(); n = read();
for(i = 1; i <= n; ++i) {
t = 2 * read(); p = read(); w = read();
a[i].a = t + p; a[i].b = t - p; a[i].w = w; //a[i].t = read();
b[i] = t - p;
debug("[%lld %lld] ===> (%lld %lld)\n", t, p, a[i].a, a[i].b);
}
sort(a + 1, a + n + 1, cmp);
sort(b + 1, b + n + 1);
for(i = 1; i <= n; ++i) a[i].b = lower_bound(b + 1, b + n + 1, a[i].b) - b;
for(i = 1; i <= n; ++i) {
debug("%lld %lld | %lld\n", a[i].a, a[i].b, a[i].w);
k = Bin.qry(a[i].b) + a[i].w;
Bin.add(a[i].b, k); ans = max(ans, k);
}
printf("%lld", ans);
return 0;
}