結果
| 問題 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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));
}
りあん