Gym 101190E (NEERC2016) Expect to Wait

Gym 101190E (NEERC2016) Expect to Wait

扫描线

传送门:HustOJ

题意

给你n个共享单车的 [借/还,数量,时间]。有q个查询,每个查询表示若初始有m个单车,那么所有人的等待时间总和是多少。

思路

%%%

首先得看出来这是个扫描线。。。
然后跟普通扫描线不同的是:从y轴扫下来时,x的长度只增不减。所以不需要用线段树维护边,只要扫一遍边集,每次加一下面积就可以了。

存边时存横边的y值和长度,扫描时用一个平行于x轴的线从y方向扫下来。每遇到一条边,更新面积。

代码

注意开数组要开 2105 ,因为n和q的边都存进去了

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
#define M(a,b) memset(a,b,sizeof(a))
#define N n
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int MAXN=200007;
const int oo=0x3f3f3f3f;
typedef long long LL;
const LL loo=4223372036854775807ll;
typedef long double LB;
const LL mod=1e9+7;
const double eps=1e-6;

struct Line
{
    int k;LL x; LL y;
    Line() { k=x=y=0; }
    Line(int _a, LL _b, LL _c) { k=_a, x=_b, y=_c; }
    bool operator < (const Line &t) const
    {
        if(y!=t.y)
            return y>t.y;
        else return k>t.k;
    }
}l[MAXN];
LL res[MAXN];
int main()
{
    //freopen("expect.in", "r", stdin);
    //freopen("expect.out", "w", stdout);

    int n, m;
    while(scanf("%d%d", &n, &m)==2)
    {
        LL last1=0, last2=0;LL now=0;LL ma=-oo;
        for(int i=0;i<n;i++)
        {
            char c;LL ta, tb;
            scanf(" %c %lld %lld", &c, &ta, &tb);
            if(c=='-')
            {
                l[now]=Line(1, ta-last2, last1);
                last1+=tb;
            }
            else
            {
                l[now]=Line(1, ta-last2, last1);
                last1-=tb;
            }
            ma=max(ma, last1);
            last2=ta;
            now++;
        }
        for(int i=0;i<m;i++)
        {
            LL tmp;scanf("%lld", &tmp);
            l[now++]=Line(2, i, tmp);
            if(tmp<last1) res[i]=-1;
        }
        sort(l, l+now);
        LL ans=0, k=0, lastk=ma;
        for(int i=0;i<now;i++)
        {
            ans+=(lastk-l[i].y)*k;
            if(l[i].k==1)
            {
                k+=l[i].x;
            }
            else
            {
                if(res[l[i].x]!=-1) res[l[i].x]=ans;
            }
            lastk=l[i].y;
        }
        for(int i=0;i<m;i++)
        {
            if(res[i]==-1) printf("INFINITY\n");
            else printf("%lld\n", res[i]);
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值