题目描述

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 题解移步晨哥博客https://www.cnblogs.com/tian-luo/p/9387640.html然后其实题目数据是按照时间顺序给的,所以我代码中的排序其实是没有用的qwq
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e5+;
ll x;
int n,m;
struct orz{
ll t,a;
int id;}c[N];
ll r[N],ans[N];
bool cmp(orz a,orz b)
{
return a.t<b.t;
}
int main()
{
scanf("%lld",&x);
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%lld",&r[i]);
scanf("%d",&m);
for (int i=;i<=m;i++)
{
scanf("%lld%lld",&c[i].t,&c[i].a);
c[i].id=i;
}
sort(c+,c++m,cmp);
r[n+]=c[m].t+;
ll mx=x,mn=,tmp=,tt=,flag=-; int k=;
for (int i=;i<=m;i++)
{
while (r[k]<=c[i].t&&k<=n)
{
tt=(r[k]-r[k-])*flag;
tmp+=tt;
mx+=tt; mn+=tt;
if (mn>x) mn=x; if (mn<) mn=;
if (mx>x) mx=x; if (mx<) mx=;
k++; flag*=-;
} ll now=c[i].a+tmp; //cout<<'*'<<now<<' '<<tmp<<' ';
if (now>mx) now=mx; else if (now<mn) now=mn; //cout<<now<<endl; if (k%) ans[c[i].id]=max(now-(c[i].t-r[k-]),(ll));
else ans[c[i].id]=min(now+(c[i].t-r[k-]),x);
}
for (int i=;i<=m;i++) printf("%lld\n",ans[i]);
return ;
}
												

Sandglass的更多相关文章

  1. 【AtCoder】ARC082 F - Sandglass

    [链接]F - Sandglass [题意]给定沙漏A和B,分别装着a和X-a的沙子,开始时A在上B在下,每秒漏1,漏完不再漏.给定n,有n个时刻ai沙漏倒转.给定m个询问,每次询问给定初值a和时刻t ...

  2. 2017国家集训队作业[arc082d]Sandglass

    2017国家集训队作业[arc082d]Sandglass 题意: ​ 有一个沙漏,初始时\(A\)瓶在上方,两个瓶子的最大容量都为\(X\)克,沙子流动的速度为\(1g\)每单位时间.给出\(K\) ...

  3. 【ARC082D】Sandglass

    Description ​ 题目链接 Description ​ 好题.题意是维护一个初始值,交替加减一段时间,有上界\(m\)和下界0(不能超过这两条界限),问对于某一种初始值,在某一个时刻时该值为 ...

  4. [arc082F]Sandglass

    Description 传送门 Solution 这题是真的666啊... 以下是本题最关键最关键的结论:如果ai<=aj,则在某个时间t,前者的A中沙子克数(记为t(ai))一定大于等于t(a ...

  5. [arc082f]Sandglass 递推

    Description 有一个沙漏由两个上下相通玻璃球A和B构成,这两个玻璃球都含有一定量的沙子,我们暂且假定AB中位于上方的玻璃球的为U,下方的玻璃球为L,则除非U中没有沙子,否则每秒钟都会有1克沙 ...

  6. 【推导】【模拟】AtCoder Regular Contest 082 F - Sandglass

    题意:有个沙漏,一开始bulb A在上,bulb B在下,A内有a数量的沙子,每一秒会向下掉落1.然后在K个时间点ri,会将沙漏倒置.然后又有m个询问,每次给a一个赋值ai,然后询问你在ti时刻,bu ...

  7. Arc082_F Sandglass

    Description有一个沙漏由两个上下相通玻璃球$A$和$B$构成,这两个玻璃球都含有一定量的沙子,我们暂且假定$A,B$中位于上方的玻璃球的为$U$,下方的玻璃球为$L$,则除非$U$中没有沙子 ...

  8. [ARC082F] Sandglass(线段树)

    Description 有一个沙漏由两个上下相通玻璃球 \(A\) 和 \(B\) 构成,这两个玻璃球都含有一定量的沙子,我们暂且假定 \(AB\) 中位于上方的玻璃球的为 \(U\),下方的玻璃球为 ...

  9. 【AtCoder Regular Contest 082 F】Sandglass

    [链接]点击打开链接 [题意] 你有一个沙漏. 沙漏里面总共有X单位的沙子. 沙漏分A,B上下两个部分. 沙漏从上半部分漏沙子到下半部分. 每个时间单位漏1单位的沙子. 一开始A部分在上面.然后在r1 ...

随机推荐

  1. HBase 参考文档翻译之 Getting Started

    本篇是对HBase官方参考文档的大体翻译,介于本人英文水平实在有限,难免有纰漏之处.本篇不只是对官方文档的翻译,还加入了一些本人对HBase的理解.在翻译过程中,一些没有营养的废话,我就忽略了没有翻译 ...

  2. Web后台任务处理

    文章:.NET Core开源组件:后台任务利器之Hangfire Hangfire官网介绍:在.NET和.NET Core应用程序中执行后台处理的简便方法.无需Windows服务或单独的过程. 以持久 ...

  3. [codecademy]html&css

    1. HTML is the language used to create the web pages you visit everyday. It provides a logical way t ...

  4. js 拼接字符串时,本来想要’#1′ ,返回的却是’#01′

    今天在操作一个元素时,id值是拼接的. var index = $(this).attr(‘index’);    //0var id = ‘#’ + (index+1);    //#01$(id) ...

  5. animate.css与wow.js制作网站动效

    animate.css 官网:https://daneden.github.io/animate.css/ 包括:attention seekers:关注者 bouncing entrances:跳跃 ...

  6. Python 时间推进器-->在当前时间的基础上推前n天 | CST时间转化标准日期格式

    由于公司任务紧迫,好久没有在园子里写自己的心得了,今天偷个闲发表点简单的代码块,在开源的时代贡献微薄力量.话不多说,直接上代码块: ]) m = ]) d = ]) the_date = dateti ...

  7. Python文件操作大全,随机删除文件夹内的任意文件

     在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法: os.path.abspath(path) #返回绝对路径os.path.basename(path ...

  8. BZOJ4897 THUSC2016成绩单(区间dp)

    拿走一个区间的代价只与最大最小值有关,并且如果最后一次拿走包含区间右端点的子序列一定不会使答案更劣,于是设f[i][j][x][y]为使i~j区间剩余最小值为x最大值为y且若有数剩余一定包含j的最小代 ...

  9. BZOJ4289 PA2012Tax(最短路)

    一个暴力的做法是把边看成点,之间的边权为两边的较大权值,最短路即可.但这样显然会被菊花图之类的卡掉. 考虑优化建图.将边拆成两个有向边,同样化边为点.原图中同一条边在新图中的两个点之间连边权为原边权的 ...

  10. chrome源码之恢复上次打开的标签页的学习

    startup_browser_creator_impl.cc ————————>打开任何页面或浏览器的入口函数bool StartupBrowserCreatorImpl::ProcessSt ...