HDU 6638 Snowy Smile(线段树维护最大连续子段和)

传送门

代码

/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://www.cnblogs.com/Knowledge-Pig/ 
	> Created Time: 2021/11/11 15:17:05
 ************************************************************************/

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define LL long long
#define pb push_back
#define fi first
#define se second
#define pr pair<int,int>
#define mk(a,b) make_pair(a,b)
#define endl '\n'
#define ls (node << 1)
#define rs (node << 1 | 1)
using namespace std;
struct node{
	int x, y, w;
}a[2021];
struct tree{
	LL L, R, ans, sum;
}tr[2021 * 30];
int n, x[2021], y[2021];
vector<int> q[2021];
void build(int node, int l, int r){
	tr[node].L = tr[node].R = tr[node].ans = tr[node].sum = 0;
	if(l >= r) return;
	int mid = (l + r) >> 1;
	build(ls, l, mid); build(rs, mid + 1, r);
}
void push_up(int node){
	tr[node].sum = tr[ls].sum + tr[rs].sum;
	tr[node].L = max(tr[ls].L, tr[ls].sum + tr[rs].L);
	tr[node].R = max(tr[rs].R, tr[rs].sum + tr[ls].R);
	tr[node].ans = max(tr[ls].ans, tr[rs].ans);
	tr[node].ans = max(tr[node].ans, tr[ls].R + tr[rs].L);
}
void update(int node, int l, int r, int pos, int add){
	if(l >= r){
		tr[node].sum += add;
		tr[node].L = tr[node].R = tr[node].ans = max(tr[node].sum, 0ll);
		return ;
	}
	int mid = (l + r) >> 1;
	if(pos <= mid) update(ls, l, mid, pos, add);
	else update(rs, mid + 1, r, pos, add);
	push_up(node);
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	int T; cin >> T;
	while(T--){
		cin >> n;
		for(int i = 1; i <= n; ++i){
			cin >> a[i].x >> a[i].y >> a[i].w;
			x[i] = a[i].x; y[i] = a[i].y;
			q[i].clear();
		}
		sort(x + 1, x + n + 1);
		sort(y + 1, y + n + 1);
		int nx = unique(x + 1, x + n + 1) - x - 1;
		int ny = unique(y + 1, y + n + 1) - y - 1;
		for(int i = 1; i <= n; ++i){
			a[i].x = lower_bound(x + 1, x + nx + 1, a[i].x) - x;
			a[i].y = lower_bound(y + 1, y + ny + 1, a[i].y) - y;
			q[a[i].x].push_back(i);
		}
		LL ans = 0;
		for(int i = 1; i <= nx; ++i){
			build(1, 1, ny);
			for(int j = i; j <= nx; ++j){
				for(auto u : q[j]) update(1, 1, ny, a[u].y, a[u].w);
				ans = max(ans, tr[1].ans);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
posted @ 2021-11-11 16:32  Knowledge-Pig  阅读(99)  评论(0)    收藏  举报