結果

問題 No.776 A Simple RMQ Problem
ユーザー りあんりあん
提出日時 2018-12-07 17:08:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 954 ms / 3,000 ms
コード長 5,408 bytes
コンパイル時間 2,710 ms
コンパイル使用メモリ 110,592 KB
実行使用メモリ 53,268 KB
最終ジャッジ日時 2024-09-16 16:00:01
合計ジャッジ時間 19,918 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
19,200 KB
testcase_01 AC 28 ms
19,072 KB
testcase_02 AC 110 ms
31,232 KB
testcase_03 AC 347 ms
44,288 KB
testcase_04 AC 414 ms
39,424 KB
testcase_05 AC 731 ms
52,104 KB
testcase_06 AC 216 ms
37,504 KB
testcase_07 AC 468 ms
47,844 KB
testcase_08 AC 543 ms
39,552 KB
testcase_09 AC 218 ms
46,080 KB
testcase_10 AC 330 ms
39,680 KB
testcase_11 AC 625 ms
51,712 KB
testcase_12 AC 776 ms
51,708 KB
testcase_13 AC 798 ms
51,816 KB
testcase_14 AC 793 ms
51,692 KB
testcase_15 AC 799 ms
51,556 KB
testcase_16 AC 806 ms
51,560 KB
testcase_17 AC 954 ms
47,360 KB
testcase_18 AC 610 ms
52,348 KB
testcase_19 AC 860 ms
51,024 KB
testcase_20 AC 845 ms
51,136 KB
testcase_21 AC 872 ms
51,164 KB
testcase_22 AC 859 ms
51,168 KB
testcase_23 AC 837 ms
51,020 KB
testcase_24 AC 825 ms
51,040 KB
testcase_25 AC 148 ms
23,424 KB
testcase_26 AC 576 ms
52,732 KB
testcase_27 AC 511 ms
53,268 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