[NOI1998] 免费的馅饼(三维偏序转二维偏序)

https://www.luogu.com.cn/problem/P7302

接完 i i i 能去接 j j j 的充要条件是什么?

  1. t i ≤ t j t_i\le t_j titj
  2. ∣ p i − p j ∣ ≤ 2 ( t j − t i ) |p_i-p_j|\le 2(t_j-t_i) pipj2(tjti)

绝对值的套路就是拆掉

  1. p i + 2 t i ≤ p j + 2 t j p_i+2t_i\le p_j+2t_j pi+2tipj+2tj
  2. 2 t i − p i ≤ 2 t j − p j 2t_i-p_i\le 2t_j-p_j 2tipi2tjpj

此时是一个三维偏序问题,我们可以直接cdq。但我们考虑把 2 , 3 2,3 2,3 式相加:

4 t i ≤ 4 t j 4t_i\le 4t_j 4ti4tj,发现 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; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值