結果

問題 No.776 A Simple RMQ Problem
ユーザー りあんりあん
提出日時 2018-12-07 17:08:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,031 ms / 3,000 ms
コード長 5,408 bytes
コンパイル時間 4,335 ms
コンパイル使用メモリ 104,364 KB
実行使用メモリ 58,352 KB
最終ジャッジ日時 2023-10-14 22:20:39
合計ジャッジ時間 24,885 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
21,940 KB
testcase_01 AC 69 ms
22,064 KB
testcase_02 AC 158 ms
33,424 KB
testcase_03 AC 423 ms
49,040 KB
testcase_04 AC 489 ms
42,100 KB
testcase_05 AC 847 ms
56,684 KB
testcase_06 AC 273 ms
42,236 KB
testcase_07 AC 555 ms
50,312 KB
testcase_08 AC 620 ms
42,176 KB
testcase_09 AC 274 ms
50,804 KB
testcase_10 AC 393 ms
42,384 KB
testcase_11 AC 678 ms
55,344 KB
testcase_12 AC 867 ms
55,012 KB
testcase_13 AC 889 ms
54,840 KB
testcase_14 AC 870 ms
56,844 KB
testcase_15 AC 863 ms
56,936 KB
testcase_16 AC 887 ms
54,868 KB
testcase_17 AC 1,031 ms
52,104 KB
testcase_18 AC 678 ms
57,580 KB
testcase_19 AC 955 ms
53,380 KB
testcase_20 AC 957 ms
56,100 KB
testcase_21 AC 965 ms
53,560 KB
testcase_22 AC 952 ms
54,184 KB
testcase_23 AC 970 ms
55,956 KB
testcase_24 AC 957 ms
53,616 KB
testcase_25 AC 181 ms
25,976 KB
testcase_26 AC 649 ms
57,996 KB
testcase_27 AC 594 ms
58,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.IO;
using System.Text;
using System.Diagnostics;

class Program
{
    static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
    static Scan sc = new Scan();
    const long LM = (long)1e18;
    static void Main()
    {
        int n, q;
        sc.Multi(out n, out q);
        var a = sc.LongArr;
        sg = new Segtree<node>(n, node.compose, node.ex);
        for (int i = 0; i < n; i++) sg.assign(i, new node(a[i]));
        sg.all_update();
        for (int _ = 0; _ < q; _++) {
            var inp = sc.StrArr;
            if (inp[0] == "set") {
                int i = int.Parse(inp[1]) - 1;
                long x = long.Parse(inp[2]);
                sg.update(i, new node(x));
            }
            else if (inp[0] == "max") {
                int l1 = int.Parse(inp[1]) - 1;
                int l2 = int.Parse(inp[2]);
                int r1 = int.Parse(inp[3]) - 1;
                int r2 = int.Parse(inp[4]);
                if (l1 >= l2 || r1 >= r2 || l1 >= r2) throw new Exception();
                l2 = Math.Min(l2, r2);
                r1 = Math.Max(l1, r1);
                if (l2 <= r1)
                    Prt(calc(l1, l2, r1, r2));
                else
                    Prt(Math.Max(calc(r1, l2), Math.Max(calc(l1, l2, l2, r2), calc(l1, r1, r1, r2))));
            }
            else throw new Exception();
        }
        sw.Flush();
    }
    static Segtree<node> sg;

    static long calc(int l, int r) => sg.run(l, r).inmax;

    // l1 < l2 <= r1 < r2
    static long calc(int l1, int l2, int r1, int r2) => sg.run(l1, l2).rmax + sg.run(r1, r2).lmax + (l2 == r1 ? 0 : sg.run(l2, r1).sum);

    class node {
        public readonly long lmax, rmax, inmax, sum;
        public node(long x) : this(x, x, x, x) {}
        public static readonly node ex = new node(-LM, -LM, -LM, 0);

        node(long lmax, long rmax, long inmax, long sum) {
            this.lmax = lmax;
            this.rmax = rmax;
            this.inmax = inmax;
            this.sum = sum;
        }
        public static node compose(node l, node r)
            => new node(Math.Max(l.lmax, l.sum + r.lmax),
                        Math.Max(r.rmax, l.rmax + r.sum),
                        Math.Max(Math.Max(l.inmax, r.inmax), l.rmax + r.lmax),
                        l.sum + r.sum);
    }

    static void Prt(string a) => sw.WriteLine(a);
    static void Prt<T>(IEnumerable<T> a) => Prt(string.Join(" ", a));
    static void Prt(params object[] a) => Prt(string.Join(" ", a));
}

class Scan
{
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public string Str => Console.ReadLine().Trim();
    public int[] IntArr => StrArr.Select(int.Parse).ToArray();
    public long[] LongArr => StrArr.Select(long.Parse).ToArray();
    public double[] DoubleArr => StrArr.Select(double.Parse).ToArray();
    public string[] StrArr => Str.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
    bool eq<T, U>() => typeof(T).Equals(typeof(U));
    T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
    T cv<T>(string s) => eq<T, int>()    ? ct<T, int>(int.Parse(s))
                       : eq<T, long>()   ? ct<T, long>(long.Parse(s))
                       : eq<T, double>() ? ct<T, double>(double.Parse(s))
                       : eq<T, char>()   ? ct<T, char>(s[0])
                                         : ct<T, string>(s);
    public void Multi<T>(out T a) => a = cv<T>(Str);
    public void Multi<T, U>(out T a, out U b)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); }
    public void Multi<T, U, V>(out T a, out U b, out V c)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); }
    public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); }
}

class Segtree<T> {
    int n, s, t;
    T[] tr;
    Func<T, T, T> f;
    T exnum;

    // expect : f(ex, ex) = ex
    public Segtree(int m, Func<T, T, T> f, T ex) {
        this.f = f; this.exnum = ex; n = 1;
        while (n < m) n <<= 1;
        tr = new T[(n << 1) - 1];
        for (int i = 0; i < tr.Length; i++) tr[i] = ex;
    }
    public Segtree(int m, T ini, Func<T, T, T> f, T ex) : this(m, f, ex) {
        for (int i = 0; i < m; ++i) assign(i, ini);
        all_update();
    }
    public Segtree(int m, IList<T> ini, Func<T, T, T> f, T ex) : this(m, f, ex) {
        for (int i = 0; i < m; ++i) assign(i, ini[i]);
        all_update();
    }
    public void assign(int j, T x) => tr[j + n - 1] = x;
    public void update(int j, T x) {
        assign(j, x);
        update(j);
    }
    public void update(int j) {
        int i = j + n - 1;
        while (i > 0) { i = i - 1 >> 1; tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); }
    }
    public void all_update() { for (int i = n - 2; i >= 0; i--) tr[i] = f(tr[i << 1 | 1], tr[i + 1 << 1]); }
    public T look(int i) => tr[i + n - 1];

    // [s, t)
    public T run(int s, int t) { this.s = s; this.t = t; return q(0, 0, n); }
    T q(int k, int l, int r) => r <= s || t <= l ? exnum : s <= l && r <= t ? tr[k]
                              : f(q(k << 1 | 1, l, l + r >> 1), q(k + 1 << 1, l + r >> 1, r));
}
0