Created
April 2, 2020 00:13
-
-
Save theoremoon/2310cd79f30ddb68637e935c2251f434 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 SegmentTree | |
| { | |
| private: | |
| T[] xs; | |
| long size; | |
| 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 init) | |
| { | |
| this(size); | |
| this.xs.fill(init); | |
| } | |
| void update(long l, long r, T x) | |
| { | |
| l += this.size; | |
| r += this.size; | |
| while (l < r) | |
| { | |
| if (l % 2 == 1) | |
| { | |
| xs[l] = x; | |
| l++; | |
| } | |
| l /= 2; | |
| if (r % 2 == 1) | |
| { | |
| xs[r - 1] = x; | |
| r--; | |
| } | |
| r /= 2; | |
| } | |
| } | |
| T find(long i) | |
| { | |
| i += this.size; | |
| auto ans = xs[i]; | |
| while (true) | |
| { | |
| i /= 2; | |
| if (i == 0) | |
| { | |
| return ans; | |
| } | |
| if (xs[i].time > ans.time) | |
| { | |
| ans = xs[i]; | |
| } | |
| } | |
| } | |
| } | |
| void main(string[] args) | |
| { | |
| long n, q; | |
| readf("%d %d\n", &n, &q); | |
| auto seg = new SegmentTree(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); | |
| writeln(seg.find(x).value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment