题目传送门:https://www.luogu.org/problemnew/show/P2061
题意:
现在有m个操作,每一个操作将x~y中比z小的数改为z。求m次操作后的序列。
思路:
考虑一种新的东西,扫描线。
用堆来实现找最大的数,将数列分为一些相等的数的几个区间,对于每一个区间,O(1)求和即可、
代码:
#include<cstdio>
#include<queue>
#include<algorithm>
#define LL long long
using namespace std;
struct node
{
int x,y,z;
friend bool operator <(const node &x,const node &y)
{
return x.z<y.z;
}
};
priority_queue<node> f;
int n;
LL ans=0;
node a[40010];
int b[80010];
bool cmp(node x,node y)
{
return x.x<y.x;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].z);
b[(i<<1)-1]=a[i].x;
b[(i<<1)]=a[i].y;
}
sort(a+1,a+n+1,cmp);
sort(b+1,b+(n<<1)+1);
int p=0;
for(int i=1;i<n*2;i++)
{
while(!f.empty()&&f.top().y<=b[i]) f.pop();
while(b[i]<=a[p+1].x&&a[p+1].x<b[i+1]) f.push(a[++p]);
if(!f.empty()) ans+=(LL)(b[i+1]-b[i])*f.top().z;
}
printf("%lld",ans);
}