結果

問題 No.776 A Simple RMQ Problem
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-25 02:25:43
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 497 ms / 3,000 ms
コード長 3,716 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 131,072 KB
実行使用メモリ 17,184 KB
最終ジャッジ日時 2024-06-13 03:41:39
合計ジャッジ時間 12,589 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 41 ms
6,912 KB
testcase_03 AC 164 ms
16,344 KB
testcase_04 AC 215 ms
7,032 KB
testcase_05 AC 357 ms
16,416 KB
testcase_06 AC 101 ms
9,808 KB
testcase_07 AC 221 ms
16,628 KB
testcase_08 AC 266 ms
9,884 KB
testcase_09 AC 90 ms
16,836 KB
testcase_10 AC 152 ms
10,092 KB
testcase_11 AC 278 ms
16,920 KB
testcase_12 AC 359 ms
17,136 KB
testcase_13 AC 377 ms
17,136 KB
testcase_14 AC 368 ms
17,136 KB
testcase_15 AC 368 ms
17,136 KB
testcase_16 AC 374 ms
17,008 KB
testcase_17 AC 497 ms
17,136 KB
testcase_18 AC 275 ms
17,136 KB
testcase_19 AC 397 ms
17,088 KB
testcase_20 AC 406 ms
17,168 KB
testcase_21 AC 396 ms
17,088 KB
testcase_22 AC 405 ms
17,184 KB
testcase_23 AC 424 ms
17,132 KB
testcase_24 AC 398 ms
16,904 KB
testcase_25 AC 87 ms
5,376 KB
testcase_26 AC 265 ms
17,132 KB
testcase_27 AC 237 ms
17,136 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