Sandglass

题目描述

We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand. When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb.
The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second. When the upper bulb no longer contains any sand, nothing happens.
Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X−a grams of sand (for a total of X grams).
We will turn over the sandglass at time r1,r2,..,rK. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0.
You are given Q queries. Each query is in the form of (ti,ai). For each query, assume that a=ai and find the amount of sand that would be contained in bulb A at time ti.

Constraints
1≤X≤109
1≤K≤105
1≤r1<r2<..<rK≤109
1≤Q≤105
0≤t1<t2<..<tQ≤109
0≤ai≤X(1≤i≤Q)
All input values are integers.

 

样例输入

180
3
60 120 180
3
30 90
61 1
180 180

 

样例输出

60
1
120

//总量为x,A中初始为a0,我们维护时间t之前A中的最大和最小值(0到x)
 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100050];
int main()
{
    int x,k,q;
    scanf("%d %d",&x,&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&a[i]);
    }
    int l=0,r=x;
    int cnt=0,j=0,s=-1,sum=0,ans,num,t,a0;//cnt用来记录上一次的时间点,ans来记录这一次的时间段,s用来表示是倒出还是倒入,j用来找到最靠近时间t的转折点。
    //t是询问的时间点,a0是A中的合法值,l是a中最小值,r是a中最大值,num记录转折点后的那一小段
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d %d",&t,&a0);
        while(j<k&&a[j+1]<=t)//不断更新找到t之前,转折点后A中的量
        {
            ans=s*(a[j+1]-cnt);
            l=max(0,min(x,l+ans));
            r=max(0,min(x,r+ans));
            cnt=a[j+1];
            s=-s;
            sum+=ans;
            j++;
        }
        num=t-cnt;//t之前,转折点后,做最后的维护
        a0=max(l,min(r,a0+sum));
        a0=max(0,min(x,a0+s*num));
        printf("%d\n",a0);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值