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 编程测试练习2

    1.将A.txt(多行)文件的内容读取出来写入到B.txt中 2.总结 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚 ...

  2. RSA非对称加密算法

    基本定义: RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制.在公开密钥密码体制中,加密密钥(即公开密钥 ...

  3. iview--2

    安装iview 接下来进行配置 按照手册 https://www.iviewui.com/docs/guide/start 引入iView 打开我的项目,出现了这么多的错 解决这个问题的办法 如果你用 ...

  4. 【三小时学会Kubernetes!(二) 】Kubernetes 简介及Pod实践

    Kubernetes 简介 我向你保证我没有夸大其词,读完本文你会问“为什么我们不称它为 Supernetes?” Kubernetes 是什么? 从容器启动微服务后,我们有一个问题,让我们通过如下问 ...

  5. ECMAScript 5中对Array中新增了9个方法

    ECMAScript 5中对Array中新增了9个方法: 5个迭代方法(循环操作数组中的各个项):forEach(),map(),filter(),every()和some() 2个归并方法(迭代数组 ...

  6. 设计模式--单例模式C++实现

    单例模式C++实现 1描述: 单例模式,又称单件模式. 定义:确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 2具体类图描述: 2.1 StartUML内部生成模式类图 该类图由S ...

  7. opencv 图片降噪

    —— # -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , fr ...

  8. 037——VUE中表单控件处理之表单修饰符:lazy/number/trim

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Java复习1.基本知识

    Java语言概述 20131003 开头语: 开发领域,重要的编程语言基本都是C/C++,然后就是Java, C/C++就不用说了,另外掌握Java对你是有很大的帮助的,而且也会扩宽你的择业范围.同时 ...

  10. CF Round #456 (Div. 2)

    这时我第一次打CF 然后一看t1 哇好水 然后秒A了 看B 哇好像也很水 然后A了 看了C 不会... 然后去看D 似乎概率 然后推了一下,退出来了 然后看E 不会... 接着问了半个小时怎么hack ...