70pts求调(卡半天了)(带注释)
查看原帖
70pts求调(卡半天了)(带注释)
1449881
TwiZoe楼主2025/8/31 22:43

一定关注大恩人www

#include<bits/stdc++.h>																		//必须维护线段而非端点
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
const int N = 10002;
int n, w, h,temp,tree[N<<3],tag[N<<3],val,L,R,ans;											//val,L,R用来update
long long a[N];																				//用来后续离散化
struct line {
	long long y;
	long long x1, x2;
	int v,type;                                                                           //v表示权值
	bool operator <(const line& t)const { return y == t.y ? type > t.type:y < t.y; }      //同高度入边优先
}lines[N<<1];
inline int get_id(long long x) {                                                           //获得离散化后的值(这里temp就是没有+1的)
	return std::lower_bound(a + 1, a + temp, x) - a;
}
void push_up(int p) {                                                                      //下面就是线段树的日常维护(维护[x,x+1) )
	tree[p] = std::max(tree[ls(p)], tree[rs(p)]);
	return;
}
void pushdown(int p) {
	tag[ls(p)] += tag[p];
	tag[rs(p)] += tag[p];
	tree[ls(p)] += tag[p];
	tree[rs(p)] += tag[p];
	tag[p] = 0;
	return;
}
void update(int p, int l, int r) {
	if (l == r) {
		tree[p] += val;
		return;
	}
	if (l >= L && r <= R) {
		tag[p] += val;
		tree[p] += val;
		return;
	}
	if(tag[p])pushdown(p);
	int mid = (l + r) >> 1;
	if (mid >= L)update(ls(p), l, mid);
	if (mid < R)update(rs(p), mid + 1, r);
	push_up(p);
	return;
}
inline void init(int i) {																  //给update中的L,R,val赋值
	L = get_id(lines[i].x1);
	R = get_id(lines[i].x2);
	val = lines[i].type==1 ? lines[i].v : -lines[i].v;
	return;
}
int main() {
	int T; scanf("%d", &T);
	while (T--) {
		memset(tag, 0, sizeof(tag));
		memset(tree, 0, sizeof(tree));
		ans = 0;
		scanf("%d %d %d", &n, &w, &h);
		temp = 0;
		for (int i = 1; i <= n; i++) {
			long long x, y;
			int v; 
			scanf("%lld %lld %d", &x, &y, &v);
			lines[++temp].y = y;
			lines[temp].v = v;
			lines[temp].type = 1;
			lines[temp].x1 = x - w;
			lines[temp].x2 = x - 1;
			a[temp] = x - w;
			lines[++temp].y = y + h-1;
			lines[temp].v = v;
			lines[temp].type = -1;
			lines[temp].x1 = x - w;
			lines[temp].x2 = x - 1;
			a[temp] = x - 1;
		}
		std::sort(a + 1, a + temp + 1);
		temp=std::unique(a + 1, a + temp + 1)-a;                                          //这里temp就是个数+1
		std::sort(lines + 1, lines + 2 * n + 1);
		for (int i = 1; i <= 2 * n; i++) {
			init(i);
			update(1, 1, temp-1);
			ans = std::max(ans, tree[1]);
			int cnt = 1;
			while (lines[i + cnt].y == lines[i].y) {                                      //一次处理同一高度的线
				init(i + cnt);
				update(1, 1, temp-1);
				ans = std::max(ans, tree[1]);
				cnt++;
			}
			i += --cnt;
		}
		printf("%d\n", ans);
	}
	return 0;
}
2025/8/31 22:43
加载中...