Submission #712437

#TimeUsernameProblemLanguageResultExecution timeMemory
712437rainboyTriple Jump (JOI19_jumps)C11
100 / 100
1741 ms125212 KiB
#include <stdio.h> #define N 500000 #define Q 500000 #define M (N * 2 + Q) #define N_ (1 << 21) /* N_ = pow2(ceil(log2(M))) */ #define INF 0x3f3f3f3f int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int ll[M], rr[M], ww[M], xx[M], idx[M], m; int compare_l(int h1, int h2) { return ll[h1] != ll[h2] ? ll[h1] - ll[h2] : h1 - h2; } int compare_r(int h1, int h2) { return rr[h1] - rr[h2]; } int (*compare)(int, int); void sort(int *hh, int l, int r) { while (l < r) { int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp; while (j < k) { int c = compare(hh[j], h); if (c == 0) j++; else if (c < 0) { tmp = hh[i], hh[i] = hh[j], hh[j] = tmp; i++, j++; } else { k--; tmp = hh[j], hh[j] = hh[k], hh[k] = tmp; } } sort(hh, l, i); l = k; } } int st1[N_ * 2], st2[N_ * 2], mn1[N_ * 2], mn2[N_ * 2], lz[N_], h_, n_; void build(int m) { int i; h_ = 0; while (1 << h_ < m) h_++; n_ = 1 << h_; for (i = 1; i < n_ * 2; i++) st1[i] = -1, st2[i] = -1, mn1[i] = 0, mn2[i] = INF; } void put(int i, int x) { if (st1[i] != -1) st1[i] += x; mn1[i] += x; if (i < n_) lz[i] += x; } void pus(int i) { if (lz[i]) { int l = i << 1, r = l | 1; if (mn1[l] < mn1[r]) put(i << 1 | 0, lz[i]); else if (mn1[l] > mn1[r]) put(i << 1 | 1, lz[i]); else put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]); lz[i] = 0; } } void pul(int i) { if (!lz[i]) { int l = i << 1, r = l | 1; st2[i] = max(st2[l], st2[r]); if (mn1[l] < mn1[r]) { st1[i] = st1[l]; st2[i] = max(st2[i], st1[r]); mn1[i] = mn1[l]; mn2[i] = min(mn2[l], mn1[r]); } else if (mn1[l] > mn1[r]) { st1[i] = st1[r]; st2[i] = max(st2[i], st1[l]); mn1[i] = mn1[r]; mn2[i] = min(mn1[l], mn2[r]); } else { st1[i] = max(st1[l], st1[r]); mn1[i] = mn1[l]; mn2[i] = min(mn2[l], mn2[r]); } } } void push(int i) { int h; for (h = h_; h > 0; h--) pus(i >> h); } void pull(int i) { while (i > 1) pul(i >>= 1); } void update(int i, int x) { i += n_; push(i); st1[i] = x, st2[i] = -1, mn1[i] = 0, mn2[i] = INF; pull(i); } void update_(int i, int x) { if (x <= mn1[i]) return; if (x < mn2[i]) { put(i, x - mn1[i]); return; } pus(i); update_(i << 1 | 0, x), update_(i << 1 | 1, x); pul(i); } int query(int l, int r) { int x; push(l += n_), push(r += n_); x = -1; for ( ; l <= r; l >>= 1, r >>= 1) { if ((l & 1) == 1) x = max(x, max(st1[l], st2[l])), l++; if ((r & 1) == 0) x = max(x, max(st1[r], st2[r])), r--; } return x; } int main() { static int aa[N], qu[N], hh[M], ans[Q]; int n, q, h, h_, h1, h1_, i, cnt; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &aa[i]); scanf("%d", &q); for (h = 0; h < q; h++) scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--; m = q; cnt = 0; for (i = 0; i < n; i++) { while (cnt && aa[qu[cnt - 1]] < aa[i]) cnt--; if (cnt) ll[m] = qu[cnt - 1], rr[m] = i * 2 - qu[cnt - 1], ww[m] = aa[qu[cnt - 1]] + aa[i], m++; qu[cnt++] = i; } cnt = 0; for (i = n - 1; i >= 0; i--) { while (cnt && aa[qu[cnt - 1]] <= aa[i]) cnt--; if (cnt) ll[m] = i, rr[m] = qu[cnt - 1] * 2 - i, ww[m] = aa[i] + aa[qu[cnt - 1]], m++; qu[cnt++] = i; } for (h = 0; h < m; h++) hh[h] = h; compare = compare_l, sort(hh, 0, m); for (h = 0; h < m; h++) { xx[h] = ll[hh[h]]; idx[hh[h]] = h; } build(m); compare = compare_r, sort(hh, 0, m); for (i = 0, h = 0; i < n; i++) { h_ = h; while (h_ < m && rr[hh[h_]] <= i) h_++; for (h1 = h; h1 < h_; h1++) if ((h1_ = hh[h1]) >= q) update(idx[h1_], ww[h1_]); update_(1, aa[i]); for (h1 = h; h1 < h_; h1++) if ((h1_ = hh[h1]) < q) ans[h1_] = query(idx[h1_], m - 1); h = h_; } for (h = 0; h < q; h++) printf("%d\n", ans[h]); return 0; }

Compilation message (stderr)

jumps.c: In function 'main':
jumps.c:159:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
jumps.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
jumps.c:162:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
jumps.c:164:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |   scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...