F - Fundraising Gym - 101889F（线段树DP）

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;

struct Node {
int x,y;
ll z;
bool operator < (const Node&rhs) const {
if(x != rhs.x) return x < rhs.x;
return y > rhs.y;
}
}a[maxn];

vector<int>vec;

struct Tree {
int l,r;
ll mx,lazy;
}t[maxn << 2];

void pushdown(int i) {
if(t[i].lazy) {
t[i * 2].lazy = max(t[i * 2].lazy,t[i].lazy);
t[i * 2].mx = max(t[i * 2].mx,t[i].lazy);

t[i * 2 + 1].lazy = max(t[i * 2 + 1].lazy,t[i].lazy);
t[i * 2 + 1].mx = max(t[i * 2 + 1].mx,t[i].lazy);
}
}

void pushup(int i) {
t[i].mx = max(t[i * 2].mx,t[i * 2 + 1].mx);
}

void build(int i,int l,int r) {
t[i].l = l;t[i].r = r;
if(l == r) {
return;
}
int m = (l + r) >> 1;
build(i * 2,l,m);
build(i * 2 + 1,m + 1,r);
pushup(i);
}

ll query(int i,int x,int y) {
if(x > y) return 0;
if(x <= t[i].l && t[i].r <= y) {
return t[i].mx;
}
int m = (t[i].l + t[i].r) >> 1;
ll res = 0;
pushdown(i);
if(x <= m) {
res = max(res,query(i * 2,x,y));
}
if(y > m) {
res = max(res,query(i * 2 + 1,x,y));
}
return res;
}

void update(int i,int x,int y,ll v) {
if(x > y) return ;
if(x <= t[i].l && t[i].r <= y) {
t[i].lazy = max(t[i].lazy,v);
t[i].mx = max(t[i].mx,v);
return;
}
pushdown(i);
int m = (t[i].l + t[i].r) >> 1;
if(x <= m) update(i * 2,x,y,v);
if(y > m) update(i * 2 + 1,x,y,v);
pushup(i);
}

int main() {
int n;scanf("%d",&n);
for(int i = 1;i <= n;i++) {
scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].z);
vec.push_back(a[i].y);
}
sort(a + 1,a + 1 + n);
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
int cnt = 0;
for(int i = 1;i <= n;i++) {
if(cnt && a[cnt].x == a[i].x && a[cnt].y == a[i].y) {
a[cnt].z += a[i].z;
} else {
a[++cnt] = a[i];
}
}
n = cnt;

int len = vec.size();
build(1,1,len);
ll ans = 0,pre = 0;

for(int i = n;i >= 1;i--) {
int pos = lower_bound(vec.begin(),vec.end(),a[i].y) - vec.begin() + 1;
ll num = query(1,pos + 1,len) + a[i].z;
ans = max(ans,num);
update(1,1,pos,num);
}

printf("%lld\n",ans);
return 0;
}


10-23 69
08-28 31
10-05 292
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐