刚学OI,为何WA一个点,求助
查看原帖
刚学OI,为何WA一个点,求助
49719
__JR_飘摇__楼主2018/11/4 18:20

具体思路是把y放进线段树里

#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL T,n,w,h,maxx,v,p;
LL b[41000];
struct line
{
	LL x,l,r,w;
}a[41000];
struct segment_tree
{
	LL q[81000],f[81000];
	void clear()
	{
		memset(q, 0, sizeof(0));
		memset(f, 0, sizeof(0));
	}
	void Add(int x,int l,int r,int fx,int fy,int num)//标记永久化线段树 
	{
		if(fx <= l && fy >= r){q[x] += num;f[x] += num;return;}
		int mid = (l + r) >> 1;
		if(fx <= mid) Add((x << 1), l, mid, fx, fy, num);
		if(fy > mid) Add((x << 1) + 1, mid + 1, r, fx, fy, num);
		f[x] = max(f[x << 1], f[(x << 1) + 1]) + q[x];
	}
	void add(int l,int r,int w){Add(1, 1, 2 * n, l, r, w);}
	LL query() {return f[1];} 
}G;
bool cmp(const line&a,const line&b)
{
	return a.x < b.x;
}
int main()
{
	scanf("%lld", &T);
	while(T --)
	{
		maxx = 0;G.clear();v = 0;p = 0;
		scanf("%lld%lld%lld", &n, &w, &h);
		for(int i = 1;i <= n;i ++)
		{
			LL x,y,c;
			scanf("%lld%lld%lld", &x, &y, &c);
			int x2 = x + w - 1,y2 = y + h - 1;
			a[++ v].x = x;a[v].l = y;a[v].r = y2;a[v].w = c;
			a[++ v].x = x2 + 1;a[v].l = y;a[v].r = y2;a[v].w = - c;
			b[++ p] = y;b[++ p] = y2;
		}
		sort(b + 1,b + p + 1);
		for(int i = 1;i <= v;i ++)//离散化 
		{
			a[i].l = lower_bound(b + 1,b + p + 1,a[i].l) - b;
			a[i].r = lower_bound(b + 1,b + p + 1,a[i].r) - b;
		}
		sort(a + 1,a + v + 1,cmp);
		for(int i = 1;i <= v;i ++)
		{
			G.add(a[i].l, a[i].r, a[i].w);
			maxx = max(maxx, G.query());
		}
		printf("%lld\n", maxx);
	}
}

求查错。

2018/11/4 18:20
加载中...