1 条题解
-
0
#include <bits/stdc++.h> using namespace std; int n, opt, l, r, c; int a[112345], b[112345]; int SZ, CNT, ID[112345], L[1004], R[1004], lazy[1004]; void update(int l, int r, int c) { if (ID[l] == ID[r]) { for (int i = l; i <= r; i++) a[i] += c; for (int i = L[ID[l]]; i <= R[ID[l]]; i++) b[i] = a[i]; sort(b + L[ID[l]], b + R[ID[l]] + 1); return; } for (int i = l; i <= R[ID[l]]; i++) a[i] += c; for (int i = L[ID[l]]; i <= R[ID[l]]; i++) b[i] = a[i]; sort(b + L[ID[l]], b + R[ID[l]] + 1); for (int i = ID[l] + 1; i <= ID[r] - 1; i++) lazy[i] += c; for (int i = L[ID[r]]; i <= r; i++) a[i] += c; for (int i = L[ID[r]]; i <= R[ID[r]]; i++) b[i] = a[i]; sort(b + L[ID[r]], b + R[ID[r]] + 1); } int query(int l, int r, int c) { int res = -1; if (ID[l] == ID[r]) { for (int i = l; i <= r; i++) if (a[i] + lazy[ID[i]] < c) res = max(res, a[i] + lazy[ID[i]]); return res; } for (int i = l; i <= R[ID[l]]; i++) if (a[i] + lazy[ID[i]] < c) res = max(res, a[i] + lazy[ID[i]]); for (int i = L[ID[r]]; i <= r; i++) if (a[i] + lazy[ID[i]] < c) res = max(res, a[i] + lazy[ID[i]]); for (int i = ID[l] + 1; i <= ID[r] - 1; i++){ int pos = lower_bound(b + L[i], b + R[i] + 1, c - lazy[i]) - b; if(pos > L[i]) res = max(res, b[pos-1] + lazy[i]); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; SZ = sqrt(n); for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = a[i]; ID[i] = (i - 1) / SZ + 1; } CNT = n / SZ + (n % SZ > 0); for (int i = 1; i <= CNT; i++) { L[i] = (i - 1) * SZ + 1; R[i] = i * SZ; sort(b + L[i], b + R[i] + 1); } for (int i = 1; i <= n; i++) { cin >> opt >> l >> r >> c; if (opt == 0) update(l, r, c); else cout << query(l, r, c) << "\n"; } return 0; } /* 0 区间加 1 区间前驱 */
- 1
信息
- ID
- 874
- 时间
- 1500ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 87
- 已通过
- 17
- 上传者