2019-2020 XX Open Cup, Grand Prix of Korea

J. Park life

题解

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+7;
int n,val[N];

struct node
{
int l,r,pos;

}a[N];

vector <int> f[N];
priority_queue <long long >dp[N];
bool cmp(node x,node y)
{
if(x.l==y.l)
return x.r>y.r;
return x.l<y.l;
}

struct compare
{
bool operator ()(node x,node y)
{
if(x.l==y.l)
return x.r>y.r;
return x.l<y.l;
}
};

void get_tree()
{
stack <node>s;
for(int i=n;i>=0;i--)
{
while(!s.empty()&&(a[i].r>=s.top().r))
{
f[a[i].pos].push_back(s.top().pos);
s.pop();
}
s.push(a[i]);
}

}

void dfs(int x)
{

for(auto i:f[x])
{
dfs(i);
if(dp[i].size()>dp[x].size())
{
swap(dp[x],dp[i]);//启发式合并
}
vector <long long > temp;
while(!dp[i].empty()) //答案集合的顺序组合
{
temp.push_back(dp[i].top()+dp[x].top());
dp[i].pop();
dp[x].pop();
}
for(auto j:temp)
{
dp[x].push(j);
}
}
dp[x].push(val[x]);//加上val
}
void print()
{
for(int i=0;i<=n;i++)
{
printf("i:%d\n",i);
for(auto j:f[i])
{
printf("%d\n",j);
}
printf("\n");
}
}
int main()
{
scanf("%d",&n);
a[0].l=0,a[0].r=1e6+5;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&a[i].l,&a[i].r,&val[i]);
a[i].r--;
a[i].pos=i;
}
sort(a,a+n+1,cmp);
get_tree();

dfs(0);
long long ans=0;
for(int i=1;i<=n;i++)
{
if(!dp[0].empty())
{
ans+=dp[0].top();//每次答案累加
dp[0].pop();
}
printf("%lld ",ans);
}
return 0;
}

/*
6
1 2 10
2 3 10
1 3 21
3 4 10
4 5 10
3 5 19

4
1 5 1
2 5 1
3 5 1
4 5 1

*/

$道路千万条,点赞第一条;阅读不规范,笔者两行泪$
posted @ 2020-11-15 20:52  Sakura_Momoko  阅读(309)  评论(0编辑  收藏  举报