public void ArgumentOutOfRangeInApplyTest(int l, int r)
        {
            var lst = new LazySegmentTree <int, int>(10, new SimpleOracle());

            if (l < 0)
            {
                Assert.Throws <ArgumentOutOfRangeException>(() => lst.Apply(l, 1));
            }
            Assert.Throws <ArgumentOutOfRangeException>(() => lst.Apply(l, r, 1));
        }
        public void QueryNaiveTest([Range(1, 30)] int n)
        {
            for (var ph = 0; ph < 10; ph++)
            {
                var lst         = new LazySegmentTree <Monoid, Map>(n, new Oracle());
                var timeManager = new TimeManager(n);
                for (var i = 0; i < n; i++)
                {
                    lst.Set(i, new Monoid(i, i + 1, -1));
                }
                var now = 0;
                for (var q = 0; q < 3000; q++)
                {
                    var ty = Utilities.RandomInteger(0, 3);
                    var(l, r) = Utilities.RandomPair(0, n);
                    switch (ty)
                    {
                    case 0:
                    {
                        var result = lst.Query(l, r);
                        Assert.That(result.L, Is.EqualTo(l));
                        Assert.That(result.R, Is.EqualTo(r));
                        Assert.That(result.Time, Is.EqualTo(timeManager.Query(l, r)));
                        break;
                    }

                    case 1:
                    {
                        var result = lst.Get(l);
                        Assert.That(result.L, Is.EqualTo(l));
                        Assert.That(result.L + 1, Is.EqualTo(l + 1));
                        Assert.That(result.Time, Is.EqualTo(timeManager.Query(l, l + 1)));
                        break;
                    }

                    case 2:
                        now++;
                        lst.Apply(l, r, new Map(now));
                        timeManager.Action(l, r, now);
                        break;

                    case 3:
                        now++;
                        lst.Apply(l, new Map(now));
                        timeManager.Action(l, l + 1, now);
                        break;

                    default:
                        throw new InvalidOperationException();
                    }
                }
            }
        }
        public void SimpleUsageTest()
        {
            var n   = 10;
            var lst = new LazySegmentTree <int, int>(new int[n], new SimpleOracle());

            Assert.That(lst.Length, Is.EqualTo(n));
            Assert.That(lst.QueryToAll(), Is.Zero);
            lst.Apply(0, 3, 5);
            Assert.That(lst.QueryToAll(), Is.EqualTo(5));
            lst.Apply(2, -10);
            Assert.That(lst.Query(2, 3), Is.EqualTo(-5));
            Assert.That(lst.Query(2, 4), Is.Zero);
        }
예제 #4
0
        public static void Solve()
        {
            var NQ = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();

            var(N, Q) = (NQ[0], NQ[1]);
            var A = Console.ReadLine().Split(" ").Select(int.Parse)
                    .Select(x => x == 0 ? new S(1, 0, 0) : new S(0, 1, 0)).ToArray();

            var lst = new LazySegmentTree <S, F>(A, new Oracle());

            for (var i = 0; i < Q; i++)
            {
                var TLR = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
                var(T, L, R) = (TLR[0], TLR[1], TLR[2]);
                L--;
                if (T == 1)
                {
                    lst.Apply(L, R, new F(true));
                }
                else
                {
                    Console.WriteLine(lst.Query(L, R).Inversion);
                }
            }
        }
예제 #5
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();

            S Operation(S a, S b) => new S(a.X * b.W + b.X, a.W * b.W);

            var idData = new S(0, 1);
            var inv9   = ModuloInteger.Inverse(9);

            S Mapping(int f, S x) => f == 0 ? x : new S((x.W - 1) * inv9 * f, x.W);
            int Composition(int f, int g) => f == 0 ? g : f;

            var idInt = 0;

            var lst = new LazySegmentTree <S, int>(N, Operation, idData, Mapping, Composition, idInt);

            for (var i = 0; i < N; i++)
            {
                lst.Set(i, new S(1, 10));
            }
            for (var i = 0; i < Q; i++)
            {
                var(L, R, D) = Scanner.Scan <int, int, int>();
                lst.Apply(L - 1, R, D);
                Console.WriteLine(lst.QueryToAll().X);
            }
        }
예제 #6
0
        public static void Solve()
        {
            var(W, N) = Scanner.Scan <int, int>();
            var lst = new LazySegmentTree <long, long>(W, new Oracle());

            for (var i = 0; i < N; i++)
            {
                var(l, r) = Scanner.Scan <int, int>();
                l--;
                var answer = lst.Query(l, r) + 1;
                lst.Apply(l, r, answer);
                Console.WriteLine(answer);
            }
        }
예제 #7
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var st = new LazySegmentTree <Monoid, long>(N, new Oracle());

            for (var i = 0; i < M; i++)
            {
                var(t, l, r) = Scanner.Scan <int, int, int>();
                st.Apply(l - 1, r, t);
            }

            var answer = st.QueryToAll().Value;

            Console.WriteLine(answer);
        }
예제 #8
0
        public override void Solve(IOManager io)
        {
            var n = io.ReadInt();
            var a = io.ReadIntArray(n);
            var b = io.ReadIntArray(n);
            var c = io.ReadIntArray(n);

            var segtree = new LazySegmentTree <MaxLong, Adder>(n + 1);

            segtree[0] = new MaxLong(b[0]);

            for (int i = 0; i < a.Length; i++)
            {
                segtree.Apply(0, i + 1, new Adder(a[i]));
                var max = segtree.Query(0, i + 1);
            }
        }
예제 #9
0
        public static void Solve()
        {
            var NQ = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();

            var(N, Q) = (NQ[0], NQ[1]);
            var A = Console.ReadLine().Split(" ").Select(x => new S(int.Parse(x), 1)).ToArray();

            var lst = new LazySegmentTree <S, F>(A, new Oracle());

            for (var i = 0; i < Q; i++)
            {
                var q = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
                if (q[0] == 0)
                {
                    lst.Apply(q[1], q[2], new F(q[3], q[4]));
                }
                else
                {
                    Console.WriteLine(lst.Query(q[1], q[2]).A);
                }
            }
        }
        public void MaxLeftTest([Range(1, 30)] int n)
        {
            for (var ph = 0; ph < 10; ph++)
            {
                var lst         = new LazySegmentTree <Monoid, Map>(n, new Oracle());
                var timeManager = new TimeManager(n);
                for (var i = 0; i < n; i++)
                {
                    lst.Set(i, new Monoid(i, i + 1, -1));
                }
                var now = 0;
                for (var q = 0; q < 1000; q++)
                {
                    var ty = Utilities.RandomInteger(0, 2);
                    var(l, r) = Utilities.RandomPair(0, n);
                    if (ty == 0)
                    {
                        bool F(Monoid s)
                        {
                            if (s.L == -1)
                            {
                                return(true);
                            }
                            return(l <= s.L);
                        }

                        Assert.That(lst.MinLeft(r, F), Is.EqualTo(l));
                    }
                    else
                    {
                        now++;
                        lst.Apply(l, r, new Map(now));
                        timeManager.Action(l, r, now);
                    }
                }
            }
        }
예제 #11
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var st = new LazySegmentTree <Monoid, int>(N * 2, new Oracle());

            while (Q-- > 0)
            {
                var(t, k) = Scanner.Scan <int, int>();
                if (t == 1)
                {
                    k--;
                    var x = st.Get(k).Value;
                    if (x % 2 == 1)
                    {
                        k = N * 2 - 1 - k;
                    }
                    Console.WriteLine(k + 1);
                }
                else
                {
                    st.Apply(N - k, N + k, 1);
                }
            }
        }