Submission #834926

#TimeUsernameProblemLanguageResultExecution timeMemory
834926maomao90Triple Jump (JOI19_jumps)C++17
100 / 100
1100 ms121372 KiB
// Hallelujah, praise the one who set me free // Hallelujah, death has lost its grip on me // You have broken every chain, There's salvation in your name // Jesus Christ, my living hope #include <bits/stdc++.h> using namespace std; #define REP(i, s, e) for (int i = (s); i < (e); i++) #define RREP(i, s, e) for (int i = (s); i >= (e); i--) template <class T> inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;} template <class T> inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;} typedef unsigned long long ull; typedef long long ll; typedef long double ld; #define FI first #define SE second typedef pair<int, int> ii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> iii; #define ALL(_a) _a.begin(), _a.end() #define SZ(_a) (int) _a.size() #define pb push_back typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ii> vii; typedef vector<iii> viii; #ifndef DEBUG #define cerr if (0) cerr #endif const int INF = 1000000005; const ll LINF = 1000000000000000005ll; const int MAXN = 500005; int n; int a[MAXN]; int q; vii qry[MAXN]; vi ev[MAXN]; ll ans[MAXN]; #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1 struct Node { ll mn, smn, mxa, res, lz; } st[MAXN * 4]; void init(int u = 1, int lo = 1, int hi = n) { st[u].mn = -LINF; st[u].smn = LINF; st[u].res = 0; st[u].lz = -LINF; if (lo == hi) { st[u].mxa = a[lo]; return; } MLR; init(lc, lo, mid); init(rc, mid + 1, hi); st[u].mxa = max(st[lc].mxa, st[rc].mxa); } void apply(ll x, int u, int lo, int hi) { if (x <= st[u].mn) { return; } assert(x <= st[u].smn); st[u].mn = x; mxto(st[u].res, st[u].mn + st[u].mxa); mxto(st[u].lz, x); } void propo(int u, int lo, int hi) { if (st[u].lz == -LINF) { return; } MLR; apply(st[u].lz, lc, lo, mid); apply(st[u].lz, rc, mid + 1, hi); st[u].lz = -LINF; } void updmx(int s, int e, ll x, int u = 1, int lo = 1, int hi = n) { if (s > e || x <= st[u].mn) { return; } if (lo >= s && hi <= e && x <= st[u].smn) { apply(x, u, lo, hi); return; } assert(lo != hi); propo(u, lo, hi); MLR; if (s <= mid) { updmx(s, e, x, lc, lo, mid); } if (e > mid) { updmx(s, e, x, rc, mid + 1, hi); } st[u].res = max(st[lc].res, st[rc].res); if (st[lc].mn == st[rc].mn) { st[u].mn = st[lc].mn; st[u].mxa = max(st[lc].mxa, st[rc].mxa); st[u].smn = min(st[lc].smn, st[rc].smn); } else if (st[lc].mn < st[rc].mn) { st[u].mn = st[lc].mn; st[u].mxa = st[lc].mxa; st[u].smn = min(st[lc].smn, st[rc].mn); } else { st[u].mn = st[rc].mn; st[u].mxa = st[rc].mxa; st[u].smn = min(st[rc].smn, st[lc].mn); } } ll qmx(int s, int e, int u = 1, int lo = 1, int hi = n) { if (lo >= s && hi <= e) { return st[u].res; } MLR; propo(u, lo, hi); ll res = 0; if (s <= mid) { mxto(res, qmx(s, e, lc, lo, mid)); } if (e > mid) { mxto(res, qmx(s, e, rc, mid + 1, hi)); } return res; } /* void updmx(int s, int e, ll x) { REP (i, s, e + 1) { mxto(mx[i], x); } } ll qmx(int s, int e) { ll res = 0; REP (i, s, e + 1) { mxto(res, a[i] + mx[i]); } return res; } */ int main() { #ifndef DEBUG ios::sync_with_stdio(0), cin.tie(0); #endif cin >> n; REP (i, 1, n + 1) { cin >> a[i]; } vi stk; REP (j, 1, n + 1) { while (!stk.empty() && a[stk.back()] < a[j]) { ev[stk.back()].pb(j); stk.pop_back(); } if (!stk.empty()) { ev[stk.back()].pb(j); } stk.pb(j); } stk.clear(); RREP (j, n, 1) { while (!stk.empty() && a[stk.back()] < a[j]) { ev[j].pb(stk.back()); stk.pop_back(); } if (!stk.empty()) { ev[j].pb(stk.back()); } stk.pb(j); } cin >> q; REP (i, 1, q + 1) { int l, r; cin >> l >> r; qry[l].pb({r, i}); } init(); RREP (l, n, 1) { for (int r : ev[l]) { // c - r = r - l // c = 2 * r - l updmx(2 * r - l, n, a[l] + a[r]); } for (auto [r, i] : qry[l]) { ans[i] = qmx(l, r); } } REP (i, 1, q + 1) { cout << ans[i] << '\n'; } return 0; }

Compilation message (stderr)

jumps.cpp: In function 'void init(int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
jumps.cpp:60:5: note: in expansion of macro 'MLR'
   60 |     MLR;
      |     ^~~
jumps.cpp: In function 'void propo(int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
jumps.cpp:78:5: note: in expansion of macro 'MLR'
   78 |     MLR;
      |     ^~~
jumps.cpp: In function 'void updmx(int, int, ll, int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
jumps.cpp:93:5: note: in expansion of macro 'MLR'
   93 |     MLR;
      |     ^~~
jumps.cpp: In function 'll qmx(int, int, int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
      |                       ~~~^~~~
jumps.cpp:119:5: note: in expansion of macro 'MLR'
  119 |     MLR;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...