【模拟】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.
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
总之就是一个沙漏有A,B两半,X是A的容量,然后每个t时刻都会翻转一次。给你q个询问,每个询问给你a的初始值,问你t时刻a中有多少沙子
#include <bits/stdc++.h> #define ll long long using namespace std; pair<int,int>team[100050]; int x,k,t[100050],q; ll mn,mx,Now,ki,s; int main() { scanf("%d %d",&x,&k); for(int i=1;i<=k;i++) { scanf("%d",&t[i]); } scanf("%d",&q); for(int i=0;i<q;i++) { scanf("%d %d",&team[i].first,&team[i].second); } s=-1; mx=x; mn=0; ki=1; for(int i=0;i<q;i++) { while(ki<=k&&t[ki]<=team[i].first) { ll now=t[ki]-t[ki-1]; now*=s; s*=-1; mn+=now; mx+=now; mn=max(mn,(ll)0); mn=min(mn,(ll)x); mx=max(mx,(ll)0); mx=min(mx,(ll)x); Now+=now; ki++; } ll now=team[i].first-t[ki-1]; now*=s; ll ans=team[i].second+Now; if(ans<mn) ans=mn; if(ans>mx) ans=mx; ans+=now; ans=max(ans,(ll)0); ans=min(ans,(ll)x); printf("%lld\n",ans); } return 0; }