Exemplo n.º 1
0
        public static void Solve()
        {
            var(N, K, D) = Scanner.Scan <int, int, int>();
            var A = Scanner.ScanEnumerable <int>().ToArray();

            var answer = new int[K];
            var st     = new SegmentTree <Monoid>(A.Select((x, i) => new Monoid(i, x)), new Oracle());
            var l      = 0;

            for (var k = 0; k < K; k++)
            {
                var r = N - (K - k - 1) * D;
                if (r <= l)
                {
                    Console.WriteLine(-1);
                    return;
                }

                var min = st.Query(l, r);
                answer[k] = min.A;
                l         = min.Idx + D;
            }

            Console.WriteLine(string.Join(" ", answer));
        }
Exemplo n.º 2
0
        public static void Solve()
        {
            var(W, N) = Scanner.Scan <int, int>();
            const long inf = (long)1e18;
            var        dp  = new long[N + 1, W + 1];

            for (var i = 0; i <= N; i++)
            {
                for (var j = 0; j <= W; j++)
                {
                    dp[i, j] = -inf;
                }
            }
            dp[0, 0] = 0;

            var seg    = new SegmentTree <long> [N + 1];
            var oracle = new Oracle();

            seg[0] = new SegmentTree <long>(W + 1, oracle);
            seg[0].Set(0, 0);

            for (var i = 0; i < N; i++)
            {
                var(l, r, v) = Scanner.Scan <int, int, long>();
                l--;
                seg[i + 1] = new SegmentTree <long>(W + 1, oracle);

                for (var j = 0; j <= W; j++)
                {
                    dp[i + 1, j] = dp[i, j];
                    if (j - l >= 0)
                    {
                        var ll  = Math.Max(0, j - r);
                        var rr  = Math.Max(0, j - l);
                        var max = seg[i].Query(ll, rr);
                        if (max != -inf)
                        {
                            dp[i + 1, j] = Math.Max(dp[i + 1, j], max + v);
                        }
                    }

                    seg[i + 1].Set(j, dp[i + 1, j]);
                }
            }

            var answer = dp[N, W];

            if (answer == -inf)
            {
                answer = -1;
            }
            Console.WriteLine(answer);
        }
Exemplo n.º 3
0
        public static void Solve()
        {
            var(N, K) = Scanner.Scan <int, int>();
            const int max = 3 * (int)1e5 + 5;
            var       st  = new SegmentTree <int>(max, (a, b) => Math.Max(a, b), 0);

            for (var i = 0; i < N; i++)
            {
                var A = Scanner.Scan <int>();
                var l = Math.Max(0, A - K);
                var r = Math.Min(A + K, max - 1) + 1;
                st.Set(A, st.Query(l, r) + 1);
            }

            Console.WriteLine(st.Query(0, max));
        }
Exemplo n.º 4
0
        public static void Solve()
        {
            var(N, K) = Scanner.Scan <int, int>();
            var P     = Scanner.ScanEnumerable <int>().ToArray();
            var minst = new SegmentTree <int>(P, new MinOracle());
            var maxst = new SegmentTree <int>(P, new MaxOracle());
            var inc   = new int[N];

            inc[0] = 1;
            for (var i = 1; i < N; i++)
            {
                if (P[i] > P[i - 1])
                {
                    inc[i] = inc[i - 1] + 1;
                }
                else
                {
                    inc[i] = 1;
                }
            }

            var answer = N - K + 1;
            var count  = 0;

            if (inc[K - 1] >= K)
            {
                count++;
            }
            for (var i = 0; i + K < N; i++)
            {
                var min = minst.Query(i, i + K);
                var max = maxst.Query(i + 1, i + K + 1);
                if (P[i] == min && P[i + K] == max)
                {
                    answer--;
                }
                else if (inc[i + K] >= K)
                {
                    count++;
                }
            }

            answer -= Math.Max(0, count - 1);

            Console.WriteLine(answer);
        }
Exemplo n.º 5
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var A  = Scanner.ScanEnumerable <int>().ToArray();
            var st = new SegmentTree <int>(A, new Oracle());

            while (Q-- > 0)
            {
                var(T, X, Y) = Scanner.Scan <int, int, int>();
                X--;
                if (T == 1)
                {
                    st.Set(X, st.Get(X) ^ Y);
                }
                else
                {
                    Console.WriteLine(st.Query(X, Y));
                }
            }
        }
Exemplo n.º 6
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var S = Scanner.Scan <string>();

            int Score(char c) => c switch
            {
                '(' => 1,
                ')' => - 1,
                _ => default
            };

            var seg = new SegmentTree <Monoid>(N, new Oracle());

            for (var i = 0; i < N; i++)
            {
                var score = Score(S[i]);
                seg.Set(i, new Monoid(score, score));
            }

            while (Q-- > 0)
            {
                var(q, l, r) = Scanner.Scan <int, int, int>();
                l--;
                if (q == 1)
                {
                    r--;
                    var(lv, rv) = (seg.Get(l), seg.Get(r));
                    seg.Set(l, rv);
                    seg.Set(r, lv);
                }
                else
                {
                    var x      = seg.Query(l, r);
                    var answer = x.Value == 0 && x.Min >= 0;
                    Console.WriteLine(answer ? "Yes" : "No");
                }
            }
        }