結果

問題 No.776 A Simple RMQ Problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-25 02:25:43
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 512 ms / 3,000 ms
コード長 3,716 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 117,356 KB
実行使用メモリ 19,348 KB
最終ジャッジ日時 2023-09-03 23:05:50
合計ジャッジ時間 13,228 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 45 ms
7,188 KB
testcase_03 AC 166 ms
17,232 KB
testcase_04 AC 229 ms
7,204 KB
testcase_05 AC 365 ms
18,112 KB
testcase_06 AC 105 ms
11,740 KB
testcase_07 AC 231 ms
18,176 KB
testcase_08 AC 295 ms
10,200 KB
testcase_09 AC 89 ms
17,480 KB
testcase_10 AC 180 ms
10,532 KB
testcase_11 AC 332 ms
18,224 KB
testcase_12 AC 425 ms
17,456 KB
testcase_13 AC 429 ms
17,820 KB
testcase_14 AC 439 ms
18,328 KB
testcase_15 AC 405 ms
18,684 KB
testcase_16 AC 386 ms
17,840 KB
testcase_17 AC 512 ms
18,896 KB
testcase_18 AC 282 ms
19,348 KB
testcase_19 AC 402 ms
18,364 KB
testcase_20 AC 417 ms
18,500 KB
testcase_21 AC 400 ms
18,884 KB
testcase_22 AC 412 ms
17,668 KB
testcase_23 AC 443 ms
17,668 KB
testcase_24 AC 414 ms
18,312 KB
testcase_25 AC 93 ms
4,376 KB
testcase_26 AC 275 ms
18,648 KB
testcase_27 AC 247 ms
18,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


// -=- ==- -== === len
alias P = Tuple!(long, long, long, long, int);

P mul(in P a, in P b) {
  if (a[4] == 0) return b;
  if (b[4] == 0) return a;
  return P(max(a[0], b[0], a[2] + b[1]), max(a[1], a[3] + b[1]), max(b[2], a[2] + b[3]), a[3] + b[3], a[4] + b[4]);
}

class SegmentTree {
  int n;
  P[] prod;
  this(long[] ini) {
    const n_ = cast(int)(ini.length);
    for (n = 1; n < n_; n <<= 1) {}
    prod = new P[n << 1];
    foreach (i; 0 .. n_) {
      prod[n + i] = P(ini[i], ini[i], ini[i], ini[i], 1);
    }
    foreach_reverse (a; 0 .. n) {
      prod[a] = mul(prod[a << 1], prod[a << 1 | 1]);
    }
  }
  void update(int a, long val) {
    prod[a += n] = P(val, val, val, val, 1);
    for (; (a >>= 1); ) {
      prod[a] = mul(prod[a << 1], prod[a << 1 | 1]);
    }
  }
  P rangeProd(int a, int b) {
    P retL, retR;
    for (a += n, b += n; a <= b; a >>= 1, b >>= 1) {
      if ( a & 1) retL = mul(retL, prod[a++]);
      if (~b & 1) retR = mul(prod[b--], retR);
    }
    return mul(retL, retR);
  }
}

enum INF = 10L^^18;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const Q = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      auto seg = new SegmentTree(A);
      foreach (q; 0 .. Q) {
        const type = readToken();
        switch (type) {
          case "set": {
            const i = readInt() - 1;
            const x = readLong();
            seg.update(i, x);
          } break;
          case "max": {
            const l1 = readInt() - 1;
            const l2 = readInt();
            const r1 = readInt() - 1;
            const r2 = readInt();
            int[] xs = [l1, l2, r1, r2];
            xs = xs.sort.uniq.array;
            const len = cast(int)(xs.length) - 1;
            auto ps = new P[len];
            foreach (j; 0 .. len) {
              ps[j] = seg.rangeProd(xs[j], xs[j + 1] - 1);
            }
            debug {
              writeln("xs = ", xs);
              writeln("ps = ", ps);
            }
            long ans = -INF;
            foreach (i; 0 .. len) foreach (j; i .. len) {
              if (l1 <= xs[i] && xs[i + 1] <= l2 && r1 <= xs[j] && xs[j + 1] <= r2) {
                if (i == j) {
                  chmax(ans, ps[i][0]);
                } else {
                  chmax(ans, ps[i][2] + ps[i + 1 .. j].fold!"a + b[3]"(0L) + ps[j][1]);
                }
              }
            }
            writeln(ans);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0