結果
| 問題 |
No.776 A Simple RMQ Problem
|
| コンテスト | |
| ユーザー |
goodbaton
|
| 提出日時 | 2018-12-25 11:04:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 305 ms / 3,000 ms |
| コード長 | 3,243 bytes |
| コンパイル時間 | 1,258 ms |
| コンパイル使用メモリ | 108,556 KB |
| 実行使用メモリ | 12,032 KB |
| 最終ジャッジ日時 | 2024-10-01 13:15:36 |
| 合計ジャッジ時間 | 9,268 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 |
ソースコード
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010
/* Starry Sky Tree */
//0-index
struct StarrySkyTree{
typedef pair<pair<ll,ll>, ll> Type;
int segn2;
vector<Type> data; // {{min, max}, diff}
vector<ll> s_data;
StarrySkyTree(int n)
{
for(segn2=1; segn2<n; segn2*=2);
data.assign(segn2*2, {{0, 0}, -LLINF});
s_data.assign(segn2*2, 0);
}
Type merge(Type a, Type b) {
Type res;
res.first.first = min(a.first.first, b.first.first);
res.first.second = max(a.first.second, b.first.second);
res.second = max({a.second, b.second, b.first.second - a.first.first});
return res;
}
Type add(Type a, ll x) {
a.first.first += x;
a.first.second += x;
return a;
}
//get value of [a,b)
Type query(int a, int b, int l = 0, int r = -1, int k = 0){
//debug(a); debug(b);
if(r == -1) r = segn2;
if(r <= a || b <= l) return {{LLINF, -LLINF}, -LLINF}; //大きさに注意
if(a <= l && r <= b) return add(data[k], s_data[k]);
return add(merge(query(a, b, l, (l+r)/2, k*2+1), query(a, b, (l+r)/2 , r, k*2+2)), s_data[k]);
}
//add x to [a,b)
Type add(int a, int b, ll x, int l = 0, int r = -1, int k = 0){
if(r == -1) r = segn2;
if(a <= l && r <= b)
s_data[k] += x;
else if(a < r && l < b)
data[k] = merge(add(a, b, x, l, (l+r)/2, k*2+1), add(a, b, x, (l+r)/2, r, k*2+2));
return add(data[k], s_data[k]);
}
};
int main(){
int n, q;
int a[SIZE];
scanf("%d%d", &n, &q);
StarrySkyTree seg(n+1);
seg.add(n+1, INF, -LLINF);
for(int i=0;i<n;i++){
scanf("%d", a+i);
seg.add(i+1, n+1, a[i]);
}
for(int i=0;i<q;i++){
char s[5];
scanf("%s", s);
if(s[0] == 's'){
//set
int k, x;
scanf("%d%d", &k, &x);
seg.add(k, n+1, x - a[k-1]);
a[k-1] = x;
}else{
//max
int l1, l2, r1, r2;
scanf("%d%d%d%d", &l1, &l2, &r1, &r2);
l1--; l2--;
ll ans = -LLINF;
auto res1a = seg.query(l1, min(l2+1, r1));
auto res1b = seg.query(r1, r2+1);
ans = max(ans, res1b.first.second - res1a.first.first);
debug(res1a.first.first);
debug(res1b.first.second);
auto res2a = seg.query(l1, l2+1);
auto res2b = seg.query(max(r1, l2+1), r2+1);
ans = max(ans, res2b.first.second - res2a.first.first);
debug(res2a.first.first);
debug(res2b.first.second);
if(r1 <= l2){
auto res3a = seg.query(max(r1-1, l1), min(r2, l2+1)+1);
debug(res3a.second);
ans = max(ans, res3a.second);
}
printf("%lld\n", ans);
}
}
return 0;
}
goodbaton