結果

問題 No.776 A Simple RMQ Problem
ユーザー pekempeypekempey
提出日時 2018-12-23 00:32:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 2,148 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 24,828 KB
実行使用メモリ 8,784 KB
最終ジャッジ日時 2023-10-26 02:17:04
合計ジャッジ時間 4,538 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,796 KB
testcase_01 AC 1 ms
5,796 KB
testcase_02 AC 17 ms
8,028 KB
testcase_03 AC 53 ms
8,284 KB
testcase_04 AC 61 ms
8,056 KB
testcase_05 AC 100 ms
8,296 KB
testcase_06 AC 31 ms
8,100 KB
testcase_07 AC 67 ms
8,324 KB
testcase_08 AC 77 ms
8,128 KB
testcase_09 AC 35 ms
8,452 KB
testcase_10 AC 46 ms
8,152 KB
testcase_11 AC 84 ms
8,604 KB
testcase_12 AC 125 ms
8,784 KB
testcase_13 AC 105 ms
8,784 KB
testcase_14 AC 106 ms
8,784 KB
testcase_15 AC 105 ms
8,784 KB
testcase_16 AC 106 ms
8,784 KB
testcase_17 AC 135 ms
8,784 KB
testcase_18 AC 82 ms
8,784 KB
testcase_19 AC 121 ms
8,784 KB
testcase_20 AC 117 ms
8,784 KB
testcase_21 AC 118 ms
8,784 KB
testcase_22 AC 118 ms
8,784 KB
testcase_23 AC 119 ms
8,784 KB
testcase_24 AC 119 ms
8,784 KB
testcase_25 AC 39 ms
5,796 KB
testcase_26 AC 79 ms
8,784 KB
testcase_27 AC 71 ms
8,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:64:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:67:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   67 |         scanf("%d", &a);
      |         ~~~~~^~~~~~~~~~
main.cpp:72:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
main.cpp:75:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |             scanf("%d %d %d %d", &a, &b, &c, &d);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:91:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |             scanf("%d %d", &k, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>

#define N 131072

long long min(long long x, long long y) {
    return x < y ? x : y;
}

long long max(long long x, long long y) {
    return x > y ? x : y;
}

struct F {
    long long ans;
    long long sum;
    long long rmax;
    long long lmax;
} dat[N * 2];

struct F merge(F a, F b) {
    struct F res;
    res.sum = a.sum + b.sum;
    res.lmax = max(a.lmax, a.sum + b.lmax);
    res.rmax = max(b.rmax, b.sum + a.rmax);
    res.ans = max(max(a.ans, b.ans), a.rmax + b.lmax);
    return res;
}

void update(int k, int v) {
    k += N;
    dat[k].sum = v;
    dat[k].lmax = v;
    dat[k].rmax = v;
    dat[k].ans = v;
    while (k > 1) {
        k >>= 1;
        dat[k] = merge(dat[k * 2], dat[k * 2 + 1]);
    }
}

struct F query(int l, int r) {
    l += N;
    r += N;
    struct F ansl, ansr;
    ansl.sum = 0;
    ansl.lmax = -2e18;
    ansl.rmax = -2e18;
    ansl.ans = -2e18;
    ansr.sum = 0;
    ansr.lmax = -2e18;
    ansr.rmax = -2e18;
    ansr.ans = -2e18;
    while (l < r) {
        if (l & 1) ansl = merge(ansl, dat[l++]);
        if (r & 1) ansr = merge(dat[--r], ansr);
        l >>= 1;
        r >>= 1;
    }
    return merge(ansl, ansr);
}

int main(void) {
    int n, q;
    scanf("%d %d", &n, &q);
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        update(i, a);
    }
    while (q--) {
        char s[4];
        scanf("%s", s);
        if (s[0] == 'm') {
            int a, b, c, d;
            scanf("%d %d %d %d", &a, &b, &c, &d);
            a--;
            c--;
            int l = max(a, c);
            int r = min(b, d);
            long long ans = -2e18;
            if (l < r) {
                ans = max(ans, query(a, l).rmax + query(l, d).lmax);
                ans = max(ans, query(a, r).rmax + query(r, d).lmax);
                ans = max(ans, query(l, r).ans);
            } else {
                ans = max(ans, query(a, b).rmax + query(c, d).lmax + query(b, c).sum);
            }
            printf("%lld\n", ans);
        } else {
            int k, v;
            scanf("%d %d", &k, &v);
            update(k - 1, v);
        }
    }
    return 0;
}

0