結果

問題 No.776 A Simple RMQ Problem
ユーザー pekempey
提出日時 2018-12-23 00:32:12
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 143 ms / 3,000 ms
コード長 2,148 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 24,832 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2024-09-25 10:20:54
合計ジャッジ時間 4,506 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0