Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created April 25, 2020 02:48
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 theoremoon/dabbec8b8d11daf466ba67e0bdd111ff to your computer and use it in GitHub Desktop.
Save theoremoon/dabbec8b8d11daf466ba67e0bdd111ff to your computer and use it in GitHub Desktop.
import std.stdio, std.string, std.algorithm, std.array, std.range, std.conv,
std.typecons, std.math, std.container, std.format, std.numeric;
alias T = Tuple!(long, "value", long, "time");
class Segtree
{
private:
long size;
T[] xs;
public:
this(long size)
{
long i = 1;
while (i <= size)
{
i *= 2;
}
this.size = size;
this.xs = new T[](i * 2);
}
this(long size, T value)
{
this(size);
this.xs.fill(value);
}
void update(long l, long r, T x)
{
l += this.size;
r += this.size;
while (l < r)
{
if (l % 2 == 1)
{
this.xs[l] = x;
l++;
}
l /= 2;
if (r % 2 == 1)
{
this.xs[r - 1] = x;
r--;
}
r /= 2;
}
}
T find(long i)
{
i += this.size;
T ans = this.xs[i];
while (true)
{
i /= 2;
if (i == 0)
{
break;
}
if (ans.time < this.xs[i].time)
{
ans = this.xs[i];
}
}
return ans;
}
}
void main(string[] args)
{
long n, q;
readf("%d %d\n", &n, &q);
auto seg = new Segtree(n, T((1 << 31) - 1, -1));
foreach (i; 0 .. q)
{
long cmd, s, t, x;
readf("%d ", &cmd);
if (cmd == 0)
{
readf("%d %d %d\n", &s, &t, &x);
seg.update(s, t + 1, T(x, i));
}
else
{
readf("%d\n", &x);
auto val = seg.find(x);
writeln(val.value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment