【掃描線】Gym - 101190E - Expect to Wait
阿新 • • 發佈:2017-07-13
n) typedef mes 掃描 tdi printf wan std const
假設初始人數為0,
將每個時刻在等待的人數寫下來,就是求個和。
如果縱坐標看成人數,橫坐標看成時間,就是求個面積。
因為初始人數不一定為零,所以離線後掃描線即可回答所有詢問。
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int n,m,e; struct LINE{ int y,l,id; }ls[200010]; bool cmp(const LINE &a,const LINE &b){ return a.y!=b.y ? a.y>b.y : a.l<b.l; } ll anss[100010]; int main(){ freopen("expect.in","r",stdin); freopen("expect.out","w",stdout); int x,y; char op[4]; scanf("%d%d",&n,&m); int now=0,last=1; for(int i=1;i<=n;++i){ scanf("%s%d%d",op,&x,&y); if(now>0 && i>1){ ls[++e]=(LINE){now,x-last}; } if(op[0]==‘+‘){ now-=y; } else{ now+=y; } last=x; } if(now>0){ ls[++e]=(LINE){now,1000000001}; } for(int i=1;i<=m;++i){ ++e; scanf("%d",&ls[e].y); ls[e].id=i; ls[e].l=0; } sort(ls+1,ls+e+1,cmp); ll nowans=0; int nowl=0; int i=1; for(;i<=e;++i){ if(ls[i].l==0){ anss[ls[i].id]=nowans; } if(ls[i].y>0 && ls[i].l==1000000001){ break; } nowl+=(ll)ls[i].l; if(ls[i].y!=ls[i+1].y){ nowans+=(ll)(ls[i].y-ls[i+1].y)*(ll)nowl; } } for(;i<=e;++i)if(ls[i].l==0){ anss[ls[i].id]=-1; } for(i=1;i<=m;++i){ if(anss[i]==-1){ puts("INFINITY"); } else{ printf("%I64d\n",anss[i]); } } return 0; }
【掃描線】Gym - 101190E - Expect to Wait