Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created March 31, 2020 07:43
Show Gist options
  • Select an option

  • Save theoremoon/2a31822389861295a5108e74a2aa57fb to your computer and use it in GitHub Desktop.

Select an option

Save theoremoon/2a31822389861295a5108e74a2aa57fb 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 initial)
{
this(size);
xs.fill(initial);
}
void update(long l, long r, T t)
{
l += this.size;
r += this.size;
while (l < r)
{
if (l % 2 == 1)
{
xs[l] = t;
l++;
}
l /= 2;
if (r % 2 == 1)
{
xs[r - 1] = t;
r--;
}
r /= 2;
}
}
long find(long i)
{
i += this.size;
T ans = xs[i];
while (true)
{
i /= 2;
if (i == 0)
{
break;
}
if (xs[i].time > ans.time)
{
ans = xs[i];
}
}
return ans.value;
}
}
void main(string[] args)
{
long n, q, com, s, t, x, index;
readf("%d %d\n", &n, &q);
auto seg = new Segtree(n, T((1 << 31) - 1, 0));
foreach (i; 0 .. q)
{
readf("%d ", &com);
if (com == 0)
{
readf("%d %d %d\n", &s, &t, &x);
seg.update(s, t + 1, T(x, i + 1));
}
if (com == 1)
{
readf("%d\n", &index);
writeln(seg.find(index));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment