Atocder ARC082 F-Sandglass


Problem Statement

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

All input values are integers.

Input

The input is given from Standard Input in the following format:

Output

For each query, print the answer in its own line.

Sample Input 1

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

Sample Output 1

60
1
120
In the first query, 30 out of the initial 90 grams of sand will drop from bulb A, resulting in 60 grams. In the second query, the initial 1 gram of sand will drop from bulb A, and nothing will happen for the next 59 seconds. Then, we will turn over the sandglass, and 1 second after this, bulb A contains 1 gram of sand at the time in question.

Sample Input 2

100
1
100000
4
0 100
90 100
100 100
101 100

Sample Output 2

100
10
0
0
In every query, the upper bulb initially contains 100 grams, and the question in time comes before we turn over the sandglass.

Sample Input 3

100
5
48 141 231 314 425
7
0 19
50 98
143 30
231 55
342 0
365 100
600 10

Sample Output 3

19
52
91
10
58
42
100


题目大意:有一个沙漏分成AB两面,里面共有X个点位的沙子,每一单位时间向下漏1个单位的沙子。
然后有n次操作,每次把沙漏翻转(不消耗时间)
询问当初始A有ai的沙子,B有X-ai的沙子,在t时刻A中有多少沙子


考场上平方暴力

然后想想正解,挺神奇的一道题

首先我们可以用ai=0和X各按照规则跑一遍,跑出每个操作的A中沙子的上下界限

然后我们考虑预处理偏量,不考虑规则,预处理每个时刻的偏量

然后我们对于起始的量ai,直接加上偏量,如果在0和X处理出的区间内就合法,否则变成最靠近的一个

考虑如果直接处理偏量和0或X的折线相交会发生什么

首先明确ai只有可能在0或X触及界限的时候才会相交

那么在相交点右侧一定会存在一个转折点,那么在这个转折点位置ai一定比0低或比X高,所以在这之后ai一定不可能再回到0和X之间

然后就很简单了,是可以做到线性,但我懒,就多挂了一个log


#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define LL long long
int n,q;
LL X,r[N];
struct Node{LL a,t;}p[N];
LL l_line[N],r_line[N];
LL cnt[N];
int main(){
scanf("%lld%d",&X,&n);
r[0]=0;for(int i=1;i<=n;i++)scanf("%lld",&r[i]);
scanf("%d",&q);
for(int i=1;i<=q;i++)scanf("%lld%lld",&p[i].t,&p[i].a);
LL na=0,nb=X;
l_line[0]=0;r_line[0]=X;
for(int tmp=1;tmp<=n;tmp++){
if(tmp&1){
LL tip=min(na,r[tmp]-r[tmp-1]);
na-=tip;
nb+=tip;
}else{
LL tip=min(nb,r[tmp]-r[tmp-1]);
na+=tip;
nb-=tip;
}
l_line[tmp]=na;
}
na=X,nb=0;
for(int tmp=1;tmp<=n;tmp++){
if(tmp&1){
LL tip=min(na,r[tmp]-r[tmp-1]);
na-=tip;
nb+=tip;
}else{
LL tip=min(nb,r[tmp]-r[tmp-1]);
na+=tip;
nb-=tip;
}
r_line[tmp]=na;
}
for(int i=1;i<=n;i++)
if(i&1)cnt[i]=cnt[i-1]-(r[i]-r[i-1]);
else cnt[i]=cnt[i-1]+(r[i]-r[i-1]);
for(int i=1;i<=q;i++){
int ll=1,rr=n,res=0;
while(ll<=rr){
int mid=(ll+rr)>>1;
if(r[mid]<=p[i].t)res=mid,ll=mid+1;
else rr=mid-1;
}
LL pic=p[i].a+cnt[res];
if(l_line[res]>pic)pic=l_line[res];
if(r_line[res]<pic)pic=r_line[res];
if(res&1)pic+=min(X-pic,p[i].t-r[res]);
else pic-=min(pic,p[i].t-r[res]);
printf("%lld\n",pic);
}
return 0;
}

Atocder ARC082 F-Sandglass 【思维题】*的更多相关文章

  1. 【AtCoder】ARC082 F - Sandglass

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

  2. AtCoder Beginner Contest 188 F - +1-1x2 思维题

    题目描述 给你两个数 \(x\),\(y\) 可以对 \(x\) 进行 \(+1,-1\) 或 \(\times 2\) 的操作 问最少操作多少次后变为 \(y\) \(x,y \leq 10^{18 ...

  3. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  4. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  5. UVA.11384 Help is needed for Dexter (思维题)

    UVA.11384 Help is needed for Dexter (思维题) 题意分析 同样水题一道,这回思路对了. 给出数字n,面对一个1,2,3,4--n的数字序列,你可以对他们的部分或者全 ...

  6. BZOJ 2734 洛谷 3226 [HNOI2012]集合选数【状压DP】【思维题】

    [题解] 思维题,看了别人的博客才会写. 写出这样的矩阵: 1,3,9,... 2,6,18,... 4,12.36,... 8,24,72,... 我们要做的就是从矩阵中选出一些数字,但是不能选相邻 ...

  7. 【2019.7.15 NOIP模拟赛 T1】夹缝(mirror)(思维题)

    思维题 此题应该是比较偏思维的. 假设一次反射后前进的距离是\(2^x(2y+1)\),则显然,它可以看做是前进距离为\(2^x\)的光线经过了\((2y+1)\)次反射,两者是等价的,甚至后者可能还 ...

  8. [Hdu-5155] Harry And Magic Box[思维题+容斥,计数Dp]

    Online Judge:Hdu5155 Label:思维题+容斥,计数Dp 题面: 题目描述 给定一个大小为\(N*M\)的神奇盒子,里面每行每列都至少有一个钻石,问可行的排列方案数.由于答案较大, ...

  9. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

随机推荐

  1. python中的上下文管理器

    刚刚看了vamei大神的上下文管理器博客,理解如下: 其实我自己经常用到上下文管理器,尤其是在打开文件的时候,如果自己比较懒,不想手工打上f.close(),使用上下文管理器就ok拉. 上下文管理器就 ...

  2. 01_Storm体系概要

    1. Storm发展历史 Storm历史 1. 2010年12月,backtype公司Nathan,提出Storm的核心概念2. backtype, 提供数据分析,数据处理服务的一个公司3. 2011 ...

  3. 生产消费模式:多线程读写队列ConcurrentQueue

    需求:现需要将多个数据源的数据导入到目标数据库,这是一个经典的生产消费应用的例子. 直接上代码,看下实现: // 初始化列队缓冲区 队列大小为100 IDataCollection<List&l ...

  4. 不一样的入门:看C# Hello World的17种写法

    摘要:本文针对不同阶段.不同程度的C#学习者,介绍了C# Hello World的17种不同写法,希望会对大家有所帮助.(C# Hello World写法入门.C# Hello World写法进阶.C ...

  5. nlp基本知识点(不断更新)

    1.精确率与召回率 召回率也称为查全率,指的是:你查询到的相关数目/(你查询到的相关的数目+你没有查询到的相关的数目). 精确率: A/A+B 召回率:A/A+C 再比如: 我 是 中国人 这里正确的 ...

  6. Educational Codeforces Round 13 A、B、C、D

    A. Johny Likes Numbers time limit per test 0.5 seconds memory limit per test 256 megabytes input sta ...

  7. IE类兼容一

    X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的.通过在meta中设置X-UA-Compatible的值,可以指定网页的兼容性模式设置. 在网页中指定的模式优 ...

  8. 如何使移动web页面禁止横屏?

    https://segmentfault.com/q/1010000005813183 一般只有移动版有这种需求,我们一般不去禁止,而是比例缩放,css实现,竖屏1rem = 9pt ,横屏1rem ...

  9. Android系统源代码

    Android系统源代码 在线源码网站 1,http://androidxref.com 2,http://www.grepcode.com/ 3,http://www.androidos.net.c ...

  10. cordova安卓sdk

    Android SDK在线更新镜像服务器来下载安装: 1.北京化工大学镜像服务器地址: IPv4: ubuntu.buct.edu.cn/ 端口:80 IPv4: ubuntu.buct.cn/ 端口 ...