Skip to content

Instantly share code, notes, and snippets.

@ia7ck
Last active September 24, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ia7ck/5fb391f5e4cdae661df036c894930151 to your computer and use it in GitHub Desktop.
Save ia7ck/5fb391f5e4cdae661df036c894930151 to your computer and use it in GitHub Desktop.
ARC082-F Sandglass
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.typecons;
int x, k;
rd(x); rd(k);
auto r=readln.split.to!(int[]);
alias Q=Tuple!(int, "t", int, "a");
auto qs=new Q[](0);
foreach(t; r) qs~=Q(t, -1);
int q; rd(q);
foreach(_; 0..q){
int t, a; rd(t, a);
qs~=Q(t, a);
}
sort(qs);
int p=0, l=0, u=x;
int d=0, f=-1;
foreach(e; qs){
int dd=(e.t-p)*f;
u=min(x, max(0, u+dd));
l=max(0, min(x, l+dd));
d+=dd; p=e.t;
if(e.a==-1){// reverse
f*=-1;
}else{
int y=e.a+d;
writeln(min(u, max(l, y)));
}
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment