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. Devops 到底是什么?

    Devops 到底是什么? 过去一年以来,一批来自欧美的.不墨守陈规的系统管理员和开发人员一直在谈论一个新概念:DevOps.DevOps就是开发(Development)和运维(Operations ...

  2. mysql字符编码的设置以及mysql中文乱码的解决方法

    查看字符编码 首先,将中文插入到数据库乱码是因为没有将数据库编码设置为支持中文的编码,mysql的默认编码是Latin1,不支持中文,应该设置为utf8查看自己的数据库编码是否已设置好,进入数据库,输 ...

  3. Linux系统用户及用户组管理

    目录一.新增/删除用户和用户组二.创建/修改密码三.用户身份切换--su和sudo 一.新增/删除用户和用户组1.用户组 命令 : groupadd 语法 : groupadd [-g GID] gr ...

  4. python+opencv+pyqt5控制摄像头在Qlabel上显示

    import cv2 import numpy as numpy from PIL import * import sys from PyQt5.QtWidgets import * from PyQ ...

  5. mongodb安装、运行

    1.下载安装: 切换到:/usr/local/ mkdir -p mongodb groupadd -g 800 mongodb useradd -u 801 -g mongodb mongodb c ...

  6. RocketMQ学习分享

    消息队列的流派 什么是 MQ Message Queue(MQ),消息队列中间件.很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的 ...

  7. IDEA的Tomcat配置Web的项目创建以及Servlet简单运行。

    相关软件: 1.IDEA编译器 2.JDK 3.Tomcat          (相关软件都可以到官网上下载,老表提示:不要下载最新版本因为不要做试验品)   IDEA的安装非常简单,找好安装的盘,n ...

  8. 2-3 sshd服务---暴力破解应对策略

      sshd服务暴力破解步骤 sshd暴力破解方法 防止暴力破解调优 1. 变更默认端口 2. 变更root用户 3. 日志监控-->防止暴力破解(fail2ban应用) fail2ban详解 ...

  9. JAVA常用数据结构API

    Quene

  10. UVA-1611 Crane (构造)

    题目大意:给一个1~n的序列,每次操作可以把长度为偶数的序列交换前一半和后一半的位置.求出将这个序列变成升序的步骤. 题目分析:构造求解. 代码如下: # include<iostream> ...