看到题目很容易想到用线段树写,不过为了挑战一下自己的思维,看了别人的代码,纠结的将思路理清楚了,磨蹭了一天才搞定。
首先将线段离散化,求出每个预处理的线段的刚好通过和刚好通不过的时间边界,最后按时间顺序扫描。
ps:用结构体内部排序比外部排序开了将近3倍。
ACcode:
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int NS=100010;
struct Node
{
int d,t,f;
Node() {}
Node(int a,int b,int c): d(a),t(b),f(c){}
bool operator < (const Node cmp) const{
return d<cmp.d;
}
}L[NS<<1];
struct NOde
{
int d,t,f;
NOde() {}
NOde(int a,int b,int c): d(a),t(b),f(c){}
bool operator < (const NOde cmp) const{
return t<cmp.t;
}
}X[NS<<2];
int n,m,l,r,d,f,t,p,top,ans,cnt;
multiset<int> myset;
bool cmp1(Node a1,Node a2)
{
if (a1.d!=a2.d) return a1.d<a2.d;
return a1.f>a2.f;
}
bool cmp2(Node a1,Node a2)
{
if (a1.t!=a2.t) return a1.t<a2.t;
return a1.f>a2.f;
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
top=0;
for (int i=0;i<m;i++)
{
scanf("%d%d%d",&l,&r,&t);
L[i+i]=Node(l,t,1);
L[i+i+1]=Node(r,t,-1);
}
m=m+m;
sort(L,L+m);
for (int i=0;i<m;i++)
{
d=L[i].d,t=L[i].t,f=L[i].f;
if (!myset.empty())
{
X[top++]=NOde(l,*myset.begin()-p,1);
X[top++]=NOde(r,*myset.begin()-d,-1);
}
p=d;
if (f>0) myset.insert(t);
else myset.erase(myset.find(t));
}
sort(X,X+top);
ans=cnt=0;
for (int i=0,j=0;i<n;i++)
{
scanf("%d",&t);
for (;j<top&&X[j].t<=t;j++)
{
if (X[j].f<0)
{
cnt++;
ans-=X[j].t;
}
else
{
cnt--;
ans+=X[j].t;
}
}
printf("%d\n",ans+t*cnt);
}
}
return 0;
}